X hits on this document

PDF document

Running and Debugging Perl - page 11 / 30

67 views

0 shares

0 downloads

0 comments

11 / 30

Running and Debugging Perl

#!/usr/bin/perl

  • #

    nostrict.plx

use warnings; use strict;

our $first = "this"; our $second = "first"; our $third;

{

no strict ('refs'); $third = ${$second};

}

print "$third\n";

>perl nostrict.plx Name "main::first" used only once: possible typo at nostrict.plx line 6. this >

The warnings have been turned off for our symbolic link, but again we get that warning about only explicitly using $first once, even though we have indirectly used it again. This is a useful reminder of how warnings work: perl will check to see that the code looks structurally sound, but won't actually calculate runtime values or resolve variables. If it did, it would have picked up above on ${$second} being resolved as $first.

Don't turn off these checks simply because they stop your program from running. You should always find a way to fix the program so as to satisfy them.

use diagnostics

There's another pragma that may help you while debugging. use diagnostics will show you not only an error message or warning but also the explanatory text from the perldiag documentation page. For instance:

#!/usr/bin/perl

  • #

    diagtest.plx

use warnings; use strict; use diagnostics;

my $a, $b = 6; $a = $b;

should give something like:

>perl diagtest.plx Parentheses missing around "my" list at diagtest.plx line 7 (#1)

(W parenthesis) You said something like

my $foo, $bar = @_;

289

Document info
Document views67
Page views67
Page last viewedSat Oct 29 00:30:53 UTC 2016
Pages30
Paragraphs723
Words10014

Comments