What are the semantics of 'stat' on a dirhandle in Perl?

On researching another question I noted that the stat function in Perl can take a dirhandle as its argument (instead of a filehandle or filename).

However I can't find any examples of correct use of this - there are none in the Perl manual.

Can anyone show an example of how to use it?

Answers


You use it in the same way you do for a file or filehandle:

#!/usr/bin/perl
use strict;

my $dir = shift;
opendir(DIR, $dir) or die "Failed to open $dir: $!\n";
my @stats = stat DIR;
closedir(DIR);
my $atime = scalar localtime $stats[8];

print "Last access time on $dir: $atime\n";

The ability to use stat on directory handles was just added around Perl 5.10 so it should be avoided if you care about portability.


Need Your Help

How to print Java Marker Annotations?

java annotations

I have just begun reading into Java annotations. I need to print out all the three annotations written above the checkprime method but it is only printing the first.Seems like a stupid question to ...

What would be the simplest way to deal with a text file using JSP?

java unix jsp shell ksh

There is an actual running Java ServerPages (JSP) application within a *NIX box which I somewhat administer with kind of good permissions. The idea is to create a new but dead simple JSP page to co...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.