๐Ÿ“œ
B.TECH III YEAR โ€ข II SEMESTER โ€ข R22

Scripting Languages - Lab Manual

Scripting Languages Laboratory โ€ข Program 20 Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ III Year ๐Ÿ“š II Semester ๐Ÿ“œ Scripting Languages ๐Ÿ’Ž Ruby โ€ข TCL โ€ข Perl ๐Ÿงช Lab Program
Aim of Program 20

Write a Perl script to print the file in reverse order using command line arguments.

โ† All Programs
Solution

Aim:

☛   Write a Perl script to print the file in reverse order using command line arguments

Solution :

INPUT FILE: "numbers.txt"


1
2
3
4
5


PROGRAM: (reverse.pl)

 
#!/usr/bin/perl
use strict;
use warnings;

# Check if filename is passed
if (@ARGV != 1) {
    die "Usage: perl reverse.pl <filename>\n";
}

my $file = $ARGV[0];

# Open the file
open(my $fh, "<", $file) or die "Cannot open file '$file': $!";

# Read all lines into an array
my @lines = <$fh>;
close($fh);

# Print in reverse order
print reverse @lines;


OUTPUT:


# perl reverse.pl numbers.txt
5 
4 
3 
2
1



โ† Back to Scripting Languages Lab Programs ๐Ÿ“š All Lab Manuals