title: Perl directories, file sort, print list with size and date
body: <p>My task is:</p>
  
  <ol>
  <li>Read the directory, type of sorting, and order of sorting from command line.</li>
  <li>Sort the file names and print them out with size and date. </li>
  </ol>
  
  <p>Here is what I got so far.</p>
  
  <pre><code>#!/usr/bin/perl
  
  use 5.010;
  use strict;
  use warnings;
  
  use Getopt::Long;
  my $dir = "";
  my $sortby = "";
  my $order = "";
  my $result;
  
  $result = GetOptions (
                      'dir=s'  =&gt; \$dir,                # specify derictory
                     'sortby=s'    =&gt;  \$sortby,        # 'name' or 'date'
                     'order=s'     =&gt;  \$order);        # 'asc'- or 'des'-ending order of sorting
  
  print "derictory = $dir, sortby  = $sortby, order = $order \n\n";
  
  
  opendir (DH, $dir)or die "couldn open dericroty: $!\n";
  my @filenames = grep ! /^\./, readdir DH;
  closedir (DH);
  
  if ($sortby eq "name") {
  
       if ($order eq "asc") {
           foreach my $name (sort {lc $a cmp lc $b} @filenames) {
           my @statinfo = stat("$dir/$name");
           print "$name\tsize= " . $statinfo[7] . ",\t last modified=" . 
           scalar(localtime($statinfo[9])) . "\n";
           }
       }
       elsif ($order eq "des") {
           foreach my $name (sort {lc $b cmp lc $a} @filenames) {
           my @statinfo = stat("$dir/$name");
           print "$name\tsize= " . $statinfo[7] . ",\t last modified=" . 
           scalar(localtime($statinfo[9])) . "\n";
           }
       }      
  }
  
  
  if ($sortby eq "date") {
      if ($order eq "asc") {
          @filenames = sort { -M "$dir/$a" &lt;=&gt; -M "$dir/$b" } (@filenames);   
          print  join ("\n", @filenames);
      }    
      elsif ($order eq "des") {
          @filenames = sort { -M "$dir/$b" &lt;=&gt; -M "$dir/$a" } (@filenames);   
          print  join ("\n", @filenames);
      }    
  }
  </code></pre>
  
  <p>The problem is if I need to sort it by date modified, I don't know how to print out the list of the file names with the size and date. I guess I am supposed to use the stat function, but I can't loop through names, and get each stat.<br>
  All I have above is basically what I was able to google and put together. </p>