Monks,

I have been reading some codes from perl cookbook and there is one listed below makes me puzzled.

#!/usr/bin/perl # lst - list sorted directory contents (depth first) use Getopt::Std; use File::Find; use File::stat; use User::pwent; use User::grent; getopts("lusrcmi") or die <<"DEATH"; Usage: $0 [-mucsril] [dirs ...] or $0 -i [-mucsrl] < filelist Input format: -i read pathnames from stdin Output format: -l long listing Sort on: -m use mtime (modify time) [DEFAULT] -u use atime (access time) -c use ctime (inode change time) -s use size for sorting Ordering: -r reverse sort NB: You may only use select one sorting option at a time. DEATH unless ($opt_i || @ARGV) { @ARGV = (".") } if ($opt_c + $opt_u + $opt_s + $opt_m > 1) { die "can only sort on one time or size"; } $IDX = "mtime"; $IDX = "atime" if $opt_u; $IDX = "ctime" if $opt_c; $IDX = "size" if $opt_s; $TIME_IDX = $opt_s ? "mtime" : $IDX; *name = *File::Find::name; # forcibly import that variable # the $opt_i flag tricks wanted into taking # its filenames from ARGV instead of being # called from find. if ($opt_i) { *name = *_; # $name now alias for $_ while (<>) { chomp; &wanted; } # ok, not stdin really } else { find(\&wanted, @ARGV); } # sort the files by their cached times, youngest first @skeys = sort { $time{$b} <=> $time{$a} } keys %time; # but flip the order if -r was supplied on command line @skeys = reverse @skeys if $opt_r; for (@skeys) { unless ($opt_l) { # emulate ls -l, except for permissions print "$_\n"; next; } $now = localtime $stat{$_}->$TIME_IDX( ); printf "%6d %04o %6d %8s %8s %8d %s %s\n", $stat{$_}->ino( ), $stat{$_}->mode( ) & 07777, $stat{$_}->nlink( ), user($stat{$_}->uid( )), group($stat{$_}->gid( )), $stat{$_}->size( ), $now, $_; } # get stat info on the file, saving the desired # sort criterion (mtime, atime, ctime, or size) # in the %time hash indexed by filename. # if they want a long list, we have to save the # entire stat object in %stat. yes, this is a # hash of objects sub wanted { my $sb = stat($_); # XXX: should be stat or lstat? return unless $sb; $time{$name} = $sb->$IDX( ); # indirect method call $stat{$name} = $sb if $opt_l; } # cache user number to name conversions; don't worry # about the apparently extra call, as the system caches the # last one called all by itself sub user { my $uid = shift; $user{$uid} = getpwuid($uid) ? getpwuid($uid)->name : "#$uid" unless defined $user{$uid}; return $user{$uid}; } # cache group number to name conversions; ditto on unworryness sub group { my $gid = shift; $group{$gid} = getgrgid($gid) ? getgrgid($gid)->name : "#$gid" unless defined $group{$gid}; return $group{$gid}; }

There is a hash, %time. But it seems coming from nowhere.

Could anyone have some insight into this? Thanks


In reply to where does this "%time" come frome? by lightoverhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.