Search This Blog

Friday, July 12, 2013

PERL: BASICS Tutorial Hands on - 1

#!/usr/bin/perl #That should be the first line for any perl script in UNIX print "C:\\temp"."\n"; #To display the sentencases use print print "C:/temp"."\n"; #Escape characters like \b, \n, \r, \t etc are similar in perl as well $var1=3; #variable can be declared using $ $var2=7; #comments should start with # $var1++; #Each command should end with semicolan --$var2; #autoincrement and decrement varaible works as as usual $var3=$var1+$var2; #Arithmatic variable are also same $var3=$var1-$var2; #comments can also write at the end of line $var3=$var1*$var2; #allows single line comments only $var3=$var1/$var2; #in UNIX should be with .pl extension $var3=$var1**$var2; #power $var3=$var1%$var2; #quotient $var3 += $var2; #Assignments also works as usual $var4="Hello "; #string concatenation with . print "$var4 Hi"."\r"; print $var4 x $var1 ."\n"; # var4 is repeated var1 times #open(FILE,"$d) ? "ternary Operator" : "not working"; print $e; while($d++ < $c--) { #while loop print $d."\t".$c."\n"; } until($a==$b++) { #until loop print "until loop"; }

PERL: Basic tutorial - 1

UNIX, the first line of a Perl program should begin with #!/usr/bin/perl and the file should have executable permissions. Then typing the name of the script will cause it to be executed. The basic data types known to Perl are scalars, lists, and hashes. Scalar $var1 Simple variables that can be a number, a string, or a reference. A scalar is a “thingy.” List @var1 An ordered array of scalars accessed using a numeric subscript. $var1[0] Hash %var1 An unordered set of key/value pairs accessed using the keys as subscripts. $var1{key} Perl uses an internal type called a typeglob to hold an entire symbol table entry. The effect is that scalars, lists, hashes, and filehandles occupy separate namespaces (i.e., $var1[0] is not part of $var1 or of %var1). The prefix of a typeglob is *, to indicate “all types.” Typeglobs are used in Perl programs to pass data types by reference. You will find references to literals and variables in the documentation. Literals are symbols that give an actual value, rather than represent possible values, as do variables. For example in $var1 = 1, $var1 is a scalar variable and 1 is an integer literal. Variables have a value of undef before they are defined (assigned). The upshot is that accessing values of a previously undefined variable will not (necessarily) raise an exception. To display, simply use Perl’s print function: #!/usr/bin/perl #That should be the first line for any perl script in UNIX print "C:\\temp"."\n"; #To display the sentencases use print print "C:/temp"."\n"; #Escape characters like \b, \n, \r, \t etc are similar in perl as well The basic assignment operator is “=”. The other arithmetic operators work in normal way: $var1=3; #variable can be declared using $ $var2=7; #comments should start with # $var1++; #Each command should end with semicolan --$var2; #autoincrement and decrement varaible works as as usual $var3=$var1+$var2; #Arithmatic variable are also same $var3=$var1-$var2; #comments can also write at the end of line $var3=$var1*$var2; #allows single line comments only $var3=$var1/$var2; #in UNIX should be with .pl extension $var3=$var1**$var2; #power $var3=$var1%$var2; #quotient $var3 += $var2; #Assignments also works as usual $var4="Hello "; #string concatenation with . print "$var4 Hi"."\r"; print $var4 x $var1 ."\n"; # var4 is repeated var1 times Other logical operators and logical conditions will be same as other languages. Below has syntax with If, unless, until and while loops: $a=1; $b=0; if ($a && $b) {#logical operators are n print "AND IS NOT WOrkind"."\n"; #Blocks of statements should be in curly braces } elsif ($a and $b) { print "AND word IS not working"; #syntaxes are case sensitive } elsif (!$a || $b) { print "Negation or OR IS not working"; } elsif (not $a or $b) { print "OR IS working"."\t"; } elsif (not $a or $b) { print "OR IS working"."\t"; } #special variables print $_.$0.$$.$!."\n"; #default inpout and pattern searching space, program name, process id, current value of error number #string variables $s1="Hello"; $s2='Hello'; print $s1."\t".$s2; $c=9; $d=0; unless(--$c==++$d) {#comparison operators are just like we have in arithmetics print "false statement"; #UNLESS statement } else { print "true statement"; } print "invert if function works" if ($c); print "invert of unless" unless (!$d); $e=($c>$d) ? "ternary Operator" : "not working"; print $e; while($d++ < $c--) { #while loop print $d."\t".$c."\n"; } until($a==$b++) { #until loop print "until loop"; }