What is the global hash %_ ?

O

ozarfreo

I've seen that %_ is treated as a special variable in Perl, as $_ and
@_ are: no warning is issued when using it without a package name, no
'my' is allowed on that var; the problem is that I could not find it
among Perl's documentation.

I'd like to use it in a program but am not sure whether I might be
interfering with some other intended use for %_ . Can someone point out
what is this variable exactly, and whether it is safe to use it?
 
J

John W. Krahn

I've seen that %_ is treated as a special variable in Perl, as $_ and
@_ are: no warning is issued when using it without a package name, no
'my' is allowed on that var; the problem is that I could not find it
among Perl's documentation.

I'd like to use it in a program but am not sure whether I might be
interfering with some other intended use for %_ . Can someone point out
what is this variable exactly, and whether it is safe to use it?

If it is not listed in perlvar then perl doesn't care what you do with it.


John
 
G

Gunnar Hjalmarsson

I've seen that %_ is treated as a special variable in Perl, as $_ and
@_ are: no warning is issued when using it without a package name, no
'my' is allowed on that var; the problem is that I could not find it
among Perl's documentation.

I'd like to use it in a program but am not sure whether I might be
interfering with some other intended use for %_ .

Considering similar variables that are used by Perl and documented in
perlvar, it does not seem unlikely that it will be predefined in future
Perl versions. Hence, I for one would not use it. To be safe.
 
D

Dr.Ruud

(e-mail address removed) schreef:
I've seen that %_ is treated as a special variable in Perl, as $_ and
@_ are: no warning is issued when using it without a package name, no
'my' is allowed on that var; the problem is that I could not find it
among Perl's documentation.

I'd like to use it in a program but am not sure whether I might be
interfering with some other intended use for %_ . Can someone point
out what is this variable exactly, and whether it is safe to use it?

$ perl -Mstrict -wle '
$, = "<\n";
@_ = (one=>1, two=>2, three=>3);
print @_, "$_[1] <--"
'
one<
1<
two<
2<
three<
3<
1 <--


$ perl -Mstrict -wle '
$, = "<\n";
%_ = (one=>1, two=>2, three=>3);
print %_, "$_{one} <--"
'
three<
3<
one<
1<
two<
2<
1 <--
 
O

ozarfreo

Ferry said:
Those described in perlvar have additional functionality.
Those not described there may have one in the future, so you shouldn't
use them nevertheless.
From perlmod:
<< Only identifiers starting with letters (or underscore) are stored
in a package's symbol table. All other symbols are kept in package
"main", including all punctuation variables, like $_. >>

Thanks to all! That completely answers my question.
 
B

Brian McCauley

Gunnar said:
Considering similar variables that are used by Perl and documented in
perlvar, it does not seem unlikely that it will be predefined in future
Perl versions. Hence, I for one would not use it. To be safe.

It seems moderately unlikely to be and I do sometimes %_ it to
represent the "current context" in callback APIs.

{
local *_ = \%context;
$foo_callback->($foo_callback_arg1,$foo_callback_arg2);
}
 
D

Dr.Ruud

Brian McCauley schreef:
It seems moderately unlikely to be and I do sometimes %_ it to
represent the "current context" in callback APIs.

{
local *_ = \%context;
$foo_callback->($foo_callback_arg1,$foo_callback_arg2);
}

I once tried to make &_ do as much as possible what say() will, a bit
like this:

sub _{print @_, "\n"}

sub _{print +(@_ ? @_ : $_), $/}

sub _(*@)
{
local $\ = "\n" ;
0 == @_ and return print ;
1 == @_ and ref $_[0] ? return print @{ $_[0] }
: return print $_[0] ;
my $fh = $_[0] ;
ref $_[1] ? print $fh @{ $_[1] }
: print $fh $_[1] ;
}

See also:
perl -MPerl6::Say -e 'say for (1,2,3)'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top