Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Mysterious append failure.

by ccn (Vicar)
on Dec 09, 2004 at 12:32 UTC ( #413494=note: print w/replies, xml ) Need Help??

in reply to Mysterious append failure.

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.

Replies are listed 'Best First'.
Re^2: Mysterious append failure.
by gothic_mallard (Pilgrim) on Dec 09, 2004 at 12:35 UTC

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


    --- 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.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2022-05-20 07:59 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (73 votes). Check out past polls.