On Mon, Jun 7, 2010 at 01:22, Bryan Harris <bryansli...@gmail.com> wrote: snip > How do other people deal with this? If I have a printf with seven complex > (?, like the above) variables in it and one of them is undef, aside from > breaking them all onto different print lines and re-running the script, how > else can I figure out which one is undef? snip
Finding the undef in print statements is easy: just look at the output. I generally know what I expect to be in a given variable and if it doesn't show up in the output, then that is the culprit. For instance, if print join(",", @list), "\n"; produces "foo,bar,,quux\n" then I know that the third element of @list is undef. If print "Ran $x tests against program $name\n"; produces "Ran 5 tests against program\n"; then I know $name is undef. It is harder to find the undef if code like my $n = $x * $y * $z; One solution is to test in a debugger, but, I am ashamed to say, I hate debuggers. They get in the way of how I do things. In general, I drop in a warn or print statement like this: print defined $x, defined $y, defined $z; my $n = $x * $y * $z; and rerun the code. But recently I have started using [Carp::REPL][1] to test code. Here is an example session: cow...@amans:~$ perl -MCarp::REPL=warn g.pl Use of uninitialized value $z in multiplication (*) at g.pl line 10. Trace begun at g.pl line 10 re.pl(main):001:0> :l File g.pl: 5: 6: my $x = 5; 7: my $y = 10; 8: my $z; 9: *10: my $n = $x * $y * $z; 11: 12: print "n is $n\n"; re.pl(main):002:0> defined $x ? $x : "undef" 5 re.pl(main):003:0> defined $y ? $y : "undef" 10 re.pl(main):004:0> defined $z ? $z : "undef" undef re.pl(main):005:0> :q n is 0 cow...@amans:~$ It has most of the advantages of a debugger without all of the pain. Of course, it can't stop at an arbitrary point in your code like a debugger can, but it will stop at any warning or die. Well, that is a bit of a lie, you can drop Carp::REPL::repl into your code and get the REPL without a warn or die, but that requires changing your code: cow...@amans:~$ perl g.pl Trace begun at g.pl line 12 re.pl(main):001:0> :l File g.pl: 7: 8: my $x = 5; 9: my $y = 10; 10: my $z; 11: *12: repl; 13: my $n = $x * $y * $z; 14: 15: print "$n\n"; re.pl(main):002:0> $x 5 re.pl(main):003:0> $y 10 re.pl(main):004:0> $z re.pl(main):005:0> $z = 3 3 re.pl(main):006:0> :q 150 cow...@amans:~$ [1] : http://search.cpan.org/dist/Carp-REPL/lib/Carp/REPL.pm -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/