Pages

Sunday, May 12, 2013

Perl practicals - Lab 1


Learning perl by practicing 

Step:1 Find Perl Interpreter

hippo-MacBook-Pro:~ hippo$ which perl
/usr/bin/perl

Step:2.a Implicit Execution



Every script starts with shebang:"#!" which is not read as a comment. First line is also a place where you put your interpreter which in this case is perl.

#!/usr/bin/perl print "Perl Programming\n";

Make Perl Script Executable: chmod +x perl_script.pl

hippo-MacBook-Pro:~ hippo$ chmod +x perl_script.pl
hippo-MacBook-Pro:~ hippo$ ./perl_script.pl
Perl Programming

Step:2.b Explicit Execution

print "Perl Programming\n";

Make Perl Script Executable: chmod +x perl_script.pl

hippo-MacBook-Pro:~ hippo$ chmod +x perl_script.pl
hippo-MacBook-Pro:~ hippo$ perl perl_script.pl
Perl Programming

Step:3 Simple Perl script

#!/usr/bin/perl # print "Perl Programming Tutorial\n";

hippo-MacBook-Pro:~ hippo$ ./simple_perl_script.pl
Perl Programming Tutorial

Step:4 Current path to Perl modules

List all available current paths to perl modules:

perl -e 'print "@INC" . "\n";'

hippo-MacBook-Pro:~ hippo$ perl -e 'print "@INC" . "\n";'
/Library/Perl/5.12/darwin-thread-multi-2level /Library/Perl/5.12 /Network/Library/Perl/5.12/darwin-thread-multi-2level /Network/Library/Perl/5.12 /Library/Perl/Updates/5.12.4 /System/Library/Perl/5.12/darwin-thread-multi-2level /System/Library/Perl/5.12 /System/Library/Perl/Extras/5.12/darwin-thread-multi-2level /System/Library/Perl/Extras/5.12

Step:5 Variables

$ - Scalar Variable
% - Hash Variable
@ - Array
& - Subroutines

Step:6 Using Perl default variable $_

#!/usr/bin/perl
$_ = "Perl Programming default variable.\n";
print;

hippo-MacBook-Pro:~ hippo$ ./perl_default_variable.pl
Perl Programming default variable.

Step:7 Defined Function

#!/usr/bin/perl

# declare perl scalar do but not define value
$perl_scalar;
#we can use conditional operator '?:' to test perl defined funtion
$variable = defined($perl_scalar) ? "Variable \$perl_scalar is Defined!"
 : "Variable \$perl_scalar is NOT Defined!";
print $variable."\n";

# declare perl scalar with value
$perl_scalar="perl";
$variable = defined($perl_scalar) ? "Variable \$perl_scalar is Defined!"
: "Variable \$perl_scalar is NOT Defined!";
print $variable."\n";

hippo-MacBook-Pro:~ hippo$ ./defined.pl
Variable $perl_scalar is NOT Defined!
Variable $perl_scalar is Defined!

Step:8 Scalar variable

#!/usr/bin/perl
#Scalars hold just single data type: string, number or perl reference
#Scalars definition in Perl
$scalar_number = -5;
$scalar_string1 = "In PERL Scalars are always referenced with \x24 in front of each variable name. ";
$scalar_string2 = "5 items";
#Undescore can be use for big numbers
$scalar_milion = 1_000_000;
#Print scalar values
print $scalar_number."\n";
print $scalar_string1."\n";
print $scalar_string2."\n";
print $scalar_milion."\n";
#perl scalar addition
print $scalar_number + $scalar_milion."\n";

hippo-MacBook-Pro:~ hippo$ ./perl_scalars.pl
-5
In PERL Scalars are always referenced with $ in front of each variable name.
5 items
1000000
999995

Step:9 Single-Quoted Strings

#!/usr/bin/perl

#Single-Quoted scalar strings
$scalar_string1='perl';
print "String 1: ".$scalar_string1."\n";
$scalar_string2='#!/usr/bin/perl';
print "String 2: ".$scalar_string2."\n";
$scalar_string3='Perl
Programming
Tutorial';
print "String 3: ".$scalar_string3."\n";
$scalar_string4='Perl\n';
print "String 4: ".$scalar_string4."\n";
$scalar_string5='\'\'\\';
print "String 5: ".$scalar_string5."\n";
$scalar_string6='';
print "String 6: ".$scalar_string6."\n";
$scalar_string7='I\'m reading Perl Programming Tutorial';
print "String 7: ".$scalar_string7."\n";

hippo-MacBook-Pro:~ hippo$ ./perl_default_variable.pl
String 1: perl
String 2: #!/usr/bin/perl
String 3: Perl
Programming
Tutorial
String 4: Perl\n
String 5: ''\
String 6:
String 7: I'm reading Perl Programming Tutorial

Step:10 Double-Quoted Strings

#!/usr/bin/perl
#Double-Quoted scalar strings
$scalar_string1="perl";
print "String 1: ".$scalar_string1."\n";
$scalar_string2="#!/usr/bin/perl";
print "String 2: ".$scalar_string2."\n";
$scalar_string3="Perl
Programming
Tutorial";
print "String 3: ".$scalar_string3."\n";
$scalar_string4="Perl\n";
print "String 4: ".$scalar_string4."\n";
$scalar_string5="\'\'\\\"";
print "String 5: ".$scalar_string5."\n";
$scalar_string6="";
print "String 6: ".$scalar_string6."\n";
# add "!" ASCII character in octal form !=041
$scalar_string7="I\'m reading Perl Programming Tutorial \041";
print "String 7: ".$scalar_string7."\n";
# add "@" ASCII character in hexadecimal form @=40

hippo-MacBook-Pro:~ hippo$ ./perl_strings.pl
String 1: perl
String 2: #!/usr/bin/perl
String 3: Perl
Programming
Tutorial
String 4: Perl

String 5: ''\"
String 6:
String 7: I'm reading Perl Programming Tutorial !

Step:11 String Operators

#!/usr/bin/perl

#Scalar string Operators
$scalar_string1="pe"."rl";
print "String 1: ".$scalar_string1."\n";
$scalar_string2="Perl Programming Tutorial " x (1+1);
print "String 2: ".$scalar_string2."\n";
$scalar_string3="3"."\ttabs" x 3;
print "String 3: ".$scalar_string3."\n";
$scalar_string4="Perl\x20".'Programming '."Tutorial";
print "String 4: ".$scalar_string4."\n";
$scalar_string5=9x5;
print "String 5: ".$scalar_string5."\n";

hippo-MacBook-Pro:~ hippo$ ./perl_strings.pl
String 1: perl
String 2: Perl Programming Tutorial Perl Programming Tutorial
String 3: 3 tabs tabs tabs
String 4: Perl Programming Tutorial
String 5: 99999

Step:12 Non-Decimal Integers

#!/usr/bin/perl

#perl binary integer
$hash_binary_integer = 0b10000;
#perl octal integer
$hash_octal_integer = 020;
#perl hexadecimal integer
$hash_hexadecimal_integer1 = 0x10;
$hash_hexadecimal_integer2 = 0x124c_78_aa;

print $hash_octal_integer."\n";
print $hash_binary_integer."\n";
print $hash_hexadecimal_integer1."\n";
print $hash_hexadecimal_integer2."\n";

hippo-MacBook-Pro:~ hippo$ ./non_decimal_integer.pl
16
16
16
307001514

Step:13 Scalar Constant Variable

#!/usr/bin/perl

$ordinary_scalar = 5;
$ordinary_scalar = 10;

print $ordinary_scalar."\n";

#perl constant declaration
*SCALAR_CONSTANT = 5;
$SCALAR_CONSTANT = 10;

hippo-MacBook-Pro:~ hippo$ ./perl_constant.pl
10
Modification of a read-only value attempted at ./perl_constant.pl line 10.

Step:14 String And Numeric comparison Operators

#!/usr/bin/perl
# String comparison
if ( 'Perl' eq 'perl' ) {
print "TRUE\n";
} else {
print "FALSE\n";
}

# Numeric comparison
if ( '2.4' != '2.6' ) {
print "TRUE\n";
} else {
print "FALSE\n";
}

hippo-MacBook-Pro:~ hippo$ ./comparision.pl
FALSE
TRUE

Post a Comment
Read more: http://bloggerknown.blogspot.com/2013/02/changing-blog-page-by-page-number.html#ixzz2TaVMS7IW