AW: _ handle and sub _
-Ursprüngliche Nachricht- Von: Rafael Garcia-Suarez [mailto:[EMAIL PROTECTED] Gesendet: Montag, 5. September 2005 16:24 An: Dintelmann, Peter Cc: perl5-porters@perl.org Betreff: Re: _ handle and sub _ On 9/5/05, Dintelmann, Peter [EMAIL PROTECTED] wrote: When a subroutine _ is defined it may mask the _ handle used by file test operators. #!/opt/perl32/bin/perl use strict; use warnings; sub _ { /etc/hosts } open my $f, /dev/null or warn $!; stat $f or warn $!; print size: , -s _, \n; In the last line sub _ is invoked and the script prints the size of /etc/hosts instead of 0. However -X *_ seems to get this right. Should we mention this in perlfunc? I don't think so, since this behaviour is the same with any other identifier. Certainly, but _ is in main which may cause problems with modules. Here is a simple example using File::Find which resembles the problem I was originally debubbing. #!/opt/perl32/bin/perl -l use strict; BEGIN { sub _ {1} } use File::Find; # _ handle used in File::Find my $cnt = 0; find sub {$cnt++}, /etc; print $cnt; # 1 instead of 815 on my system Replacing the _ handle with *_ in the module saved my day (do you think we should patch File::Find?) which reminds me about the usage of local $_ in modules. Though the choice of _ as a sub name may be problematic as pointed out by MJD there is existing code which uses it already.
_ handle and sub _
When a subroutine _ is defined it may mask the _ handle used by file test operators. #!/opt/perl32/bin/perl use strict; use warnings; sub _ { /etc/hosts } open my $f, /dev/null or warn $!; stat $f or warn $!; print size: , -s _, \n; In the last line sub _ is invoked and the script prints the size of /etc/hosts instead of 0. However -X *_ seems to get this right. Should we mention this in perlfunc? --- /opt/perl32/lib/5.8.7/pod/perlfunc.pod Wed Jun 1 16:20:40 2005 +++ /var/tmp/perlfunc.pod Mon Sep 5 17:05:06 2005 @@ -366,6 +366,10 @@ print Text\n if -T _; print Binary\n if -B _; +If a function named C_ is defined, the file tests will operate on its +return value instead of the saved stat structure. Replace C-X _ with +C-X *_ to circumvent this behaviour. + =item abs VALUE =item abs
Re: _ handle and sub _
On 9/5/05, Dintelmann, Peter [EMAIL PROTECTED] wrote: When a subroutine _ is defined it may mask the _ handle used by file test operators. #!/opt/perl32/bin/perl use strict; use warnings; sub _ { /etc/hosts } open my $f, /dev/null or warn $!; stat $f or warn $!; print size: , -s _, \n; In the last line sub _ is invoked and the script prints the size of /etc/hosts instead of 0. However -X *_ seems to get this right. Should we mention this in perlfunc? I don't think so, since this behaviour is the same with any other identifier.
Re: _ handle and sub _
When a subroutine _ is defined it may mask the _ handle used by file test operators. There are a number of problems with using '_' as a subroutine name; this is one of the simplest and most predictable. '__' is apparently a much safer choice. See http://hop.perl.plover.com/errata/byid.cgi/131 for a discussion of some of the additional problems with '_'. +If a function named C_ is defined, the file tests will operate on its +return value instead of the saved stat structure. Replace C-X _ with +C-X *_ to circumvent this behaviour. This strikes me as one of those additions to the manual that is well-meaning but not worth the space it requires. The problem it is designed to addres is uncommon, and the fraction of people who encounter that problem who actually look in the right part of the manual, find the new remark, and recognize its significance will be vanishingly small.
Re: _ handle and sub _
Also: http://hop.perl.plover.com/~alias/list.cgi?2:mss:264