3. Command Line
Common Options
-c “compile check” – check syntax, but don’t run the program
-w turn on basic warnings (-W for *all* warnings)
-d load the debugger (with program or -e)
-e ‘code’ run this code
-n wraps this around the –e code:
while (<>) { code goes here }
-p same as –n, but prints $_ after each iteration:
while (<>) {code} continue {print $_}
-a “autosplit”, used with –p or –n, puts this in the while (<>) loop:
@F = split; # whitespace, same as split ‘ ‘
-F/pattern/ used with –a to split on a different pattern
-h help summary
05/13/12 3
4. Command Line: Examples
Version:
C:>perl -v
This is perl, v5.8.7 built for MSWin32-x86-multi-thread
(with 14 registered patches, see perl -V for more detail)
Compiler Check:
C:>perl –c dupes.pl
syntax error at dupes.pl line 17, near "))
“
syntax error at dupes.pl line 23, near "}“
dupes.pl had compilation errors.
Removing a stray right paren:
C:>perl –c dupes.pl
dupes.pl syntax OK
05/13/12 4
5. Command Line: Examples
One-liners: -p and –n
-n: loop on input, but don’t print unless requested:
perl –ne “your program here”
is equivalent to
while (<>) { your program here }
-p: loop on input, run “program”, print every line:
perl –pe “your program here”
is equivalent to
while (<>) { your program here; print }
“Golf”ed line counter
perl –pe “}{$_=$.}”
Homework – what does this do?
perl –MO=Deparse –pe “}{$_=$.}”
05/13/12 5
6. Command Line: Examples
One-liners: -e
Change all “FAIL”s to “PASS”es:
perl –pe “s/FAIL/PASS/g” my_input_file
Print lines with wafer coordinates:
perl –ne “/d+,d+/ and print” my_input_file
Sum the 7th column if the last column matches “Buy”
perl –ane “$sum+= $F[6] if ($F[-1]=~/buy/i);
print qq(sum=$sumn) if eof” my_input_file
For DOS, use double-quotes, and qq() to double-quote in programs.
For Unix, use single quotes, and q() to single-quote in programs.
05/13/12 6
7. Debugger
Common Options
l x-y list lines x to y
n single step (over function calls)
s single step (into functions)
<enter> repeat last n or s
c x continue (execute) to line x
b x break on line x
b x condition break on line x when condition is true
e.g., /barcode/ (same as $_ =~ /barcode/)
B x delete breakpoint on line x (or * to delete all breakpoints)
p expression print expression, with newline
x expression eXamine expression, including nested structure
x $scalar or x @array or x %hash
h help
R Restart program, rewinding all inputs (doesn’t work on DOS)
05/13/12 7
8. Debugger: Examples
Perl Scratch Pad
C:>perl -demo
Loading DB routines from perl5db.pl version 1.28
Editor support available.
Enter h or `h h' for help, or `perldoc perldebug' for more help.
main::(-e:1): mo
DB<1>
Print shortcut:
DB<1> p 17+42
59
Examine a variable
DB<5> x %hash
0 HASH(0x1d4cd18)
'Fred' => HASH(0x1d00a4c)
'Office' => '1F17'
'Phone' => 'x1234'
'George' => HASH(0x1cef8e0)
'Office' => '2F35'
'Phone' => 'x9876'
05/13/12 8
9. Debugger: Examples
Load a program:
perl –d histo.pl histo.pl
l (“ell”): list program
DB<1> l 1-11
1 #!/your/perl/here
2: use strict;
3: use warnings;
4
5==> my %seen;
6
7: while (<>)
8 {
9: my @fields = split;
10: $seen{$fields[0]}++;
11 }
Break (with condition), and continue
DB<2> b 10 $. > 5
DB<3> c
Use of uninitialized value in hash element at
histo.pl line 10, <> line 4.
at histo.pl line 10
main::(histo.pl:10): $seen{$fields[0]}++;
05/13/12 9