Programs Related To Control Structures 1. If-Else
Programs Related To Control Structures 1. If-Else
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"; }
. .
re;o; } };
!. 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; }
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@ $-";
} } 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);