#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use vars qw( $opt_s $opt_r $opt_n );
if( ! getopts('srn' ) )
{
die "Usage: filename.pl -srn filename\n";
#Example: perl myfile.pl -r test.txt
}
my @file = <>;
if( $opt_s )
{
@file = sort { $a cmp $b } @file;
}
if( $opt_r )
{
@file = reverse @file;
}
if( $opt_n )
{
@file = sort { $a <=> $b } @file;
}
####
$perl myfile.pl -rn test.txt
# Reverse the contents of test.txt and perform
# a numeric sort on it.
####
# Sample runs:
C:\perl>perl pg8.pl -r test.txt
third line.
second line.
first line.
C:\perl>
C:\perl>perl pg8.pl -n test.txt
2
33
33
44
48
55
889
990
C:\perl>
C:\perl>perl pg8.pl -c test.txt
Unknown option: c
Usage: filename.pl -srn filename
C:\perl>