Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Debugger actions: On which lines?

by worik (Sexton)
on Aug 18, 2015 at 04:28 UTC ( [id://1138950]=perlquestion: print w/replies, xml ) Need Help??

worik has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl -w use strict; my $x = 3; for(1..3){ my $y = $x; $y++; $x -= $y; }

The above little programme, if I put an acton on line 6 ($y++) the action occurs at line 6, line 7 then at line 5 where it is invalid ($y not defined)

To illustrate here is my session in the debugger...

DB<3> a 6 print ("\$y $y\n") DB<4> n DB<4> DB<4> $y 3 DB<4> $y 4 DB<4> Use of uninitialized value $y in concatenation (.) or string at (eval +9)[/usr/share/perl/5.18/perl5db.pl:732] line 1. at (eval 9)[/usr/share/perl/5.18/perl5db.pl:732] line 1. eval 'no strict; ($@, $!, $^E, $,, $/, $\\, $^W) = @DB::saved;pack +age main; print ("\\$y $y\\n"); ' called at /usr/share/perl/5.18/perl5db.pl line 732 DB::eval called at /usr/share/perl/5.18/perl5db.pl line 2661 DB::DB called at test.pl line 5 $y DB<4>

Clearly I misunderstand the manual. How do I do an action so it happens on only one line?

Replies are listed 'Best First'.
Re: Debugger actions: On which lines?
by james28909 (Deacon) on Aug 18, 2015 at 05:23 UTC
    Not trying to go off topic, but isnt that code slightly redundant? after the first loop the output will always be the same. But back to the question. I do not get any errors using strict or warnings, or in the debugger.
    EDIT: Updated code

      Aha! Adding back the shebang line gives the error. Taking it out and it performs as one would expect!

      To be crystal try:

      #!/usr/bin/perl -w use strict; use warnings; my $x = 3; my $y; for(1..3){ $y = $x;print "line 6 ---- x = $x -- y = $y\n"; $y++; print "line 7 ---- x = $x -- y = $y\n"; $x -= $y; print "line 8 ---- x = $x -- y = $y\n"; }

      For me that has the error and...

      use strict; use warnings; my $x = 3; my $y; for(1..3){ $y = $x;print "line 6 ---- x = $x -- y = $y\n"; $y++; print "line 7 ---- x = $x -- y = $y\n"; $x -= $y; print "line 8 ---- x = $x -- y = $y\n"; }

      ....does not

        It's not the shebang, it's the -w switch. Take out -w and only use use warnings instead and the error goes away because warnings is lexically scoped but -w is global. Using -w turned warnings on globally which is why your error came from the debugger code as it told you.

        I don't know why the action is executed on each line of the loop; I agree that docs indicate that it should be executed only on that line each time through the loop.

        Update: Realized the example code had both -w and use warnings.

        The way forward always starts with a minimal test.
        It works with or without the shebang for me, go figure... I have windows 10 64bit and active-state perl 5.16.3 32bit. I think you should declare $y before the loop though like my $y = 0; that would probably get rid of your errors.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1138950]
Approved by 1nickt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-19 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found