[comp.lang.perl] bizarre interaction between @_ and `last`

tneff@bfmny0.BFM.COM (Tom Neff) (06/14/90)

In article <F*kr6!i@cs.psu.edu> flee@psuvax1.cs.psu.edu (Felix Lee) writes:
>Maybe backticks should do something similar to <FILEHANDLE> and return
>an array of lines in array contexts?

This would be a worthwhile enhancement!  I hope Larry considers it.

stef@zweig.sun (Stephane Payrard) (06/14/90)

The following script is supposed to print the login name of the
people which have logged the computer which executes the script.
I am running perl Patch level: 18 on a sun 4/110 with SunOS 4.1

As is, thw script prints only one name.

If I uncomment the two first lines and comment the thrid line,
it works fine.

I was suspecting that `last` was returning the first line of the
`last' command; but inserting a 'print @_' after the third line shows
that @_ at this times contains the whole output of last.
But if I insert the 'print @_' after the foreach loop, it prints only
one line.

That seems weird to me!!!

# open(P,"last|");
# @_=<P>;
@_=`last`;
foreach (@_) {
    m/(\w*)/;
    $name{$1}="";
}
delete $name{"shutdown"};
delete $name{"reboot"};
delete $name{"wtmp"};
$,=" ";
print %name;




        stef
--
Stephane Payrard -- stef@sun.com -- (415) 336 3726
Sun Microsystems -- 2550 Garcia Avenue --  M/S 10-09 -- Mountain View  CA 94043
room number: 138
                     
                     

tchrist@convex.COM (Tom Christiansen) (06/14/90)

In article <STEF.90Jun14031206@zweig.sun> stef@zweig.sun (Stephane Payrard) writes:
>
>If I uncomment the two first lines and comment the thrid line,
>it works fine.
>
># open(P,"last|");
># @_=<P>;
>@_=`last`;
>foreach (@_) {

If you want to process your last output a line at a time, then 
you'd do better with the 
    while (<P>) {
construct.  It's cheaper than reading it all in as an array, because
you only have to use the memory it takes for the longest line.

The `backquoting` mechanism isn't working as you think it is: it sucks 
in ALL the output from last as one SCALAR string, and does NOT do the
implicit:
    @_ = split(/\n/,`last`) 
that you seem to be assuming it's doing.  


--tom
--

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"

flee@psuvax1.cs.psu.edu (Felix Lee) (06/14/90)

> @_=`last`;

Backticks return a single scalar value, so @_ is now an array with one
element which happens to be a multiline string.

Maybe backticks should do something similar to <FILEHANDLE> and return
an array of lines in array contexts?
--
Felix Lee	flee@cs.psu.edu