SPUG:Split question

David Dyck david.dyck at fluke.com
Sat Mar 22 10:21:31 CST 2003


On Fri, 21 Mar 2003 at 21:07 -0800, Michael R. Wolf <MichaelRunningWolf at att...:

>     # root:q.mJzTnu8icF.:0:10:superuser:/:/bin/csh
>     # xxx::100:100:mb_daemon:/usr/make-believe/daemon:
>     my $line = qx(head /etc/passwd);
>     my ($id, @other_fields, $shell) = split /,/, $line, -1;
>
> Some accounts contain a null 7th field.  Without the 3rd argument,
> $shell will always contain the last *filled* argument, not the *7th*
> field.  Said differently, @other_fields will at most 5 fields, but
> occasionally less.

I don't think that your variable @other_fields is leaving the
variable $shell to collect the last paramter.  If I understand it, the
@other_fields soak up the rest of the arguments and $shell will always
be undef, right?

$ perl -le '@x=qw(a b c d e); ($a, at m,$e) = @x; print "a=<$a>, m=<@m>, e=<$e>"'
a=<a>, m=<b c d e>, e=<>

I skimmed through the documentation where I read this, but the only
thing close in perlsub in the discussion of prototypes.
       Any unbackslashed "@" or "%" eats all remaining arguments,
       and forces list context.

perlsub does say
       The "my" is simply a modifier on something you might
       assign to.  So when you do assign to variables in its
       argument list, "my" doesn't change whether those variables
       are viewed as a scalar or an array.

After more searching in perldata on assigning to a list I see the explanation

    The final element of a list assignment may be an array or a hash:

        ($a, $b, @rest) = split;
        my($a, $b, %rest) = @_;

    You can actually put an array or hash anywhere in the list, but the first
    one in the list will soak up all the values, and anything after it will
    become undefined. This may be useful in a my() or local().




More information about the spug-list mailing list