For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > November 2004 > Beginners question. $_ Variable.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Beginners question. $_ Variable.
Mitchell Hulscher

2004-11-29, 4:05 pm

Hello everyone,

Since a few ws I've been trying to learn Perl and so far it's
been going quite good.
My only problem is that I can't really figure out what the $_
variable does or is.
I know this might sound as a very stupid question,
but whether I am dumb or all tutorials I read are written silly,
I just can't seem to figure it out.

I thought of a theory that the $_ variable gets assigned to the
latest scalar assigned.
But of course, no results.

If someone could please help me, I'd really appreciate it.

Mitch. :-)

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 5
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------

momichan

2004-11-29, 4:05 pm

On 28 Nov 2004 19:04:14 GMT, unacceptable@gmail.com (Mitchell
Hulscher) wrote:

>Hello everyone,
>
>Since a few ws I've been trying to learn Perl and so far it's
>been going quite good.
>My only problem is that I can't really figure out what the $_
>variable does or is.
>I know this might sound as a very stupid question,
>but whether I am dumb or all tutorials I read are written silly,
>I just can't seem to figure it out.
>
>I thought of a theory that the $_ variable gets assigned to the
>latest scalar assigned.
>But of course, no results.
>
>If someone could please help me, I'd really appreciate it.
>
>Mitch. :-)
>
>----------------------------------------------
>Posted with NewsLeecher v2.0 Beta 5
> * Binary Usenet Leeching Made Easy
> * http://www.newsleecher.com/?usenet
>----------------------------------------------



The most common special variable is $_, which contains the default
input and pattern-searching string. For example:

foreach ('hickory','dickory','doc') {
print;
}
The first time the loop is executed, "hickory" is printed. The second
time around, "dickory" is printed, and the third time, "doc" is
printed. That's because in each iteration of the loop, the current
string is placed in $_ and is used by default by print. Here are the
places where Perl will assume $_, even if you don't specify it:

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

Various list functions such as 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 line-input operation's
result is tested by itself as the sole criterion of a while test
(i.e., <filehandle> ). Note that outside of a while test, this does not
happen.


remember to use "perldoc perlvar"
Jürgen Exner

2004-11-29, 4:05 pm

Mitchell Hulscher wrote:
> Since a few ws I've been trying to learn Perl and so far it's
> been going quite good.
> My only problem is that I can't really figure out what the $_
> variable does or is.


$_ is simply the default variable, which is used by _many_ Perl functions
and operators unless the user explicitely specifies a different variable or
value. To see which functions and operators use $_ by default you need to
check the documentation for each function.

E.g. 'perldoc -f print':
If LIST is also omitted, prints "$_" [...]

Or 'perldoc -f split':
If EXPR is omitted, splits the "$_" string.

Or 'perldoc perlop' the s operator:
If no string is specified via the "=~" or "!~" operator, the
"$_" variable is searched and modified.

Very often $_ is set using the standard pattern
while (<FOO> ) {
# do something with $_
}
which reads the file that is associated with the file handle FOO line by
line into $_

jue


Joe Smith

2004-11-29, 8:56 pm

Mitchell Hulscher wrote:

> I thought of a theory that the $_ variable gets assigned to the
> latest scalar assigned.


Think of it as input, not output.

Many built-in perl functions use $_ as their input if you don't
specify any arguments and the function needs one.

Some operators use $_ for both input and output.
$_ = 'abcde';
tr /bc/12/;
s/e/EEEEE/;
print;
Paul Lalli

2004-11-30, 4:01 pm

"Joe Smith" <joe@inwap.com> wrote in message
news:FlMqd.166420$HA.134626@attbi_s01...
> Mitchell Hulscher wrote:
>
>
> Think of it as input, not output.


Why?

> Many built-in perl functions use $_ as their input if you don't
> specify any arguments and the function needs one.


Yes, and many use $_ as their 'output'. (I don't like this terminology,
but okay).
A foreach-style loop, and the special while (<> ) { } both leap to mind.
Not to mention grep and map.

> Some operators use $_ for both input and output.
> $_ = 'abcde';
> tr /bc/12/;
> s/e/EEEEE/;
> print;


Yes, they do. So why would you want to think of $_ as only 'input'?

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com