print variable name not by hash but ref?

Discussion in 'Perl Misc' started by Ela, Apr 1, 2008.

  1. Ela

    Ela Guest

    some day i read a book telling that it may be useful to print a variable
    name for debugging purpose. since i was new that moment, i just skipped it.
    now when i want to use this feature, i no longer get back where it is.
    google search using perl and "print variable name" does not return good
    results. And I am not going to use hash because i just want to debug, e..g.
    I expect

    $var1 = 3;
    $ALongVariable = "hahahaha";

    debug($var1);
    debug($ALongVariable);

    sub debug {
    $dvar = shift;
    some more codes here?
    print $dvar;
    print "\n";
    }

    =======
    to print out:

    $var1 : 3
    $ALongVariable : hahahaha

    Could anybody help?
    Ela, Apr 1, 2008
    #1
    1. Advertising

  2. "Ela" <> wrote in
    news:fssqc1$3f7$:

    > some day i read a book telling that it may be useful to print a
    > variable name for debugging purpose. since i was new that moment,
    > i just skipped it.


    The closest thing I know of is

    http://search.cpan.org/~robin/PadWalker-1.7/PadWalker.pm

    Read the docs, especially the warnings.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Apr 1, 2008
    #2
    1. Advertising

  3. Ela

    smallpond Guest

    On Apr 1, 4:05 am, "Ela" <> wrote:
    > some day i read a book telling that it may be useful to print a variable
    > name for debugging purpose. since i was new that moment, i just skipped it.
    > now when i want to use this feature, i no longer get back where it is.
    > google search using perl and "print variable name" does not return good
    > results. And I am not going to use hash because i just want to debug, e..g.
    > I expect
    >
    > $var1 = 3;
    > $ALongVariable = "hahahaha";
    >
    > debug($var1);
    > debug($ALongVariable);
    >
    > sub debug {
    > $dvar = shift;
    > some more codes here?
    > print $dvar;
    > print "\n";
    >
    > }
    >
    > =======
    > to print out:
    >
    > $var1 : 3
    > $ALongVariable : hahahaha
    >
    > Could anybody help?



    That can't work since only the value is passed to your
    debug sub, not the variable.

    You can get to package variables through the symbol table
    but I don't know how to get to lexicals.

    perl -e 'our $foo=5; print join "\n", keys %main::;' |grep foo
    foo

    perl -e 'my $foo=5; print join "\n", keys %main::;' |grep foo
    <== no package variable named 'foo'
    smallpond, Apr 1, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bobby Chamness
    Replies:
    2
    Views:
    2,378
    Joe Smith
    Apr 22, 2007
  2. rp
    Replies:
    1
    Views:
    494
    red floyd
    Nov 10, 2011
  3. Rg

    Hash vs. Hash ref

    Rg, May 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    88
    Paul Lalli
    May 3, 2007
  4. Sébastien Cottalorda
    Replies:
    3
    Views:
    213
    Peter Makholm
    May 11, 2009
  5. Justin C
    Replies:
    1
    Views:
    177
    Justin C
    Oct 7, 2013
Loading...

Share This Page