
PERL PROGRAMS
1. Sample PERL PROGRAM
#!usr/bin/perl #
the above line is shebang
directive
$name=<STDIN>; chomp($name); print
"$name\n";
2. Write a perl program for Scalar
variables in PERL
#!/usr/bin/perl
my $animal="Camel"; # this variable is lexically scoped ie local my $ans=42_243; #this is similar
to 42243
print "$animal\n";
print "$ans\n";
print "The square of &ans",$ans*$ans,"\n";
3. Write a perl program for Array
in
PERL
#!usr/bin/perl
my @animal=("cow","Buffalo","Camel"); print "@animal\n"; # list all
elements in array print "$#animal\n"; # list last element position print "$animal[0]\n"; #list 0th position
element
print "$count"; #
count no of elements in array
4. Write a perl program for Hash in PERL
#!usr/bin/perl
%color=('apple'=>"red",'banana'=>"yellow",'orange'=>"orange");
print "$color{'apple'}\n";
5. Write a perl program for To display all values in hash
#!usr/bin/perl
%color=('apple'=>"red",'banana'=>"yellow",'orange'=>"orange");
print "$color{'apple'}\n";
# to display
all the values in hash
foreach $key (@keys)
{
print "$color{$key}\n";
}
6.Write a perl program for If loop in perl
#!usr/bin/perl my $a=10;
$condition=1;
if($condition)
{
my $y=100; print "$a\n"; print
"$y\n";
}
print "$a\n";
print "$y\n";
7. Write a perl program for If
not loop
(unless)in perl
#!usr/bin/perl
$a=5;
unless($a>10)
#is equal to if not
{
print "a less than 10";
}
8. Write a perl program for While loop
(until) in
perl
#!usr/bin/perl
$a=0;
until($a>10)
#is equal to while
{
print "$a\n";
$a++;
}
9. Write a perl program for for
each loop (upper limit is not
fixed )in perl
#!usr/bin/perl
my @animals=("cow","buffalo","camel",123,100,243,300);
foreach $key(@animals)
{
print "$key\n";
}
10. Write a perl program for String operation:
#!usr/bin/perl
$a="hello";
$b="world";
print $a.$b,"\n";
$str="-";
print $str
x 80,"/n";
print "@a\n";
11. Write a perl program for Magic
variable in perl
#!usr/bin/perl
%color=('apple'=>"red",'banana'=>"yellow",'orange'=>"orange");
$h=\%color;
print "$h\n";#print the reference address
{
print "$h->($_}\n"; # to display all
key values we use $_ instead of
keys
}
12 .Write a perl program for Function
#!usr/bin/perl sub sayHello()
{
print "Hello\n";
}
&sayHello();
13.Write a perl program for Function with
parameter
#!usr/bin/perl sub add()
{
my ($a,$b)=@_;
#the values will be stored
in @_ array variable for dynamically
print $a+$b;
}
&add(10,15);
14.Write a perl program for program to
check greater among 3 number
#Greatest among 3 number print "Enter A value : ";
$a=<>;
print "Enter b value : ";
$b=<>;
print "Enter c value : ";
$c=<>;
if(($a >
$b)&&($a > $c))
{
print "A is greater";
}
elsif(($b > $c)&&($b > $a))
{
print "B is greater";
}
else
{
print "C is greater";
}
15.Write a perl program for Program to
get details in
run time
#! usr/bin/perl
print "Enter the Student
Information";
print "\n enter name"; chomp($name = <>);
print
"\nenter dob";
chomp($dob=<>);
print "\n enter registerno.";
chomp($registern0.=<>); print "\n enter
education"; chomp($Education=<>);
print "\n Student Information";
print "\n Name: $name";
print "\n Date Of Birth
:$dob";
print "\n Register no.:$registern0.";
print "\nQualification :$Education";
simple perl program | sample perl program | perl program download | example perl program |execute perl program | perl script | perl builder | perl beginners | perl software | perl program download | perl script | perl program windows | perl tutorial
0 comments:
Post a Comment