learn perl in amc square learning

17
Learn Perl at AMC Square Learning

Upload: amc-square

Post on 17-Sep-2015

9 views

Category:

Documents


2 download

DESCRIPTION

AMC Squarelearning Bangalore is the best training institute for a career development. it had students from various parts of the country and even few were from West African countries.

TRANSCRIPT

  • Learn Perl at AMC Square Learning

  • Perl"Practical Extraction and Reporting Language"written by Larry Wall and first released in 1987Perl has become a very large system of modulesname came first, then the acronymdesigned to be a "glue" language to fill the gap between compiled programs (output of "gcc", etc.) and scripting languages"Perl is a language for easily manipulating text, files and processes": originally aimed at systems administrators and developers

  • What is Perl? Perl is a High-level Scripting languageFaster than sh or csh, slower than CNo need for sed, awk, head, wc, tr, Compiles at run-timeAvailable for Unix, PC, MacBest Regular Expressions on Earth

  • Executing Perl scripts"bang path" convention for scripts:can invoke Perl at the command line, oradd #!/public/bin/perl at the beginning of the scriptexact value of path depends upon your platform (use "which perl" to find the path)one execution method: % perl print "Hello, World!\n"; CTRL-D Hello, World!preferred method: set bang-path and ensure executable flag is set on the script file

  • Perl BasicsComment lines begin with: #File Naming Schemefilename.pl (programs)filename.pm (modules)Example prog: print Hello, World!\n;Statements must end with semicolon$a = 0;Should call exit() function when finishedExit value of zero means successexit (0); # successfulExit value non-zero means failureexit (2); # failure

  • Data TypesInteger25 750000 1_000_000_0008#100 16#FFFF0000Floating Point1.25 50.0 6.02e23 -1.6E-8Stringhi there hi there, $name qq(tin can)print Text Utility, version $ver\n;Boolean0 0.0 "0" represent Falseall other valuesrepresent True

  • Variable TypesScalar$num = 14;$fullname = John H. Smith;Variable Names are Case SensitiveUnderlines Allowed: $Program_Version = 1.0;

  • Scalarsusage of scalars: print ("pi is equal to: $pi\n"); print "pi is still equal to: ", $pi, "\n"; $c = $a + $bimportant! A scalar variable can be "used" before it is first assigned a valueresult depends on contexteither a blank string ("") or a zero (0)this is a source of very subtle bugsif variable name is mispelled what should be the result?do not let yourself get caught by this use the "-w" flag in the bang path: #!/public/bin/perl -w

  • OperatorsMathThe usual suspects: + - * / % $total = $subtotal * (1 + $tax / 100.0);Exponentiation: **$cube = $value ** 3;$cuberoot = $value ** (1.0/3);Bit-level Operationsleft-shift: $val = $bits >> 8;Assignments

    As usual: = += -= *= /= **= =$value *= 5;$longword

  • ArithmeticPerl operators are the same as in C and Javathese are only good for numbersbut beware: $b = "3" + "5"; print $b, "\n"; # prints the number 8if a string can be interpreted as a number given arithmetic operators, it will bewhat is the value of $b?: $b = "3" + "five" + 6?Perl semantics can be tricky to completely understand

  • ConditionalsNumeric string

    Equal: == eqLess/Greater Than:< >lt gt Less/Greater or equal:=le geZero and empty-string means FalseAll other values equate to TrueComparison:cmpResults in a value of -1, 0, or 1Logical Not: !if (! $done) { print keep going; }

  • Control Structuresif statement - second stylestatement if condition;print \$index is $index if $DEBUG;Single statements onlySimple expressions onlyunless is a reverse ifstatement unless condition;print millenium is here! unless $year < 2000;

  • Subroutines (Functions)Calling a Subroutine&subname; # no args, no return value&subname (args);retval = &subname (args);The & is optional so long assubname is not a reserved wordsubroutine was defined before being calledPassing ArgumentsPasses the valueLists are expanded@a = (5,10,15); @b = (20,25); &mysub(@a,@b);this passes five arguments: 5,10,15,20,25mysub can receive them as 5 scalars, or one array

  • Command Line Args$0 = program name@ARGV array of arguments to programzero-based index (default for all arrays)Exampleyourprog -a somefile$0 is yourprog$ARGV[0] is -a$ARGV[1] is somefile

  • Basic File I/OReading a Fileopen (FILEHANDLE, $filename) || die \ open of $filename failed: $!; while () { chomp $_;# or just: chomp; print $_\n; } close FILEHANDLE;Writing a Fileopen (FILEHANDLE, >$filename) || die \ open of $filename failed: $!; while (@data) { print FILEHANDLE $_\n; # note, no comma! } close FILEHANDLE;

  • Debugging in Perl-w option is great!#!/bin/perl -wtells you aboutmisused variablesusing uninitialized data/varablesidentifiers that are used only onceand moreDebug mode: perl -d filename [args]Display CommandshExtended helph hAbbreviated helpl (lowercase-L)list lines of codel sublist subroutine subl 5list line 5l 3-6list lines 3 through 6 inclusivellist next window of lines

  • Thank you

    ***************