An even better choice would be the oroperator introduced in Perl 5, which has such low precedence that you can leave out the parentheses on open: open FH, $file or die “Error opening $file: $!n”; 10.2 Reading Directories This has bitten us more times than we care to admit: readdir() returns the list of filenames in the directory, but they are not qualified by the directory itself. Consider the following program: use strict; use File::stat; my $dir = shift || ‘.’; opendir DIR, $dir or die “Error opening directory $dir: $!n”; while (defined (my $file = readdir DIR)) { print “File $file was last modified on ” . localtime(stat($file)->mtime), “n”; } closedir DIR; It takes a directory as its argument, defaulting to the current directory. This program prints out the names and modification times of all the files in the current directory. If run with no arguments, the program runs fine. If we then run it with an argument (this example is on Unix), it appears to start working and then fails: File . was last modified on Sun Mar 28 14:36:27 1999 File .. was last modified on Tue Aug 3 15:29:12 1999 Can’t call method “mtime” without a package or object reference at mod_times.pl line 9. What happened here? First, we see the error refers to line 9 and that perl attempted to invoke the mtimemethod on something that wasn’t even an object. What was the object? It was the result of the call to stat($file) (we’re using the stat routine from the File::stat module here, instead of the core perl stat routine that returns a list; it saves us from having to look up the index of the mtimeelement).
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services
January 31st, 2006
This entry was posted
on Tuesday, January 31st, 2006 at 7:32 am and is filed under perl.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.