more inelegancies

Discussion in 'Perl Misc' started by Larry, Aug 9, 2007.

  1. Larry

    Larry Guest

    While we are on the subject of inelegancies ... what if I want to
    assign the elements of @zam to scalars, but I don't care about
    $zam[2]? I'd like to do:

    my ($foo, $bar, undef, $baz) = @zam;

    but that won't compile, so I'm forced to do:

    my ($foo, $bar, $baz);
    ($foo, $bar, undef, $baz) = @zam;

    Any better way?

    And furthermore....

    What if $foo and $baz are already my'ed... I'd like to do:

    ($foo, my $bar, $baz) = @wham;

    but Perl doesn't like that either, so I have to do:

    my $bar;
    ($foo, $bar, $baz) = @wham;
    Larry, Aug 9, 2007
    #1
    1. Advertising

  2. Larry

    Paul Lalli Guest

    On Aug 9, 2:59 pm, Larry <> wrote:
    > While we are on the subject of inelegancies ... what if I want to
    > assign the elements of @zam to scalars, but I don't care about
    > $zam[2]? I'd like to do:
    >
    > my ($foo, $bar, undef, $baz) = @zam;
    >
    > but that won't compile,


    Says who?

    $ perl -le'
    my @zam = qw/alpha beta gamma delta/;
    my ($foo, $bar, undef, $baz) = @zam;
    print for $foo, $bar, $baz;
    '
    alpha
    beta
    delta


    > Any better way?


    Array slices.

    $ perl -le'
    my @zam = qw/alpha beta gamma delta/;
    my ($foo, $bar, $baz) = @zam[0,1,3];
    print for $foo, $bar, $baz;
    '
    alpha
    beta
    delta


    > And furthermore....
    >
    > What if $foo and $baz are already my'ed... I'd like to do:
    >
    > ($foo, my $bar, $baz) = @wham;
    >
    > but Perl doesn't like that either


    Exactly what version of Perl are you running? This, again, works just
    fine for me.

    $ perl -le'
    my @zam = qw/alpha beta gamma delta/;
    my ($foo, $baz);
    ($foo, my $bar, $baz) = @zam[0,1,3];
    print for $foo, $bar, $baz;
    '
    alpha
    beta
    delta

    And indeed, B::Deparse even shows us that it's doing exactly what we
    mean (that the second 'my' applies only to $bar, not $baz:

    $ perl -MO=Deparse,-p -le'
    my @zam = qw/alpha beta gamma delta/;
    my ($foo, $baz);
    ($foo, my $bar, $baz) = @zam[0,1,3];
    print for $foo, $bar, $baz;
    '
    BEGIN { $/ = "\n"; $\ = "\n"; }
    (my(@zam) = ('alpha', 'beta', 'gamma', 'delta'));
    my($foo, $baz);
    (($foo, my($bar), $baz) = @zam[0, 1, 3]);
    ;
    foreach $_ (($foo, $bar, $baz)) {
    print($_);
    }
    -e syntax OK

    Paul Lalli
    Paul Lalli, Aug 9, 2007
    #2
    1. Advertising

  3. Larry

    Larry Guest

    On Aug 9, 3:35 pm, Paul Lalli <> wrote:
    > Exactly what version of Perl are you running? This, again, works just
    > fine for me.


    ??!!! They are working for me too! I don't know what I was
    thinking... maybe they used to not work in some older version of Perl
    and I never checked.... ??
    Larry, Aug 9, 2007
    #3
  4. On Aug 9, 8:57 pm, Larry <> wrote:
    > On Aug 9, 3:35 pm, Paul Lalli <> wrote:
    >
    > > Exactly what version of Perl are you running? This, again, works just
    > > fine for me.

    >
    > ??!!! They are working for me too! I don't know what I was
    > thinking... maybe they used to not work in some older version of Perl
    > and I never checked.... ??


    Yes, in older versions (5.5 I think) ISTR I had to say:

    (my ($foo, $bar), undef, my($baz)) = @zam;
    Brian McCauley, Aug 10, 2007
    #4
  5. On Aug 9, 8:35 pm, Paul Lalli <> wrote:

    > And indeed, B::Deparse even shows us that it's doing exactly what we
    > mean


    > $ perl -MO=Deparse,-p -le'

    ....
    > ($foo, my $bar, $baz) = @zam[0,1,3];


    deparse output...

    > ($foo, my($bar), $baz) = @zam[0, 1, 3];


    For this reason whenever I use my() inside a list like this I _always_
    put the explicit () on the my even when, as in this case, they are
    redundant.

    Code needs to be human-readable!
    Brian McCauley, Aug 10, 2007
    #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. shess
    Replies:
    0
    Views:
    288
    shess
    Nov 24, 2003
  2. Michael
    Replies:
    4
    Views:
    408
    Matt Hammond
    Jun 26, 2006
  3. AW på ZRX

    Applet uses more and more CPU.

    AW på ZRX, Sep 11, 2006, in forum: Java
    Replies:
    3
    Views:
    366
    Tris Orendorff
    Sep 13, 2006
  4. lovecreatesbeauty
    Replies:
    17
    Views:
    654
    Keith Thompson
    Jun 16, 2006
  5. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    215
    Jeff Wood
    Sep 29, 2005
Loading...

Share This Page