Your SlideShare is downloading. ×
0
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Perl
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Perl

415

Published on

This presentation is about PERL basics for beginners.

This presentation is about PERL basics for beginners.

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
415
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
28
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PERLPERLPractical Extraction & Reporting LanguagePractical Extraction & Reporting Language
  • 2. HistoryDeveloped by Larry Wall in1987.A combination of C and UnixShell programming language.Originally developed for UNIX,but now runs under all OS.“A good perl program is one that gets the jobdone before your boss fires you.”Larry Wall, the creator of Perl.
  • 3. ContentsIntroductionVariablesOperatorsConditional Statements and LoopsRegular ExpressionsSubroutinesFile HandlingMany more....
  • 4. File Management Contd..binmode(HANDLE): change file mode from text to binaryunlink("myfile"): delete file myfilerename("file1","file2"): change name of file file1 to file2mkdir("mydir"): create directory mydirrmdir("mydir"): delete directory mydirchdir("mydir"): change the current directory to mydirsystem("command"): execute command commanddie("message"): exit program with message messagewarn("message"): warn user about problem message
  • 5. IntroductionInterpreted language, optimized for string manipulation, I/O andsystem tasks.Speed of developmentEasy to use, portableFreely availableExtension is .plExecution : perl file.plAll perl program goes through 2 phases :a compile phase where the syntax is checked and the source code,including any modules used, is converted into bytecode.a run-time phase where the bytecode is processed into machineinstructions and executed.
  • 6. Basic SyntaxAll statements should end with ;# is used for commenting a linePerl is case sensitiveBasic options : -c, -v, -w
  • 7. VariablesThree basic data types, scalars, arrays and hashesScalars : Holds a single value of any type.$var1 = 123$var2 = “Hello, how r u”$var3 = 1.23$1var = 123 # Error
  • 8. Built-in FunctionsChomp() : The chomp() function will remove (usually) any newlinecharacter from the end of a string.Chop() : The chop() function will remove the last character of a string(or group of strings) regardless of what that character is.
  • 9. Control FlowSyntax same as CIf Construct :if (expr) {stmt block;}If Else Construct :
  • 10. LoopsLoops in Perl are written in the same way as in C.Only remember that the loop variable is a scalar variable and must bepreceded by a $ sign.while (expr) {stmt block;}do {stmt block;} while (expr);for (expr1 ; expr2 ; expr3) {stmt block;}foreach var (listexpr) {
  • 11. ForeachBehaves same as for loop.foreach $person (@names) {print "$person";}$_ : Default Input and Pattern Searching Variableforeach (@names) {print "$_";}
  • 12. ListsList is a group of scalars used to initialize and array or hash.Elements can be any type of scalar data.List functions :Join : Joins list valuesSplit : A string is splitedMap : Evaluates expression or blocksGrep : Returns a sublist of list for each a specific criteria is true
  • 13. ArraysIn Perl an array does not have to be declared before it is used.The size of an array increases dynamically.An array variable has an @ before it.@num = (1,2,3,4,5);@num = (1..5);@mix_arr = ("Tom", 5, "Cruise", 90, "Jane", 35.67 34, 78, "I am Bond");You can assign an array element to any index - even one that isbeyond the current range of index numbers.The in-between elements are assigned null values.
  • 14. Push, Pop and SpliceThe push function pushes an element onto an array. If the last element inan array has index 5, pushing an element onto the array makes it theelement with index 6.The syntax is:push(@arrayname, value);The pop function pops off the last element off the array. So, if the lastelement in the array @myarray is 99, then the statement$num = pop(@myarray);assigns the value 99 to $num.The splice function is used to pop more than one element.(splice(@myarray, -3)
  • 15. Unshift and ShiftThese work like the push and pop operators, but add and subtract fromthe beginning of an array rather than the end.The syntax isunshift(@arrayname, value);shift(@arrayname);
  • 16. Associative Array or HashHashes are a list of scalars, but instead of being accessed by indexnumber, they are accessed by a key.Syntax : %myhash = (Key1, Val1, Key2, Val2, Key3, Val3)Index No Value0 Spain1 Belgium2 Germany3 NetherlandsKey ValueSP SpainBL BelgiumGE GermanyNL Netherlands
  • 17. Perl Command LineRead from command prompt.Go to example.
  • 18. Hash FunctionsAssigning : $countries{PT} = Portugal;Deleting : delete $countries{NL};Print all the keys : print keys %countries;Print all the values : print values %countries;A slice of hash : print @countries{NL, BL};How many elements : print scalar(keys %countries);Does the key exist : print “I exist n” if exists $countries{NL};
  • 19. SubroutineSyntax for subroutines :Sub subname {Stmt block;}Value of the last expression evaluated by the subroutine is automaticallyconsidered to be subroutines return value.Foreach $var (&subname) {Stmt block;}Perl defines 3 special subroutines that are executed at specific times.BEGIN : Called when our program starts running.END : Called when the program terminates.AUTOLOAD : Called when the program cannot find a subroutine it is
  • 20. File Managementopen(INFILE,"myfile"): readingopen(OUTFILE,">myfile"): writingopen(OUTFILE,">>myfile"): appendingopen(INFILE,"someprogram |"): reading from programopen(OUTFILE,"| someprogram"): writing to programopendir(DIR,"mydirectory"): open directoOperations on an open file handle$a = <INFILE>: read a line from INFILE into $a@a = <INFILE>: read all lines from INFILE into @a$a = readdir(DIR): read a filename from DIR into $a@a = readdir(DIR): read all filenames from DIR into @aread(INFILE,$a,$length): read $length characters from INFILE into $aprint OUTFILE "text": write some text in OUTFILEClose files / directories
  • 21. Perl DebuggerPerl -d myprogram.plDebugger commands :l : lists the next few statements.l 10 : specifies the line numberl 10-15 : display a range of linesl subroutine : display subroutine- : display the immediately preceding the last displayed line/search/ : Search for a line containing the pattern?backsearch? : To search backword for a patternS : lists all the subroutines in the files : execute single statement and displays next statement to executen : same as s command, but n command does not enter subroutine,directly execute itr : if we are inside a subroutine and do not want to execute further, thiscommand finishes subroutine execution and returns to the laststatement called the subroutineX : print the value of a particular variable
  • 22. ResourcesPerl.comPerl.orgLists.perl.orgPerlmonksStackoverflowPerl mongersSearch.cpan.org
  • 23. Continued...

×