Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Programs related to control structures 1.

If-else
use strict; my $answer='yes'; if( $answer eq 'yes' ) { print " the answer is yes\n"; } else { print "the answer is no\n"; }

2. If-elsif-else
$x = 5; if ($x !) { print "" is positi#e inte$er%\n"; } elsif ($x & !) { print "" is a ne$ati#e inte$er%\n"; } else { print "" is equal to 'ero\n"; }

3. While loop( with special statements like redo,next continue


$count = !; while ($count &= () { if ($count == )) { print "*+ip ,our-\n"; next; } . /0123 345 6782350 print $count%"\n"; } continue { $count99; . . if($count==:) { print "stoppin$ the loop imme;iately\n";

. .

re;o; } };

print "<oop ,inishe;-\n";

!. While loop(arra"s
.-=usr=>in=perl print "content?type@ text=html \n\n"; Anames = qw(*te#e Bill 6onnor Bra;ley); $n = !; while ($namesC$nD) { print "$namesC$nD\n"; $n99; }

#. $or each loop


.-=usr=>in=perl print "content?type@ text=html \n\n"; Anames = qw(*te#e Bill 6onnor Bra;ley); .$n = !; foreach$names(Anames) { print "$names\n"; }

Programs related to %ash &aria'les in perl 1. %ash &aria'le


my Eperson = ( name = "Foe"G a$e = HIG phone = "555?JKJK"G a;;ress = "JKH Lain *t%"G city = "6hica$o"G state = "1<"G 'ip = ":!:!J"); print "3he name is $person{name}\n\n"; . to print the sin$le +ey an; its #alue my A+ = +eys(Eperson); .to print all the +eys(>ut not #alues) print "3he unsorte; +eys are@ A+\n\n"; my As+ = sort(A+); . or@ As+ = sort(+eys(Eperson)); .to sort the +eys

print "3he sorte; +eys are@ As+\n\n";

Programs related to function in perl 1. (imple function


su> sayMhello { print "helloG worl;-\n"; } sayMhello(); .By con#entionGfunctions are ;efine; a>o#e the point where first calle; >ut can >e ;efine; after it% .sayMhello(); .su> sayMhello { . print "helloG worl;\n"; .}

2. )eturning &alues in functions


su> testJ { $a = J!; $> = JJ; . return the sum $a 9 $>; } su> testK { Aa = ('testin$'G 'one'G 'two'G 'three'); . return the sort of Aa sort(Aa); } $c = testJ(); print "\$c = $c\n"; A> = testK(); print "\A> = A>\n";

3. *ocal and glo'al &aria'le

su> printMmyMi { my $i = J!; print "in printMmyMi()@ $i\n"; } my $i = K!; print "outsi;e printMmyMi()@ $i\n"; printMmyMi(); print "outsi;e printMmyMi()@ $i\n";

!. +rguments in functions
su> stu;ent{ foreach $names(Anames){ print "the names of stu;ents are@Anames\n"; } } Anames=qw(NGBG6GO); stu;ent($names);

Programs related to file handling in perl 1. )eading from a file ( reading each line from a file and print that data to the terminal
.-usr=>in=perl if (- open (,4G "&test%txt")) { ;ie "6an't open test%txt@ $-"; .open(,4G P&test%txtP) or ;ie "6an't open test%txt@ $-";

} else { while ($line = &,4 ) { print "<ine is@ $line";

} } close (,4);

2. Writing to a file ( taking input from user from the terminal and write that data to file
.- =usr=>in=perl use strict; my $line; open (,4G " output%txt") or ;ie "6an't open output%txt@ $-"; while ($line = &*3O12 ) { print ,4 "Qou entere;@ $line"; } close (,4);

You might also like