Debugging scripts Make sure your script is executable [SineWave-3:~/git/PFB2012/review_session_scripts] simonp% chmod +x errors.pl [SineWave-3:~/git/PFB2012/review_session_scripts] simonp% errors.pl errors.pl: Command not found. Need to use ./ [SineWave-3:~/git/PFB2012/review_session_scripts] simonp% ./errors.pl [SineWave-3:~/git/PFB2012/review_session_scripts] simonp% ./errors.pl text.txt 2 With while loop------------------ No lines print!!!! Let's run in the debugger You run this with perl -d script.pl perl will start up the debugger and tell you the line it is about to execute h gets help in the debugger [SineWave-3:~/git/PFB2012/review_session_scripts] simonp% perl -d errors.pl text.txt 2 Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(errors.pl:5): my $arg1 = shift; DB<1> print "@ARGV" text.txt 2 DB<2> h List/search source lines: Control script execution: l [ln|sub] List source code T Stack trace - or . List previous/current line s [expr] Single step [in expr] v [line] View around line n [expr] Next, steps over subs f filename View source in file Repeat last n or s /pattern/ ?patt? Search forw/backw r Return from subroutine M Show module versions c [ln|sub] Continue until position Debugger controls: L List break/watch/actions o [...] Set debugger options t [expr] Toggle trace [trace expr] <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint ! [N|pat] Redo a previous command B ln|* Delete a/all breakpoints H [-num] Display last num commands a [ln] cmd Do cmd before line = [a val] Define/list an alias A ln|* Delete a/all actions h [db_cmd] Get help on command w expr Add a watch expression h h Complete help page W expr|* Delete a/all watch exprs |[|]db_cmd Send output to pager ![!] syscmd Run cmd in a subprocess q or ^D Quit R Attempt a restart Data Examination: expr Execute perl code, also see: s,n,t expr x|m expr Evals expr in list context, dumps the result or lists methods. p expr Print expression (uses script's current package). S [[!]pat] List subroutine names [not] matching pattern V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or !pattern. X [Vars] Same as "V current_package [Vars]". i class inheritance tree. y [n [Vars]] List lexicals in higher scope . Vars same as V. e Display thread id E Display all thread ids. For more help, type h cmd_letter, or run man perldebug for all docs. you can set any variable you like in the debugger DB<6> $flowers = 'red rose' DB<7> p $flowers red rose main::(errors.pl:5): my $arg1 = shift; DB<8> v 2: use warnings; 3: use strict; 4 # pass filename and number of lines to read from the file on command line 5==> my $arg1 = shift; 6: my $arg2 = shift; 7: open(my $fh, "<", $arg1); 8: print "\n\nWith while loop------------------\n\n"; 9: my $count = 3; 10: while ( $count <= $arg2 ) { 11: my $line = <$fh>; #read a line from the file R restarts the code DB<17> R Warning: some settings and command-line options may be lost! Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(errors.pl:5): my $arg1 = shift; DB<17> n main::(errors.pl:6): my $arg2 = shift; DB<17> main::(errors.pl:7): open(my $fh, "<", $arg1); DB<17> main::(errors.pl:8): print "\n\nWith while loop------------------\n\n"; DB<17> c continues to the end of the script DB<26> c With while loop------------------ the representation of written language Text (literary theory), a concept in literary theory Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. You can set up breakpoints b 12 means break the code when the debugger gets to line 12 main::(errors.pl:5): my $arg1 = shift; DB<26> b 12 DB<27> c With while loop------------------ main::(errors.pl:12): chomp $line; DB<27> p $line the representation of written language DB<28> x $line 0 'the representation of written language ' you can also set up a break on a condition here, we break on line 12 if the condition $count > 0 is met DB<30> R Warning: some settings and command-line options may be lost! Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(errors.pl:5): my $arg1 = shift; DB<30> b 12 $count > 0 DB<31> c With while loop------------------ the representation of written language main::(errors.pl:12): chomp $line; DB<31> x $line 0 'Text (literary theory), a concept in literary theory ' DB<32> q Interactive perl debugger shell starts by executing perl -de 4 (you can use any number) You can work with hashes in this debugger mode You can open and read or write files You can define any perl variable You can inspect any perl variable or data structure with x [SineWave-3:~/git/PFB2012/review_session_scripts] simonp% perl -de 4 Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 4 DB<1> $a = 'hello world' DB<2> p $a hello world DB<8> open($fh, "<" , "text.txt") DB<9> $line = <$fh> DB<10> p $line the representation of written language DB<11> open($ofh, ">", 'temporary.txt') DB<12> print $ofh "this is some text\n" DB<13> close($ofh) DB<14> %hash = (dave => 'mangoes' , bob => 'oranges' , karen => 'peaches') DB<15> x %hash 0 'karen' 1 'peaches' 2 'bob' 3 'oranges' 4 'dave' 5 'mangoes' DB<16> x \%hash 0 HASH(0x7f83c21d28a8) 'bob' => 'oranges' 'dave' => 'mangoes' 'karen' => 'peaches' DB<17> $hash{dave}='pears' DB<18> x \%hash 0 HASH(0x7f83c21d28a8) 'bob' => 'oranges' 'dave' => 'pears' 'karen' => 'peaches' DB<19> $hash{emma} = 'peaches' DB<20> x \%hash 0 HASH(0x7f83c21d28a8) 'bob' => 'oranges' 'dave' => 'pears' 'emma' => 'peaches' 'karen' => 'peaches' DB<21> $hash{karen}='apples' DB<22> x \%hash 0 HASH(0x7f83c21d28a8) 'bob' => 'oranges' 'dave' => 'pears' 'emma' => 'peaches' 'karen' => 'apples'