|
|
PERL
Tutorial
Calculating
the factorial of a number which is taken from the keyboard input This
sample program uses subroutine, diamond , arithmetic operators and
while loop and if constructs
|
|
#factorial
$input=<>;
sub factorial
{
$s=1;
$r=1;
while ($s <= $input)
{
$r *= $s;
$s++;
}
if($input == 0)
{
$r=0;
}
return $r;
}
print "The result is".$r;
|
Example
7
Scoping Global and lexical scopes
The lexical scope variable is declared using my keyword.
By default the variable is global
The $scalar variable in the below example holds "hello" outside the
block { }
whereas it takes "world" inside the block.
#scoping
$scalar="hello";
print $scalar;
{
my $scalar="world";
print $scalar;
}
print $scalar;
Example
8
Using the strict module , all the variables have to be compusorily
declared
before use. Lexical variables are declared using my keyword while
global variables
are declared using our keyword.
the use keyword is used to include perl modules into the script
(similar to #include in
C programming language)
#strict
use strict;
our $var1="global";
print $var1;
{
my $var2="local";
print $var2;
}
print $var2;
Go To Index
|
|
|