Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: File-scoped lexicals and eval scope?

by jbert (Priest)
on Nov 09, 2007 at 11:12 UTC ( [id://649886]=note: print w/replies, xml ) Need Help??


in reply to Re^2: File-scoped lexicals and eval scope?
in thread File-scoped lexicals and eval scope?

Good analysis on what was happening to $value.

But if the OP wants persistent, file-scope variables, I'd say that package variables are a better choice.

Changing the 'my' to 'our' would arrange that and be more readable than relying on tricks like 'if 0', imho.

Replies are listed 'Best First'.
Re^4: File-scoped lexicals and eval scope?
by Cambo (Initiate) on Nov 09, 2007 at 17:19 UTC
    That's the conclusion that we came to, finally, which is why the "fix" worked. It just didn't seem to be very intuitive with what we thought we knew about file-scoped lexicals. I never use package variables for this sort of thing, since those can be changed from anywhere, and I really don't like tracking down "action from a distance" issues. In this case, though, it may be the right solution. Thanks for the suggestion! Cambo
Re^4: File-scoped lexicals and eval scope?
by ysth (Canon) on Nov 11, 2007 at 03:57 UTC
    I guess I don't consider $value if 0; a trick. The idea is that, if sub initialize wants to have its use of $value be bound to the outer lexical even after that lexical's scope is exited, it needs to explicitly mention it. scalar($value); works as well, as does 0 if $value;.  Any of them should be accompanied by a comment. If you want to be pedantic, the latter is best documented as not triggering a 'Useless use' warning.
      I called it a trick because it exploits undefined behaviour in the language. perl 5.10, 5.12 or some future version may or may not preserve the same behaviour, so it seems to me bad practice to promulgate it's use.

      As I'm sure you know, perldoc perlsyn states:

      NOTE: The behaviour of a "my" statement modified with a statement modi‐ fier conditional or loop construct (e.g. "my $x if ...") is undefined. The value of the "my" variable may be "undef", any previously assigned value, or possibly anything else. Don’t rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.

        Ah. I wasn't suggesting my $value if 0;, nor would that work to make the sub close over the outer $value. I was suggesting $value if 0;, to incorporate a reference to the outer $value into the sub, while avoiding the "Useless use of a variable in void context" warning you'd get from a bare $value;.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found