constants

Where are perl constants replaced by their values?

耗尽温柔 提交于 2019-12-12 10:35:11
问题 I tried to use constant values in perl and stumbled upon the following weird behaviour: #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use constant { a => "b" }; my $c = { a => a }; my %d; $d{a} = a; print Dumper($c); print Dumper(\%d); will output $VAR1 = { 'a' => 'b' }; $VAR1 = { 'a' => 'b' }; The constant a was replaced on the right hand side of the expressions $d{a} = a and a => a , but not on the left side. I know that constants are implemented using inlinable subs

What is the perl equivalent of MAX_INT?

一笑奈何 提交于 2019-12-12 10:29:29
问题 I am new to perl and seeking the lowest value in an @array . Is there some constant that represents a very large integer number? I know I could sort the array and take the beginning, but that seems to be a lot of wasted CPU cycles. What is an elegant solution to my problem in Perl? 回答1: In the general case, you can use undef to signal a non-existent value; perl scalars aren't restricted to holding just integers. That would be written: my $min; # undef by default for my $value (@array) { $min

Defining Constants in Django

孤人 提交于 2019-12-12 09:29:29
问题 I want to have some constants in a Django Projects. For example, let's say a constant called MIN_TIME_TEST . I would like to be able to access this constant in two places: from within my Python code, and from within any Templates. What's the best way to go about doing this? EDIT: To clarify, I know about Template Context Processors and about just putting things in settings.py or some other file and just importing. My question is, how do I combine the two approaches without violating the "Don

How does the “#map(&proc)” idiom work when introspecting module classes?

丶灬走出姿态 提交于 2019-12-12 07:45:19
问题 Presenting the Idiom I found an interesting but unexplained alternative to an accepted answer. The code clearly works in the REPL. For example: module Foo class Bar def baz end end end Foo.constants.map(&Foo.method(:const_get)).grep(Class) => [Foo::Bar] However, I don't fully understand the idiom in use here. In particular, I don't understand the use of &Foo , which seems to be some sort of closure, or how this specific invocation of #grep operates on the result. Parsing the Idiom So far, I

Why language designers allow interface to contain fields?

自古美人都是妖i 提交于 2019-12-12 05:25:21
问题 As per Item 19 in Effective Java, one must use an interface to only represent a type. In this case the interface would contain the methods that form part of the public contract that is exposed by a class (implementing the interface) to the clients If so, why do interfaces support fields in the first place? Since the fields are implicitly public, static, final, (and hence constants), why did the language designers support having them? If they were unsupported then developers would invariably

Defining the document root directory as the ROOT_PATH

泄露秘密 提交于 2019-12-12 05:24:06
问题 I use some constants to define paths to specific directories on a website that I'm building, like this: define("ROOT_PATH", "http://website.com/"); define("IMAGES_DIR", ROOT_PATH . "images/"); and I usually use them like this: echo "<img src='" . IMAGES_DIR . "some_image.jpg' />"; now I want to know if there is any difference between define("ROOT_PATH", "http://website.com/"); and define("ROOT_PATH", "/home/username/public_html/"); and which one of them should I use? And why? 回答1: You can't

How would i sort a queue using only one additional queue

三世轮回 提交于 2019-12-12 05:15:04
问题 So basically, Im asked to sort a queue, and i can only use one helper queue , and am only allowed constant amount of additional memory. How would i sort a queue using only one additional queue ? 回答1: Personally, I don't like interview questions with arbitrary restrictions like this, unless it actually reflects the conditions you have to work with at the company. I don't think it actually finds qualified candidates or rather, I don't think it accurately eliminates unqualified ones. When I did

Is there any way to carry down a PHP magic constant as a function default?

a 夏天 提交于 2019-12-12 03:55:41
问题 The idea here is to create a method for logging and debugging purposes, that doesn't require passing said method the associated 'magic constants'. Effectively, I'm trying to achieve this using a method definition like so: function Debug($Message,$File=__FILE__,$Line=__LINE__) { ... } The problem I am running in to is that if I define the above method in a file other than the one I am 'debugging', I end up with the file and line from the file the method is defined in, rather than the one I am

Ruby accessing constants from inner-class

烈酒焚心 提交于 2019-12-12 03:09:09
问题 I have a nested class like so: class Mammal H = "Mammal" class Human H = "Human" end end And I want to make an Human object and after access the Human's constant, like so: human = Mammal::Human.new # makes an object successfully puts human::H # does not work ** puts Mammal::Human::H # works ["Human"] puts Mammal::H # works ["Mammal"] **.. but it won't work ("..is not a class/module [TypeError]"). What am i doing wrong? 回答1: What am I doing wrong? You're trying to refer a constant from a wrong

Define a constant function call

我只是一个虾纸丫 提交于 2019-12-12 02:33:55
问题 My code is like this: <?php define("ERROR", "SOMETHING WRONG WITH MY DATABASE"); ... if (!mysql_query($q)){ die(ERROR); } ?> Now I want to replace "SOMETHING WRONG WITH MY DATABASE" with mysql_error() in case I want to debug it. what is the easiest way ? This does not seem to work work: define("ERROR", mysql_error()); ---- edit --- I don't want to use mysql_error() under production environment, it may help the attacker figure out something related to my database? That's my point of using a