Monday, August 31, 2015

Perl tutorial part 1 - basic fundamentals and variables

# This is a program written in perl
#let's start with variables, variables are assigned like given below
$name = "Ataul Haque";

print "Hello, $name\n";

print("Bracketed Hello, $name\n");

=begin
Whatever written after=begin is considered comment
it supports multi line comments.
Doesn't end untill you end it with
=cut

#let's see the diff between single and double quote difference
print 'Hello, $name';
print "\n";

#so, we saw single quote do not interpolate variables.
$a = 11;

$var = <<"EOF";
This is the syntax for writing documents in perl
\uyou can write as many \"beautiful\" lines, as you wish.
This all get's stored in the single \Uvariable\E assigned and can be displayed
as it is very \Qsmoothly.\E let's print a = $a.
EOF

print $var;
#Now we going to define arrays with @ sign
@arr = (1,2,3,4,5,6,7,8,9,10);
print "\$arr[5]=$arr[5]\n";
#Now let's move to hash variables which stores key/value pairs
%hashvar = ('Ataul',23,'DK',24,'PK',45,'CK',67);

#print $hashvar{"DK"};
print "\uage of PK is: $hashvar{'PK'}\n";

#Now move to reuse of same variable in different forms
@fruits = ('Mango','Apple','Grapes','Litchi');
@fal = @fruits;
$bhav = @fal;
print "Fruits in basket are: @fal\n";
print "There are $bhav fruits in basket\n";

#Let's play with string operations
$str = 'Hello '.' World';
$num = 4 + 5;
$mix = $str.$num;
print $str; print "\n";
print $num; print "\n";
print $mix;
print"\n";

#multiline revisited
print <<EOF;
Here you type \uall you wish
in as many lines as you can
These all get printed as you wished
EOF

#\u now we will learn real garbage any literal starting with v and followed by . will be interpolated like garbage
#see below
$garbage = v11.22.33.444;
print $garbage;
#Let's see some special literals
#One important thing about these special literal is there are two undercores to each side of the strings
print "file name is " . __FILE__ . "\n";
print "line number is " . __LINE__ . "\n";
print "Package name is " . __PACKAGE__ . "\n";

#Another way of assigning array variables
@arr2 = qw/mon tue wed thu fri sat/;
#now access each of them
print "$arr2[3]\n";   #this will print thu
print "$arr2[-3]"; #this again will print thu

#range is seriously very easy in perl
@rangevar = (1..10);
@rangevar1 = (10..30);
@rangevar2 = ('a'..'z');
print "@rangevar\n";
print "@rangevar1\n";
print "@rangevar2\n";

#Let's play with array size
@numbers = (1,2,3,4);
$numbers[30] = 90;
$sizenum = @numbers;
$max_index = $#numbers;
print "size of number array is: $sizenum\n";
print "maximum index of array is: $max_index\n";

#Now some more functions to play with arrays
#Let's create an array and then will play with that array
@words = ("apple","boy","cat","dog"); #array created
print "@words\n";

push(@words,"elephant");   #one more element added to the array at the end
print "@words\n";

unshift(@words,"apricot");   #one more element added to the array at the begining of the array list
print "@words\n";

pop(@words); #one element removed from the end of the array
print "@words\n";

shift(@words); #One element removed from the array from the begining
print "@words\n";

#now it's my favourite slicing of array
@slicing = qw/mon tue wed thu fri sat sun/;
print "@slicing\n";
@weekdays = @slicing[0..4];   #alternatively you can write @weekdays = @slicing[0,1,2,3,4];
print "@weekdays\n";


#######################################################
Output of the Whole Program is below


Hello, Ataul Haque
Bracketed Hello, Ataul Haque
Hello, $name
This is the syntax for writing documents in perl
You can write as many "beautiful" lines, as you wish.
This all get's stored in the single VARIABLE assigned and can be displayed
as it is very smoothly\. let's print a = 11.
$arr[5]=6
Age of PK is: 45
Fruits in basket are: Mango Apple Grapes Litchi
There are 4 fruits in basket
Hello  World
9
Hello  World9
Here you type All you wish
in as many lines as you can
These all get printed as you wished
Wide character in print at C:/Users/Ataul Haque/workspace/perlEdu/FirstProg.pl line 53.
!Ƽfile name is C:/Users/Ataul Haque/workspace/perlEdu/FirstProg.pl
line number is 57
Package name is main
thu
thu1 2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e f g h i j k l m n o p q r s t u v w x y z
size of number array is: 31
maximum index of array is: 30
apple boy cat dog
apple boy cat dog elephant
apricot apple boy cat dog elephant
apricot apple boy cat dog
apple boy cat dog
mon tue wed thu fri sat sun
mon tue wed thu fri




No comments:

Post a Comment