mirror of
https://github.com/Hizenberg469/Makefile-tutorial.git
synced 2026-04-19 13:52:24 +03:00
Subroutine is done
This commit is contained in:
314
perl/wiki
314
perl/wiki
@@ -493,3 +493,317 @@
|
||||
}
|
||||
|
||||
|
||||
-> while loop:
|
||||
|
||||
print "==========Loop in Perl===========\n";
|
||||
$a = 10;
|
||||
# while loop execution & continue statement.
|
||||
while ( $a < 15 ){
|
||||
printf "Value of a: $a\n";
|
||||
$a = $a + 1;
|
||||
} continue {
|
||||
$a = $a + 1;
|
||||
}
|
||||
|
||||
print "while-loop ended here\n";
|
||||
|
||||
Output:
|
||||
==========Loops in Perl=============
|
||||
Value of a: 10
|
||||
Value of a: 12
|
||||
Value of a: 14
|
||||
while-loop ended here
|
||||
|
||||
|
||||
-> do - while loop:
|
||||
|
||||
For ex:
|
||||
|
||||
#do...while loop execution
|
||||
do{
|
||||
printf "Value of a: $a\n";
|
||||
$a = $a + 1;
|
||||
}while( $a < 10 );
|
||||
print "do while-loop ended here\n"
|
||||
|
||||
Output:
|
||||
Value of a:
|
||||
Value of a: 1
|
||||
Value of a: 2
|
||||
Value of a: 3
|
||||
Value of a: 4
|
||||
Value of a: 5
|
||||
Value of a: 6
|
||||
Value of a: 7
|
||||
Value of a: 8
|
||||
Value of a: 9
|
||||
do while-loop ended here
|
||||
|
||||
-> until loop:
|
||||
|
||||
For ex:
|
||||
|
||||
#until loop execution
|
||||
#negated while loop...
|
||||
until( $a > 17 ){
|
||||
printf "Value of a: $a\n"
|
||||
$a = $a + 1;
|
||||
}
|
||||
printf "until-loop ended here\n";
|
||||
|
||||
Output:
|
||||
Value of a:
|
||||
Value of a: 1
|
||||
Value of a: 2
|
||||
Value of a: 3
|
||||
Value of a: 4
|
||||
Value of a: 5
|
||||
Value of a: 6
|
||||
Value of a: 7
|
||||
Value of a: 8
|
||||
Value of a: 9
|
||||
Value of a: 10
|
||||
Value of a: 11
|
||||
Value of a: 12
|
||||
Value of a: 13
|
||||
Value of a: 14
|
||||
Value of a: 15
|
||||
Value of a: 16
|
||||
Value of a: 17
|
||||
until-loop ended here
|
||||
|
||||
|
||||
-> for loop:
|
||||
|
||||
# for loop execution
|
||||
for( $a = 10 ; $a < 20 ; $a = $a + 3){
|
||||
print "value of a: $a\n"
|
||||
}
|
||||
print "for-loop ended here\n"
|
||||
|
||||
Output:
|
||||
value of a: 10
|
||||
value of a: 13
|
||||
value of a: 16
|
||||
value of a: 19
|
||||
for-loop ended here
|
||||
|
||||
-> foreach loop:
|
||||
|
||||
# foreach loop execution
|
||||
@list = (200, 300, 400, 500);
|
||||
foreach $a (@list){
|
||||
print "value of a: $a\n"
|
||||
}
|
||||
print "foreach-loop ended here\n"
|
||||
|
||||
Output:
|
||||
value of a: 200
|
||||
value of a: 300
|
||||
value of a: 400
|
||||
value of a: 500
|
||||
|
||||
-> next & redo ( next without label ):
|
||||
|
||||
* next: It's like a goto statement in C/C++
|
||||
* redo: It's like a continue statement in C/C++
|
||||
* last: It's like a break statement in C/C++
|
||||
|
||||
For ex:
|
||||
#next and redo statements in loops
|
||||
|
||||
print "next statement example without label\n";
|
||||
$a = 13;
|
||||
while( $a < 18 ){
|
||||
$a = $a + 1;
|
||||
if( $a == 15 ){
|
||||
#skip the iteration
|
||||
$a = $a + 1;
|
||||
|
||||
#redo;
|
||||
next;
|
||||
#last: this keyword is like break statement in C/C++
|
||||
}
|
||||
print "value of a: $a\n";
|
||||
#$a = $a + 1;
|
||||
}
|
||||
|
||||
Output:
|
||||
next statement example without label
|
||||
value of a: 14
|
||||
value of a: 17
|
||||
value of a: 18
|
||||
|
||||
-> next with labels:
|
||||
|
||||
For ex:
|
||||
print "\nnext statement example with label\n";
|
||||
$a = 0;
|
||||
OUTER: while( $a < 4 ){
|
||||
$b = 0;
|
||||
print "value of a : $a\n";
|
||||
INNER: while ( $b < 4 ){
|
||||
if( $a == 2 ){
|
||||
$a = $a + 1;
|
||||
#jump to outer loop
|
||||
next OUTER;
|
||||
#last OUTER; #in case of last it will throw out of all nested loop
|
||||
}
|
||||
$b = $b + 1;
|
||||
print "Value of b : $b\n"
|
||||
}
|
||||
|
||||
print "\n";
|
||||
$a = $a + 1;
|
||||
}
|
||||
|
||||
Output:
|
||||
next statement example with label
|
||||
value of a: 0
|
||||
value of b: 1
|
||||
value of b: 2
|
||||
value of b: 3
|
||||
value of b: 4
|
||||
|
||||
value of a: 1
|
||||
value of b: 1
|
||||
value of b: 2
|
||||
value of b: 3
|
||||
value of b: 4
|
||||
|
||||
value of a: 2
|
||||
value of a: 3
|
||||
value of b: 1
|
||||
value of b: 2
|
||||
value of b: 3
|
||||
value of b: 4
|
||||
|
||||
|
||||
-> Function in perl:
|
||||
|
||||
For ex:
|
||||
|
||||
print "\n==========Subroutines in Perl=======\n\n";
|
||||
|
||||
print "==== Passing scalars and arrays & returning result ====\n";
|
||||
sub printSum{
|
||||
print "In Average's subroutine: \$n = $n\n";
|
||||
print "In Average's subroutine: \$sum = $sum\n";
|
||||
print "In Average's subroutine: \$average = $average\n";
|
||||
}
|
||||
|
||||
|
||||
sub Average{
|
||||
#get total number of arguments passed.
|
||||
$n = scaler(@_);
|
||||
|
||||
# If array is passed as an argument to subroutine
|
||||
# use @_ or if scalar is passed as an argument use
|
||||
# $_.
|
||||
|
||||
#Scope of variables:
|
||||
#this will be available to Average's subroutines
|
||||
local $sum = 0; # Any function called from Average
|
||||
# subroutine can access $sum variable
|
||||
|
||||
foreach $item (@_){
|
||||
$sum += $item;
|
||||
}
|
||||
printSum();
|
||||
|
||||
##private variable
|
||||
my $average = $sum / $n;
|
||||
return $average;
|
||||
}
|
||||
|
||||
#Function call
|
||||
@a = (40, 50, 60);
|
||||
|
||||
$b = 70;
|
||||
$avg = Average(10, 20, 30, @a, $b);
|
||||
print "Average for the given numbers: $avg\n";
|
||||
print "\$sum = $sum\n";
|
||||
print "\$n = $n\n";
|
||||
print "\$average = $average\n";
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
====Subroutines in Perl====
|
||||
|
||||
==== Passing scalars and array & returning result ===
|
||||
In Average's subroutine: $n = 7
|
||||
In Average's subroutine: $sum = 280
|
||||
In Average's subroutine: $average =
|
||||
Average for the given number : 40
|
||||
$sum =
|
||||
$n = 7
|
||||
$avearge =
|
||||
|
||||
|
||||
|
||||
-> Passing hashes in subroutines:
|
||||
|
||||
For ex:
|
||||
|
||||
%cardG1CPUarch = ('csc01', 'mips', 'xcc360g', 'ppc', 'cef4c', 'ppc');
|
||||
|
||||
print "===== Passing hashes to subroutines ====\n";
|
||||
#Function definition
|
||||
sub PrintHash{
|
||||
my (%hash) = @_;
|
||||
|
||||
foreach my $key ( keys %hash ){
|
||||
my $value = $hash{$key};
|
||||
print "$key : $value\n";
|
||||
}
|
||||
}
|
||||
|
||||
#Function call with hash parameter
|
||||
PrintHash(%cardG1CPUarch);
|
||||
|
||||
Output:
|
||||
|
||||
====== Passing hashes to subroutines ======
|
||||
cef4c: ppc
|
||||
xcc360g: ppc
|
||||
csc01: mips
|
||||
|
||||
|
||||
-> References in Perl:
|
||||
|
||||
For ex:
|
||||
print "\n==== References in Perl =====\n\n";
|
||||
|
||||
$var = 10;
|
||||
# Now $r has reference to $var scalar.
|
||||
$r = \$var;
|
||||
# Print value available at the location stored in $r.
|
||||
print "Type of reference is :" . ref($r) . "\n";
|
||||
print "Value of $var is: " , $$r, "\n";
|
||||
|
||||
@var = (1,2,3);
|
||||
#Now $r has reference to @var array.
|
||||
$r = \@var;
|
||||
#Print values available at the location stored in $r.
|
||||
print "Type of reference is :" . ref($r) . "\n";
|
||||
print "Value of @var is :" . @$r, "\n";
|
||||
|
||||
%var = ('key1' => 10, 'key2' => 20);
|
||||
# Now $r has reference to $var has.
|
||||
$r = \%var;
|
||||
# Print values available at the location stored in $r.
|
||||
print "Type of reference is :" . ref($r) . "\n";
|
||||
print "Value of %var is : " , $r, "\n";
|
||||
print "Value of %var is : " , %$r{'key1'}, "\n";
|
||||
|
||||
Output:
|
||||
====== Refernce in Perl ======
|
||||
|
||||
Type of reference is : SCALAR
|
||||
Value of 10 is : 10
|
||||
Type of reference is : ARRAY
|
||||
Value of 1 2 3 is : 123
|
||||
Type of reference is : HASH
|
||||
Value of %var is : key220key110
|
||||
Value of %var is : key110
|
||||
Reference in New Issue
Block a user