0% found this document useful (0 votes)
28 views8 pages

Perl

The document contains various Perl scripts demonstrating different programming tasks, including finding the largest number among three inputs, printing multiplication tables, and manipulating arrays with functions like shift, unshift, and push. It also includes scripts for string substitution, validating email and IP addresses, and printing file contents in reverse order. Each script is accompanied by example outputs to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views8 pages

Perl

The document contains various Perl scripts demonstrating different programming tasks, including finding the largest number among three inputs, printing multiplication tables, and manipulating arrays with functions like shift, unshift, and push. It also includes scripts for string substitution, validating email and IP addresses, and printing file contents in reverse order. Each script is accompanied by example outputs to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

17. a) Write a Perl script to find the largest number among three numbers.

print "enter a value";


$x=<stdin>;
print "enter b value";
$y=<stdin>;
print "enter c value";
$z=<stdin>;
if($x > $y)
{
if($x > $z)
{
print " $x is largest number\n";
}
else
{
print " $z is largest number\n";
}
}
elsif($y > $z)
{
print " $y is largest number";
}
else
{
print " $z is largest nnumber";
}

Output

kits@kits-Veriton-M200-H310:~$ perl bi.pl


enter a value10
enter b value5
enter c value14
14
is largest number
kits@kits-Veriton-M200-H310:~$

b) Write a Perl script to print the multiplication tables from 1-10 using subroutines.
Program:

@array=(1..10);
$count=1;
while($count<11)
{
foreach $mul (@array)
{
$multiply=$count*$mul;
push(@multifinal,$multiply);
}
print"@multifinal\n";
@multifinal =() ;
$count++;
}

output:

kits@kits:~$ perl mul1.pl


1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
kits@kits:~$

18. Write a Perl program to implement the following list of manipulating functions
a) Shift

This function returns the first value in an array, removing it and shifting the elements of the
array list to the left by one. Shift operation removes the value like pop but is taken from the
start of the array instead of the end as in pop. This function returns undef if the array is
empty otherwise returns first element of the array.

# Initalizing the array

@x = ('Java', 'C', 'C++');

# Print the Inital array

print "Original array: @x \n";

# Prints the value returned

# by shift function

print "Value returned by shift: ",shift(@x);

# Array after shift operation

print "\nUpdated array: @x";

kits@kits-Veriton-M200-H310:~$ perl 183.pl

Original array: Java C C++


Value returned by shift: Java

Updated array: C C++kits@kits-Veriton-M200-H310:~$

b) Unshift

This function places the given list of elements at the beginning of an array. Thereby
shifting all the values in an array by right. Multiple values can be unshift using this
operation. This function returns the number of new elements in an array.

Syntax: unshift(Array, List)

# Initalizing the array

@x = ('Java', 'C', 'C++');

# Print the Inital array

print "Original array: @x \n";

# Prints the number of elements

# returned by unshift

print "No of elements returned by unshift: ", unshift(@x, 'PHP', 'JSP');

# Array after unshift operation

print "\nUpdated array: @x";

output :

kits@kits-Veriton-M200-H310:~$ perl 182.pl

Original array: Java C C++

No of elements returned by unshift: 5

Updated array: PHP JSP Java C C++

c) Push
# Initalizing the array
@x = ('Java', 'C', 'C++');
# Print the Inital array
print "Original array: @x \n";
# Pushing multiple values in the array
push(@x, 'Python', 'Perl');
# Printing the array
print "Updated array: @x";

output :
kits@kits-Veriton-M200-H310:~$ perl 181.pl
Original array: Java C C++
Updated array: Java C C++ Python Perl

19. a) Write a Perl script to substitute a word, with another word in a string.
use warnings;
use strict;

my $str = 'singa pur city';

$str =~ s/singa pur/Singa Poor/;

print $str, "\n";

kits@kits:~$ perl 19a.pl

output:

Singa Poor city

b) Write a Perl script to validate IP address and email address.


email validity programs
use strict;
use warnings;

use 5.010;
use Email::Address;

my $line = '[email protected] dnk24@rmailcom ' ;

my @addresses = Email::Address->parse($line);
foreach my $addr (@addresses) {
say $addr;
}
kits@kits:~$ perl eads.pl
output :

[email protected]
dnk24@rmailcom

use Email::Valid;
my $address = Email::Valid->address('[email protected]');
print ($address ? 'yes valid' : 'no');

nokits@kits:~$ perl ep1.pl

output
nokits@kits:~$ perl ep1.pl
yes valid

second prog using regular expression

use strict;
use warnings;
use Email::Valid;

my @emails = (
'[email protected]',
'dnk at rediff.com',
'[email protected]',
'[email protected]',
'[email protected]',

);

foreach my $email (@emails) {


my $address = Email::Valid->address($email);
my $regex = $email =~ /^[a-z0-9.]+\@[a-z0-9.-]+$/;
if ($address and not $regex) {
printf "%-20s Email::Valid but not regex valid\n", $email;
} elsif ($regex and not $address) {
printf "%-20s regex valid but not Email::Valid\n", $email;
} else {
printf "%-20s agreed\n", $email;
}
}

kits@kits:~$ perl es2.pl

output:

[email protected] agreed
dnk at rediff.com agreed
[email protected] agreed
[email protected] agreed
[email protected] agreed

Ip address validity using regular expressition

print("Enter the IP Address you would like to validate - ");


my $ip = <STDIN>;

if($ip =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
{
$ip = $1;
}

chomp($ip);
if($ip =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/)
{
print("\nIP address found - $ip\n");
if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255)
{
print("Each octet of an IP address is ",
"within the range - $1.$2.$3.$4\n");
print("\n-> $ip IP address accepted!\n");
}
else
{
print("Octet(s) out of range. ",
"Valid number range between 0-255\n");
}
}
else
{
print("IP Address $ip is not in a valid format\n");
}

kits@kits:~$ perl pip1.pl

output:

Enter the IP Address you would like to validate - 192.168.10.25

IP address found - 192.168.10.25


Each octet of an IP address is within the range - 192.168.10.25

-> 192.168.10.25 IP address accepted!


kits@kits:~$

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

create a s file with following content

1
a
2
b
3
run the bellow command

tail s | perl -e 'print reverse <>' > sout

output:
kits@kits:~$ cat sout
3
b
2
a
1

You might also like