APM: shift question

Mike South msouth at fulcrum.org
Sat Oct 19 20:18:45 CDT 2002


>From austin-admin at mail.pm.org  Sat Oct 19 19:25:41 2002
>
>How come I can go
>
>$x=shift @array;
>
>and $x is equal to the first array element, but
>
>shift @array;
>
>doesn't put the first element to $_?
>

That's a good question.  You already got the "Stok" answer, "because
it doesn't", which is as good an answer as any.  However, I just wanted
to chime in because I remember being in the same situation as you, 
many years ago, when I started getting used to how Perl would just
do what I expected even when I didn't tell it to, and then I started
running into places, just like the example above, where I thought
something like "Well, if I do a foreach (@foo) and don't give it
a variable name it goes into $_.  So it would make sense that if
I don't tell shift() where to put something it will go to the
same place."  It was a bit frustrating--right when I thought
I knew what perl was thinking I was thinking, I was wrong on
quite a few occasions.
 
My advice to you is to become familiar with perldoc.  You are
now at the  point where you will be wondering things like this
a lot.  So, in this case, you could do a

perldoc -f shift

and you would get a nice little description of what shift does,
and what sorts of magic are associated with it.  (The -f is for
"function".)  Now, you are also probably curious about where, 
specifically, the special varialbe $_ is used even though you
don't specify it.  For that you need:

perldoc perlvar

And search for $_ in that. (you may need to backwhach the "$"
in searching that, depending on your system--that is, search for
\$_).

Here is part of the perlvar page about magical use of $_:

	Here are the places where Perl will assume $_ even
	if you don't use it:

	·  Various unary functions, including functions
	like ord() and int(), as well as the all file
	tests ("-f", "-d") except for "-t", which
	defaults to STDIN.

	·  Various list functions like print() and
	unlink().

	·  The pattern matching operations "m//", "s///",
	and "tr///" when used without an "=~" operator.

	·  The default iterator variable in a "foreach"
	loop if no other variable is supplied.

	·  The implicit iterator variable in the grep()
	and map() functions.

	·  The default place to put an input record when a
	"<FH>" operation's result is tested by itself
	as the sole criterion of a "while" test.
	Outside a "while" test, this will not happen.

Note, however, that this has not told you all the places,
because it says "various X functions like Y and Z".  So if you 
are wondering about whether a particular function uses it, 
just do a quick perldoc -f functionname to check.

(For example, I didn't know until I just read it in perldoc -f shift
that shift will act on the @ARGV array if you are in file 
scope and you don't give it an argument.  Cool.)

mike

(ps  if anything I said here didn't make sense feel free to
ask follow-up questions)





More information about the Austin mailing list