On Mon, Nov 23, 2009 at 8:36 AM, Mark_Galeck <
mark_galeck_spam_mag...@yahoo.com> wrote:

> Hello,  I want to write a simple debug-print subroutine, which you
> could call like this:
>
> $foobar = "foobar";
> dbgPrint foobar;
>
> and it would print the variable name and value.  I came up with the
> following:
>
> sub dbgPrint {
>                my $arg = $_[0];
>                my @up = caller;
>                print "file ".$up[1]."\tline ".$up[2]."\t".$arg." is
> ".${$arg}."\n";
> }
>
>
> This works great, but only for global variables.  If I have
>
> my $foobar = "foobar";
>
> then of course it does not work.  Well it is easy to write something
> that would work like this
>
> dbgPrint foobar, $foobar
>
> but this seems redundant - how to just use one argument??
> Mark
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
> Hi Mark,

I would say have a look at Data::Dumper, which will help you deal with
variables like Hash and Array as well. To figure out the name of the
variable I am at a loss... I never needed that other then on very rare
occasions where I don't mind writing print "Variable name: xxx Value: yyy".

You might want to look into the perl debugger which will definitely offer
this possibility but it might be a little overkill for the thing you are
looking at. Personally I rarely use it because I have never had such complex
issues that I needed it, usually a simple Data::Dumper and some common sense
works just as well.

Regards,

Rob

Reply via email to