Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Mysterious append failure.

by BrowserUk (Patriarch)
on Dec 09, 2004 at 12:21 UTC ( [id://413492]=perlquestion: print w/replies, xml ) Need Help??

BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

If anyone can think of a better title, /msg me and I'll change it.

Probably another of my "can't see the wood for the trees" questions, but why does this work:

P:\test>perl -wl my $b .= 'fred' . 'bill'; print $b;; ^Z fredbill

And this:

P:\test>perl -wl my $a = 'a' x 10; my $b; $b .= substr( $a, $_, 10-$_ ) . '-' x $_ for 0 .. 9; print $b; ^Z aaaaaaaaaaaaaaaaaaa-aaaaaaaa--aaaaaaa---aaaaaa----aaaaa-----aaaa------ +aaa-------aa--------a---------

But not this?

P:\test>perl -wl my $a = 'a' x 10; my $b .= substr( $a, $_, 10-$_ ) . '-' x $_ for 0 .. 9; print $b; ^Z Use of uninitialized value in print at - line 3.

Examine what is said, not who speaks.        The end of an era!
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re: Mysterious append failure.
by ccn (Vicar) on Dec 09, 2004 at 12:32 UTC

    Lexical $b is out of scope. see

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

    So, your code is the same as

    P:\test>perl -wl my $a = 'a' x 10; foreach $_ (0 .. 9) my $b .= substr( $a, $_, 10-$_ ) . '-' x $_; } print $main::b; ^Z Use of uninitialized value in print at - line 3.

      Ahh.. that looks like it makes sense. Was forgetting for a minute that declaring $b there would actually scope it inside the loop.

      D'oh.

      --- Jay

      All code is untested unless otherwise stated.
      All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
      If in doubt ask.

        Was forgetting for a minute that declaring $b there would actually scope it inside the loop.
        This isn't quite true - the output of Deparse is wrong. The lexical is scoped wider than the loop, as can be seen here:
        $ perl585 -le '$x=99; my $x = $_ for 1..3; print $x' $
        (Note that $::x isn't printed, the lexical $x is instead.) However, the initialising and releasing of $x is scoped to within the loop.

        That may be construed as a bug. There again, statement modifiers on my declarations are usally buggy, and are best avoided.

        Dave.

Re: Mysterious append failure.
by broquaint (Abbot) on Dec 09, 2004 at 12:39 UTC
    Because you're declaring a variable in a loop and it falls out of scope outside of the loop e.g
    perl -we "my $a = q[foo] for 1; print $a" Use of uninitialized value in print at -e line 1.
    It's statement modifier abuse essentially.
    HTH

    _________
    broquaint

Re: Mysterious append failure.
by gothic_mallard (Pilgrim) on Dec 09, 2004 at 12:33 UTC

    I came across this same thing yesterday while trying to do a one-line append loop. I thought it was something to do with the my - possibly re-initialising on each loop?

    Haven't got to the bottom of it yet and basically ended up doing like you have in the second example (declare the variable and then do the loop seperately).

    --- Jay

    All code is untested unless otherwise stated.
    All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
    If in doubt ask.

Re: Mysterious append failure.
by Jasper (Chaplain) on Dec 09, 2004 at 14:02 UTC
    I was going to say that  use strict would have shown up your problem, but you're using $a and $b, so it probably wouldn't have been much use, after all. Bad $a. Bad $b.

    edit:
    [jasper@carabosse spamjab]$ perl -le 'use strict; my$x=99; my $x = $_ +for 1..3; print $x' [jasper@carabosse spamjab]$ perl -le 'use strict; my$x=99; for (1..3){ +my $x=$_}; print $x' 99 [jasper@carabosse spamjab]$
    curious? Is it?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://413492]
Approved by Corion
Front-paged by tomhukins
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2024-04-18 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found