while() doesn't localize $_ but for() does in some situations. Isthis expected?

Discussion in 'Perl Misc' started by Matthew Horsfall, Jul 14, 2010.

  1. It seems that while() loops don't localize $_ in certain situations,
    whereas for loops do.

    The following works fine:

    $ echo Test | perl -e 'for (qw(Word)) { for (<STDIN>) { print "$_";
    exit;} }'
    Test

    Whereas the following crashes:

    $ echo Test | perl -e 'for (qw(Word)) { while (<STDIN>) { print "$_";
    exit;} }'
    Modification of a read-only value attempted at -e line 1.

    Is this expected behavior?

    I will note this only seems to happen when modifying $_ using <>
     
    Matthew Horsfall, Jul 14, 2010
    #1
    1. Advertising

  2. Matthew Horsfall

    Willem Guest

    Re: while() doesn't localize $_ but for() does in some situations.Is this expected?

    Matthew Horsfall wrote:
    ) It seems that while() loops don't localize $_ in certain situations,
    ) whereas for loops do.

    For loops don't *localize* $_ either.
    What they do is *alias* $_ to whatever they're looping on.

    Try:
    sub foo { print } for (<STDIN>) { foo }
    As opposed to:
    sub foo { print } my $_; for (<STDIN>) { foo }

    ) Is this expected behavior?

    Yes. for() aliases whereas while() assigns.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jul 14, 2010
    #2
    1. Advertising

  3. On Jul 14, 9:42 am, Willem <> wrote:
    > Matthew Horsfall wrote:
    > ) Is this expected behavior?
    >
    > Yes.  for() aliases whereas while() assigns.
    >


    That clarifies things greatly. Thank you.
     
    Matthew Horsfall, Jul 14, 2010
    #3
  4. Re: while() doesn't localize $_ but for() does in some situations. Is this expected?

    >>>>> "Willem" == Willem <> writes:

    Willem> For loops don't *localize* $_ either.

    foreach loops do. for loops don't.

    See the following:

    my $x = 5;
    foreach $x (10..15) {
    print $x, "\n"; # prints 10..15
    }
    print $x, "\n"; # prints 5.

    Clearly, $x "inside" the foreach loop is a different $x from the
    outside.

    print "Just another Perl hacker,"; # the original

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
     
    Randal L. Schwartz, Jul 14, 2010
    #4
  5. Matthew Horsfall

    Guest

    Re: while() doesn't localize $_ but for() does in some situations. Is this expected?

    On Wed, 14 Jul 2010 06:23:58 -0700 (PDT), Matthew Horsfall <> wrote:

    >It seems that while() loops don't localize $_ in certain situations,
    >whereas for loops do.
    >
    >The following works fine:
    >
    >$ echo Test | perl -e 'for (qw(Word)) { for (<STDIN>) { print "$_";
    >exit;} }'
    >Test
    >


    Here, the second 'for()' just re-aliases $_ to <STDIN>

    >Whereas the following crashes:
    >
    >$ echo Test | perl -e 'for (qw(Word)) { while (<STDIN>) { print "$_";
    >exit;} }'
    >Modification of a read-only value attempted at -e line 1.


    But you don't mention if you want to see "Word" printed or what
    <STDIN> returns

    >
    >Is this expected behavior?
    >
    >I will note this only seems to happen when modifying $_ using <>


    No, it happens anytime you try to write to a constant:
    $_ = "modify" for (qw(Word));

    while() has a special property, only the first assignable item in the expression will
    do an asignment to the default variable (the one you have aliased to a constant).

    So (untested),
    $ echo Test | perl -e 'for (qw(Word)) { while ($_ and <STDIN>) { print "$_";
    would work but it won't print out what you think it will.
    Or,
    for (qw(Word)) {
    while ($_ && <STDIN>) {
    print $_,"\n";
    }
    }

    Where above, <STDIN> is not assigned to anything.
    On the other hand,
    while (9 && <STDIN>)
    in the above context wil produce that error.


    -sln
     
    , Jul 14, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGFuaWVsIFdhbHplbmJhY2g=?=

    How to handle save situations in asp.net?

    =?Utf-8?B?RGFuaWVsIFdhbHplbmJhY2g=?=, Jul 21, 2004, in forum: ASP .Net
    Replies:
    12
    Views:
    960
    Steven Cheng[MSFT]
    Jul 27, 2004
  2. DigHazuse

    Validation on all PostBack situations

    DigHazuse, Nov 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    DigHazuse
    Nov 29, 2005
  3. max
    Replies:
    11
    Views:
    603
    freemont
    Nov 11, 2006
  4. George2

    bad_alloc in these situations?

    George2, Feb 28, 2008, in forum: C Programming
    Replies:
    0
    Views:
    356
    George2
    Feb 28, 2008
  5. rana
    Replies:
    1
    Views:
    815
Loading...

Share This Page