2009/11/23 Mark_Galeck <mark_galeck_spam_mag...@yahoo.com>:
> Hello,  I want to write a simple debug-print subroutine, which you
> could call like this:
>
> $foobar = "foobar";
> dbgPrint foobar;

One problem with your interface is it breaks under "use strict;". You
should always use strict and use warnings in any program not part of a
perl -e command line. Barewords and symbolic references are both ugly
and the source of hard-to-find bugs. Here is a rant about symbolic
references and why they are bad:

http://perl.plover.com/varvarname.html

> and it would print the variable name and value.  I came up with the
> following:
>

I would recommend the Smart::Comments module from CPAN. It's used as follows:

p...@tui:~/tmp$ cat foo.pl
use strict;
use warnings;

my $x = "hello";

use Smart::Comments;

### $x
p...@tui:~/tmp$ perl foo.pl

### $x: 'hello'

The line containing a comment beginning ### is treated as special.
Smart::Comments detects it contains a variable name and prints both
the name and its value. It works with lexical variables, too.

There are many advantages to Smart::Comments, one of which is that
when you want to disable debug printing from your program, you just
remove the "use Smart::Comments;" line and all smart comments become
normal comments again.

Unfortunately I can't tell you how to solve your original question;
but I hope this fixes the underlying issue.

Philip

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to