logo       

Re: On using $_ in subroutines: msg#00369

perl-beginners

Subject: Re: On using $_ in subroutines

On Sun, Jul 26, 2009 at 17:59, John W. Krahn<jwkrahn@xxxxxxx> wrote:
> Bryan Harris wrote:
snip
>> Oddly, perl won't let me do "my ($_) = shift;", so I'm stuck having to use
>> another variable.
>
> Perl 5.10 *will* let you do "my $_".
snip

Be warned that you may reveal bugs if you use make $_ lexical:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use List::Util qw/first/;

my $first = (first { $_ eq "f" } "a" .. "z") // "undef";

say "with global \$_: $first";

{
my $_ = "foo";
my $first = (first { $_ eq "f" } "a" .. "z") // "undef";

say "with lexical \$_: $first";
}

my $first = (first { $_ eq "f" } "a" .. "z") // "undef";

say "still fine with global \$_: $first";

{
local $_ = "foo";

my $first = (first { $_ eq "f" } "a" .. "z") // "undef";

say "still fine with local \$_: $first";
}

This bug is currently being discussed on the Perl 5 Porters list and
may be fixed in 5.10.1.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/


Google Custom Search

News | Mail Home | sitemap | FAQ | advertise