> -----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.

Reply via email to