Click here to get back home

bare underscore?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
bare underscore? Michael Slass 03-26-2008
Posted by Michael Slass on March 26, 2008, 7:54 pm
Please log in for more thread options
I was looking at the standard module lib.pm, and saw this puzzling (to
me) bit:

foreach (reverse @_) {
        my $path = $_;                # we'll be modifying it, so break the alias
        if ($path eq '') {
         require Carp;
         Carp::carp("Empty compile time value given to use lib");
        }

        $path = _nativize($path);

        if (-e $path && ! -d _) {
         require Carp;
         Carp::carp("Parameter to use lib must be directory, not file");
        }



What does the _ with no sigil before it mean in
        if (-e $path && ! -d _) {

?

Thanks.


--
Mike Slass

Posted by John W. Krahn on March 26, 2008, 8:18 pm
Please log in for more thread options
Michael Slass wrote:
> I was looking at the standard module lib.pm, and saw this puzzling (to
> me) bit:
>=20
> foreach (reverse @_) {
>         my $path =3D $_;                # we'll be modifying it, so break the alias
>         if ($path eq '') {
>          require Carp;
>          Carp::carp("Empty compile time value given to use lib");
>         }
>=20
>         $path =3D _nativize($path);
>=20
>         if (-e $path && ! -d _) {
>          require Carp;
>          Carp::carp("Parameter to use lib must be directory, not file");
>         }
>=20
> What does the _ with no sigil before it mean in
>         if (-e $path && ! -d _) {

perldoc -f -d
[ SNIP ]

If any of the file tests (or either the "stat" or "lstat"
operators) are given the special filehandle consisting of a
solitary underline, then the stat structure of the previous file=

test (or stat operator) is used, saving a system call. (This
doesn=92t work with "-t", and you need to remember that lstat()
and "-l" will leave values in the stat structure for the
symbolic link, not the real file.) (Also, if the stat buffer
was filled by an "lstat" call, "-T" and "-B" will reset it with
the results of "stat _"). Example:

print "Can do.\n" if -r $a || -w _ || -x _;

stat($filename);
print "Readable\n" if -r _;
print "Writable\n" if -w _;
print "Executable\n" if -x _;
print "Setuid\n" if -u _;
print "Setgid\n" if -g _;
print "Sticky\n" if -k _;
print "Text\n" if -T _;
print "Binary\n" if -B _;



John
--=20
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Michael Slass on March 27, 2008, 12:42 pm
Please log in for more thread options



>
>> What does the _ with no sigil before it mean in
>>         if (-e $path && ! -d _) {
>>
>> ?
>
>When used with a file test operator, the unadorned _ re-uses the information
>returned from the preceding test or stat() call. This can avoid race condi-
>tions that might cause stat() to return different results from one test to
>the next. It can also be a big win where stat() is relatively expensive,
>such as network shares.
>

Thanks to both of you; I was looking in the perlvar manpage for a
magic variable; I hadn't guessed it was magic in the -d operator.

Thanks again,


--
Mike Slass

Similar ThreadsPosted
using bare perl to run Unix passwd February 4, 2008, 10:42 pm
using underscore in package October 18, 2005, 2:13 pm
what does underscore stands for ? September 11, 2006, 2:37 am
Regular expression for matching words containing underscore _ character December 12, 2007, 10:27 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap