X hits on this document

PDF document

Running and Debugging Perl - page 16 / 30

75 views

0 shares

0 downloads

0 comments

16 / 30

Chapter 9

>perl -ne 'next if /^#/; print' strictvar.plx use warnings; use strict;

my $a = 5; $main::b = "OK"; our $c = 10; $d = "BAD"; >

Note that we don't, and actually can't say:

>perl -pe 'next if /^#/'

This is because -p uses a special control structure, continue, translating internally to this:

LINE: while (defined($_ = <ARGV>)) {

"Your code here"; } continue { print $_;}

Anything in a continue { } block will always get executed at the end of an iteration – even if next is used (although is still by-passed by last).

  • -

    c

  • -

    c stops perl from running your program – instead, all it does is check that the code can be compiled as

valid Perl. It's a good way to quickly check that a program has no glaring syntax errors. It also loads up and checks any modules that the program uses, so you can use it to check that the program has everything it needs:

>perl –ce 'print "Hello, world\n";'

  • -

    e syntax OK

>perl -ce 'print ("Hello, world\n"));' syntax error at -e line 1, near "))"

  • -

    e had compilation errors.

>

Be careful though, because this won't necessarily prove that your program will run properly – it checks that your program is grammatically correct, but not whether it makes sense. For instance, this looks fine:

>perl –ce 'if (1) { next }'

  • -

    e syntax OK

> but if you try to run it normally, you'll get an error:

>perl -e 'if (1) { next }' Can't "next" outside a loop block at -e line 1. >

This is the difference between a compile-time and a runtime error. A compile-time error can be detected in advance and means that perl couldn't understand what you said. A runtime error means that what you said was comprehensible but (for whatever reason) can't be done.

  • -

    c only checks for compile-time errors.

294

Document info
Document views75
Page views75
Page last viewedSun Dec 04 23:41:22 UTC 2016
Pages30
Paragraphs723
Words10014

Comments