Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Scope in use

by LanX (Bishop)
on Sep 04, 2017 at 14:48 UTC ( #1198670=note: print w/replies, xml ) Need Help??

in reply to Scope in use

You are mixing compile-time and run-time behavior. *

The double nature of my is probably causing false expectations, but this works:

C:\Windows\system32>perl use warnings; use strict; my $lib; # compile time file scope (declaration) use lib $lib="XXX"; # compile time use scope (assignment) print $lib; # run time file scope (output) __END__ XXX C:\Windows\system32>


but if you want to totally confuse your colleague show him this working code ;-)

use warnings; use strict; my $lib; print $lib; # prints XXX use lib $lib="XXX"; __END__ XXX

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

*) additional to the effect already described in use has it's own scope?

Replies are listed 'Best First'.
Re^2: Scope in use
by RonW (Vicar) on Sep 06, 2017 at 19:31 UTC
    You are mixing compile-time and run-time behavior.

    I respectfully disagree.

    my does have both compile time and run time effects. my always has a compile time effect to declare the presence of one or more variables. It also has a run time effect. Before a block of code is run, it is compiled, thus the variables are declared so the rest of the block can compile. When the block is run, the run time effect of my is applied.

    In this case, the compiler is complaining "Global symbol "$lib" requires explicit package name" which also means that there isn't a declaration in scope. This implies the my in the use statement is in a different scope.

    When you remember that use X LIST is equivalent to BEGIN { require X; X->import(LIST)' }, this makes sense.

    If you don't know that (or forget it), it can/will be a surprise.

    In the OP's example, the run time effect is not the problem.

    The following works because my does have an effect at compile time:

    my $lib; # $lib declared at file scope use lib $lib = '.'; # file-scope $lib accessed in "use scope" print $lib; # file-scope $lib accessed in file scope

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1198670]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2018-07-22 15:07 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (454 votes). Check out past polls.