perl - When does a global variable get its value assigned? -
I had to face some strange behavior, which indicates that I have some basic about Perl script execution and initial order Things do not understand. The following example:
#! Strict / usr / bin / env perl; Use warnings; Print & amp; Get_keys (), "\ n"; Use wars qw /% / hello; # 'My% hello = (...);' % Hello = (a => 1, b => 2); Sub get_keys {joining the return (',', sort key% hello); }
Prints an empty string. This means that although the variable is already visible, because the assignment with the state has not yet reached, it has no value. (Using a scalar instead of a hash, a warning will be generated about unregistered variables.)
Is that supposed to behave?
I would also be happy for the RTFM indicators.
to:
A
my
In both the compiled time compiling time and a run-time effect time, the compiler takes notice of it. Its main use is to use quietstrict 'wars'
.... delay in running the actual initial time, however, is therefore executed at the right time. At this point,% Hello is a partly scope variable (my compilation took effect at the time), but it still has no keys print get_keys () ; My% hello = (one => 1, b => 2); # Ish content now hash print get_keys (); Other notes: (1) You should usemy
orinstead of
instead of
Uses
. (2) In normal circumstances, do not call the function with leading ampersands:foo ()
instead ofand & amp; Use foo ()
.
Comments
Post a Comment