Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Re: Re: Never lock $0 inside of a BEGIN block

by demerphq (Chancellor)
on May 21, 2003 at 21:37 UTC ( #259901=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Re: Never lock $0 inside of a BEGIN block
in thread Never lock $0 inside of a BEGIN block

Heh. Ive encountered a bunch of weirdness with using my and modifiers. (I've even argued that it should produce a warning.) I haven't seen the static variable trick. Thats scary. Cool Scary though. :-)

However I'm still not convinced that this error message couldn't be improved.

BTW, the static trick only works with if 0; and for (); and thus I still consider it something that should produce a warning.

sub umm() { print "---\n"} sub _caller(){(caller(1))[3]=~/^(?:main::)?(.*)$/} sub for_blah { my $foo for 1; printf "%-15s %d\n",_caller,$foo++; } sub not_for_blah { my $foo for (); printf "%-15s %d\n",_caller,$foo++; } sub if_blah { my $foo if 1; printf "%-15s %d\n",_caller,$foo++; } sub not_if_blah { my $foo if 0; printf "%-15s %d\n",_caller,$foo++; } for_blah(),for_blah(),for_blah(),umm, not_for_blah(),not_for_blah(),not_for_blah(),umm, if_blah(),if_blah(),if_blah(),umm, not_if_blah(),not_if_blah(),not_if_blah(),umm; __END__
# No assignment #With assignment for_blah 0 for_blah 0 for_blah 0 for_blah 0 for_blah 0 for_blah 0 --- --- not_for_blah 0 not_for_blah 0 not_for_blah 1 not_for_blah 1 not_for_blah 2 not_for_blah 2 --- --- if_blah 0 if_blah 1 if_blah 0 if_blah 1 if_blah 0 if_blah 1 --- --- not_if_blah 0 not_if_blah 0 not_if_blah 1 not_if_blah 1 not_if_blah 2 not_if_blah 2 --- ---

---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...


Comment on Re: Re: Re: Re: Never lock $0 inside of a BEGIN block
Select or Download Code
Replies are listed 'Best First'.
Static variables (oh, the horror())
by ChemBoy (Priest) on May 21, 2003 at 22:07 UTC
    BTW, the static trick only works with if 0; and for ();

    Not so. Observe:

    #!/usr/local/bin/perl -l sub horror { my $foo if "reset" eq shift; print ++$foo; } horror($_) for qw(foo bar baz reset bing bang boom); __END__ 1 2 3 4 1 2 3

    (Not that the sub name is intended as an editorial comment, or anything... <grin>)

    The nasty part is just that the cleanup (resetting $foo to undef) takes place at the end of the iteration in which my $foo is actually executed (which isn't exactly what you'd expect from a "reset" argument, but that's what you get with silly example code).



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

      Not so.

      Yes so. The code you demonstrate illustrates this nicely too. Perhaps you thought I meant a literal 0? I didnt I meant if FALSE. This behaviour while nice is a bug IMO.


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (16)
As of 2015-07-28 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls