Loop consuming my list

Discussion in 'Perl Misc' started by Marc Girod, Oct 9, 2009.

  1. Marc Girod

    Marc Girod Guest

    Hello,

    I was surprised to see in my own code, a difference between
    the two lines (out of context...):

    $self->rmhlink($type, $_) for @pl;
    and:
    for my $p (@pl) { $self->rmhlink($type, $p) }

    The former consumed the contents of @pl.
    The latter did not.

    The rmhlink (member) function reads its arguments from @_,
    and returns a value, often undef.

    I tried to reproduce the case naively, but failed:

    $ perl -wle '@s=qw(a b c);sub f{return};f($_)for@s;map{print $_}@s'
    a
    b
    c

    Is there thus an interaction from the package?

    I cannot get this either...

    $ perl -wle 'package Foo;@s=qw(a b c);sub new{$t=shift;$s={};bless $s,
    $t}sub foo{return};$f=new Foo;$f->foo($_)for@s;map{print $_}@s'
    a
    b
    c

    I am afraid this is a symptom that I missed something fundamental...
    Who is eating my list?

    Thanks,
    Marc
     
    Marc Girod, Oct 9, 2009
    #1
    1. Advertising

  2. Marc Girod <> writes:

    > Hello,
    >
    > I was surprised to see in my own code, a difference between
    > the two lines (out of context...):
    >
    > $self->rmhlink($type, $_) for @pl;
    > and:
    > for my $p (@pl) { $self->rmhlink($type, $p) }
    >
    > The former consumed the contents of @pl.
    > The latter did not.


    You probably have to defined what you mean by 'consumed the contents
    of @pl'. I assume that you mean that the content of @pl has changed
    somehow. The problme is that the first construction makes $_ an alias
    to the elements in @pl and that $_ is a global variable.

    In perl 5.10 you can make $_ a lexical variable by adding a 'my $_;'
    somewhere in scope befor the loop:

    my $_;
    $self->rmhlink($type, $_) for @pl;

    For older perls you have to localize $_ in your rmhlink method. I
    would probably do both.

    See also:
    http://peter.makholm.net/2009/06/24/the-evil-of-a-global-_/

    //Makholm
     
    Peter Makholm, Oct 9, 2009
    #2
    1. Advertising

  3. Marc Girod

    Marc Girod Guest

    Thank you Peter,

    On Oct 9, 12:29 pm, Peter Makholm <> wrote:

    > You probably have to defined what you mean by 'consumed the contents
    > of @pl'.


    Indeed. In this case, the list was empty after running the loop.

    > The problem is that the first construction makes $_ an alias
    > to the elements in @pl and that $_ is a global variable.


    That's what I didn't understand.
    I thought it was lexical...

    > In perl 5.10 you can make $_ a lexical variable by adding a 'my $_;'
    > somewhere in scope befor the loop:


    Thanks. I can now see that it is what I did, without the
    side-effect of the name.

    > See also:http://peter.makholm.net/2009/06/24/the-evil-of-a-global-_/


    Excellent.
    I *did* miss something important.

    Marc
     
    Marc Girod, Oct 9, 2009
    #3
  4. Marc Girod

    Frank Seitz Guest

    Marc Girod wrote:
    > On Oct 9, 12:29 pm, Peter Makholm <> wrote:
    >
    >> You probably have to defined what you mean by 'consumed the contents
    >> of @pl'.

    >
    > Indeed. In this case, the list was empty after running the loop.


    It is not possible to change array size by manipulating $_.
    The array size must be the same.

    Frank
    --
    Dipl.-Inform. Frank Seitz
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

    Homepage: http://www.fseitz.de/
    XING-Profil: http://www.xing.com/profile/Frank_Seitz2
     
    Frank Seitz, Oct 9, 2009
    #4
  5. Marc Girod

    Marc Girod Guest

    On Oct 9, 1:50 pm, Frank Seitz <> wrote:

    > It is not possible to change array size by manipulating $_.


    Sorry, you are correct. The items were set to ''.
    I tried the advice on 5.8.8, and checked that I cannot
    make $_ lexical there, but that local $_ within rmhlink
    works.

    I believe that I'll be happy with the insight, and with
    lexicalizing by naming as I did.

    Thanks,
    Marc
     
    Marc Girod, Oct 9, 2009
    #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. author
    Replies:
    5
    Views:
    649
    author
    Jul 28, 2004
  2. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,417
    Karl Seguin
    Feb 16, 2005
  3. Replies:
    5
    Views:
    626
    benben
    Jan 31, 2006
  4. Roedy Green
    Replies:
    3
    Views:
    458
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    458
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page