Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^5: To initialise or not to initialise?

by runrig (Abbot)
on Jun 24, 2004 at 23:14 UTC ( #369515=note: print w/replies, xml ) Need Help??


in reply to Re^4: To initialise or not to initialise?
in thread To initialise or not to initialise?

$_="default" for my ($foo, $bar, $baz);
That one surprises me that it works. I thought that the "for" statement modifier was mere syntactic sugar, since that deparses as:
u2-4.14 $ perl -MO=Deparse ./tst foreach $_ (my($foo, $bar, $baz)) { $_ = 'default'; } ./tst syntax OK
But if written as a straight for loop (as it deparses to), then the variables are local to the loop. And I could've sworn I tried that once upon a time, and it didn't work...probably won't be the last time I make that mistake :)

Replies are listed 'Best First'.
Re^6: To initialise or not to initialise?
by tilly (Archbishop) on Jun 24, 2004 at 23:19 UTC
    When you loop over a list, the local variable is aliased to each member of the list. If you modify the variable, then you modify the original member of the list as well.
      When you loop over a list, the local variable is aliased to each member of the list.

      Yes, I got that part. The part that surprises me is that B::Deparse deparses it to something that doesn't work, and I always assumed that the deparsing was correct. Consider:

      use strict; use warnings; foreach $_ (my ($foo, $bar, $baz)) { $_ = "default"; } print "$foo\n";
      This code doesn't run, and it is what I mistakenly assumed your code was the equivalent of (minus the use's and print).
        Ah. Because it scopes the variables differently depending on whether the loop is inline or not.
        The part that surprises me is that B::Deparse deparses it to something that doesn't work, and I always assumed that the deparsing was correct.

        Isn't that the definition of a bug in B::Deparse then?

        I wanted to file a bug report, but rt.cpan.org doesn't know about 'B::Deparse'. Where should it be sent to? Or did you do that already?

        -- Hofmator

Re^6: To initialise or not to initialise?
by demerphq (Chancellor) on Jun 28, 2004 at 14:00 UTC

    Using my with a modifier is skating on thin ice. While it works on the right side of the for it does bizarre (or at least counterintuitive) things on the left side. In an if the behaviour verges on the undefined (yes i know all about the static var effect.)

    The fact is however that B::Deparse has long gotten the scoping effects of a for modifier wrong. Consider:

    D:\Development>perl -MO=Deparse -e "my $x=$_ for 1..10;" foreach $_ (1 .. 10) { my $x = $_; } -e syntax OK

    The $x is not scoped to the for loop. Its scoped to the container of the for loop. Its also not so great at the scoping effects of the if modifier either:

    D:\Development>perl -MO=Deparse -e "my $x=1 if 0; my $y=1 if 1;" '???'; my $y = 1; -e syntax OK

    Re: Re: 'my' headache...Why Deparse is Wrong. and Regarding B::Deparse both discuss errors like this.

    The moral of the story is essentially don't trust deparse. :-) Its a useful tool, but blindly assuming it is correct will just muddle your brain.

    Btw, a little trick:

    Which even if it shameless advertising shows why use()ing Data::Dump::Streamer when debugging stuff can be a lot nicer than the other tools around.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-10-18 15:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (74 votes). Check out past polls.

    Notices?