Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Use of uninitialised value... but it is defined ?!

by BrowserUk (Patriarch)
on Mar 02, 2016 at 08:09 UTC ( [id://1156619]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Use of uninitialised value... but it is defined ?!
in thread Use of uninitialised value... but it is defined ?!

Intriguing.

So by the time the BEGIN block runs, perl knows enough to know that there is(will be) a lexical $x, sufficient to be able to assign to it; but not enough to have initialised it.

That's just weird!


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re^3: Use of uninitialised value... but it is defined ?!

Replies are listed 'Best First'.
Re^4: Use of uninitialised value... but it is defined ?!
by dave_the_m (Monsignor) on Mar 02, 2016 at 08:37 UTC
    'my' has a compile-time and run-time action. The compile-time action is to place a new undef SV in the pad of the sub currently being compiled. This var is immediately available to be closed over when compiling any following subs or BEGIN blocks within the same scope.

    At run time it pushes the current SV in the pad onto the args stack, and adds a note to free the SV on scope exit (and replace with a fresh new one).

    An assignment, such as ..=1 happens at run time of course.

    Dave.

      Somehow, the first example seemed appropriate here :)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^4: Use of uninitialised value... but it is defined ?!
by Eily (Monsignor) on Mar 02, 2016 at 08:28 UTC

    Not really, you can set a variable's value with arbitrary code, so it's rather consistent of perl to never run it. For example if you had :

    my $first = 1; my @second = grep { ($_+1) % 2 } map { $_ ** 3 } 1..12; my %third = some_function();
    Perl could easily apply the first assignment during compilation, you would kind of expect the second to be run at run time, and the third assignment might either fail or yield the wrong data depending on whether a definition and redefinition of some_function have been seen at that point.

    But perl knowing about the structure of the code (eg. lexicals in a given scope) at compile time is kind of the point.

Re^4: Use of uninitialised value... but it is defined ?!
by Discipulus (Canon) on Mar 02, 2016 at 08:33 UTC
    BrowserUk: exactly, the order matter. the slot for the variable is created (no errors for undeclared variables) but the assignement not yet. BEGIN blocks jump directly in runtime during the compilation of the program. Quite a good feature!

    Consider the output of the following snippet: perl -wE "my $time = time; BEGIN{say time; sleep 5}; say $time"

    PS the above behaves the same with lexical and package vars:

    perl -E "$time = time; BEGIN{say time; sleep 5}; say $time"

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1156619]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-24 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found