[EMAIL PROTECTED] wrote:
Looking for some help here you perl geniuses you : )

I need a program that will list all of the files in a directory.
Without any arguments the program will list only the files (not
directories) in the current directory.  But I must have some command
line options:

-d <with 0 or 1 directory argument> if no argument is given, the cwd
is used.  If the argument given is not a directory an error is thrown.
The output needs to have the following headers:
File Name | Size | Owner | Group ( with no pipes)

-l displays a long listing (can be used with -d as "script.pl -l -d
<dirname>")
The output needs to have the following headers:
File Name | Size | Owner | Group (with no pipes)

no parameters - show the files in the cwd
The output needs to have the following header:
File Name

Just off the top of my head, season to taste:

#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Std;

getopt 'hl', \my %opt;

exists $opt{ h } && die <<HELP;
usage: $0 [-h] [-l] [directory]
     -h    this message
     -l    long listing
HELP

my $dir    = shift || '.';
my $format = "%-25s %10s %-8s %-8s\n";

opendir my $dh, $dir or die "Cannot open '$dir' $!";

if ( exists $opt{ l } ) {
    printf $format, 'File Name', 'Size', 'Owner', 'Group'
    }
else {
    print "File Name\n"
    }

while ( my $file = readdir $dh ) {
my ( $uid, $gid, $size ) = ( lstat "$dir/$file" )[ 4, 5, 7 ] or die "Cannot stat '$dir' $!";
    next unless -f _;
    if ( exists $opt{ l } ) {
printf $format, $file, $size, scalar getpwuid $uid, scalar getgrgid $gid
        }
    else {
        print "$file\n"
        }
    }




John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to