hmm this misterious { _ } operand ?!? Is it sometype of a bitflag in the
stat code it self, perhaps some magical referance to return structure.
Anyhow here is a bit of code that utilizes it, run it in a directory with
about 10 files,

HTH, Mark

<~~~ CuT

use Benchmark::Timer;
use Fcntl ':mode';

$with_='for(0...10000){foreach (glob "*"){print "$_ => Text\n" if -f ;print
"$_ => Directory\n" if -d _;print "$_ => Charachter\n" if -c _;}}';
$without_='for(0...10000){foreach (glob "*"){print "$_ => Text\n" if -f
;print "$_ => Directory\n" if -d ;print "$_ => Charachter\n" if -c ;}}';

$t = Benchmark::Timer->new(slip => 1);

eval $with_;

# using _ with file tests
$t->start('10000 times using _');
eval $with_;
$t->stop('10000 times using _');
$t->report;


# using stat on each test
$t->start('10000 times using stand');
eval $without_;
$t->stop('10000 times using stand');
$t->report;

<~~ Paste
----- Original Message ----- 
From: "Rob Dixon" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, June 10, 2003 12:34 PM
Subject: Re: typing files quickly


> Harry Putnam wrote:
> > Steve Grazzini <[EMAIL PROTECTED]> writes:
> >
> > > Harry Putnam <[EMAIL PROTECTED]> wrote:
> > > > "Janek Schleicher" <[EMAIL PROTECTED]> writes:
> > > > >
> > > > > The underscore _ holds the results of the last stat call
> > > > > (implicitly called by the -f operator), so no unnecessary
> > > > > work needs to be done.
> > > >
> > > > I've seen that `_' crop up before
> > > > I don't understand what this means.
> > >
> > > It's documented in perlfunc:
> >
> > Yes, I saw it there too.  I must be having a particularly dense time
> > of it, but I still am missing what is actually in _
>
> There is nothing 'in' the underscore. What happens is that if
> you use the -X operators with '_' as the parameter, then instead
> of calling 'stat' to fetch the file's details the results of the
> last call to 'stat' are used directly. Where and how these are
> saved you don't need to know.
>
> > >     $ perldoc -f stat
> > >         [ snip ]
> > >
> > >         If stat is passed the special filehandle
> > >         consisting of an underline, no stat is done, but
> > >         the current contents of the stat structure from
> >
> > The previous content of the stat structure is what? (in plain english)
> > Does it mean the previous values of the 13 elements produced by stat?
>
> Roughly. But the 13 elements are values that are set up to be handed
> back neatly to the Perl calling code. The information may be kept in this
> format or some other, but it doesn't matter to you.
>
> > Or something else...
> >
> >      if (-e $file and -f _) { ...
> >
> > What is -f being tested against.  If not $file then what...
>
> This is a particularly bad example, because -f will return
> 'true' only if the file actually exists anyway. But what
> this does is
>
>   - call stat($file)
>   - use the results to see if the file exists
>   - use the results to see if it's a regular file
>   - 'and' the two together and execute the conditional code accordingly
>
> > The
> >  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
> >    $atime,$mtime,$ctime,$blksize,$blocks)
> > Resulting from the -e test?
>
> Sort of. But this is the list returned from 'stat' to
> Perl calling code. For -X operators 'stat' is called
> implicitly and has to remember only enough information
> to be able to derive that list, or the result of any
> subsequent '-X _' call.
>
> > Or getting back to original question...
> > perl -e '$mode = (stat($ARGV[0]))[2];print "$mode\n";' test.f
> > 33204
> >
> > What is it about 33204 that tells us `type' is a regular file
> > The first digit being 3?
>
> No.The fact that (33204 & S_IFMT) == S_IFREG
>
> >  perl -e '$mode = (stat($ARGV[0]))[2];print "$mode\n";' test.d
> > 16893
> >   "ditto  regular directory"
> > The first digit being 1?
>
> No. The fact that (16893 & S_IFMT) == S_IFDIR
>
> > That is, might one just look for a static part of $mode to know the
> > type?
>
> Yes. $mode & S_IFMT
>
> > perldoc -f stat seems to indicate it might?
> >
> > [...]
> >             0 dev      device number of filesystem
> >             1 ino      inode number
> >             2 mode     file mode  (type and permissions)
> > [...]
> >
> > And a few tests with this:
> > #!/usr/local/bin/perl -w
> >
> > for(@ARGV){
> >    chomp;
> >   $mode = (lstat($_))[2];
> >   if ( $mode =~ /^3/){
> >     print  "$mode $_: Regular file\n";
> >   }elsif($mode =~ /^1/){
> >     print "$mode $_: Directory\n";
> >   }elsif($mode =~ /^41/){
> >     print "$mode $_: Symlink\n";
> >   }
> > }
> >
> > Seem to indicate it will work.
>
> No. I wrote the code to derive the file type from an 'lstat'
> call in a previous post and thought you were happy with that.
> Exactly how the filing system does it is something to trust
> and forget about. Especially from within Perl, which will
> happily that the bits are somewhere that aren't for the sake
> of portability.
>
> Rob
>
>
>
>
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to