How to get the variable name, not values?

Discussion in 'Perl Misc' started by lihao0129@gmail.com, Apr 22, 2007.

  1. Guest

    Hi, folks:

    Is there a way in Perl to return the variable name instead of it's
    values, like in C, we can use the '#' token with macro:

    #define print_int(var) printf( #var " is %d\n", var)

    then each time I want to print out an interger, I issue:

    print_int(number);

    instead of

    printf("number is %d \n", number);

    The output string might be very long and used for various variables,
    so I need to wrap it into a subroutine or something else available for
    this purpose. Can I do this with Perl? Many thank for your hints..

    Best regards,
    Lihao
    , Apr 22, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi, folks:
    >
    > Is there a way in Perl to return the variable name instead of it's
    > values, like in C, we can use the '#' token with macro:
    >
    > #define print_int(var) printf( #var " is %d\n", var)


    This is not C, this is CPP.
    Of course you could use CPP for Perl programs, too, if you insist.

    jue
    Jürgen Exner, Apr 22, 2007
    #2
    1. Advertising

  3. Guest

    On Apr 22, 3:50 pm, "Jürgen Exner" <> wrote:
    > wrote:
    > > Hi, folks:

    >
    > > Is there a way in Perl to return the variable name instead of it's
    > > values, like in C, we can use the '#' token with macro:

    >
    > > #define print_int(var) printf( #var " is %d\n", var)

    >
    > This is not C, this is CPP.
    > Of course you could use CPP for Perl programs, too, if you insist.
    >
    > jue


    Hi, Jue:

    This '#' token with macro definition is exactly in ISO C(not in
    traditional C though.).

    Best regards,
    Lihao
    , Apr 22, 2007
    #3
  4. Mirco Wahab Guest

    wrote:
    > #define print_int(var) printf( #var " is %d\n", var)
    >
    > then each time I want to print out an interger, I issue:
    >
    > print_int(number);
    >
    > instead of
    >
    > printf("number is %d \n", number);
    >
    > The output string might be very long and used for various variables,
    > so I need to wrap it into a subroutine or something else available for
    > this purpose. Can I do this with Perl? Many thank for your hints..


    Thats not a very good idea in Perl, the problem
    here is to take a name and get its value via
    'symbolic reference',like

    ...

    sub print_int {
    no strict 'refs';
    printf "\$$_[0] is %d\n", ${$_[0]}
    }


    our $number = 42;
    print_int('number');

    ...

    These things are not recommended, better
    work around possible problems by simply
    "duplicating" the argument


    ...
    sub print_all { local$"=' is '; print "@_\n" }


    my $number = 42;
    print_all('number', $number);
    ...


    Regards

    M.
    Mirco Wahab, Apr 22, 2007
    #4
  5. Guest

    On Apr 22, 4:23 pm, Mirco Wahab <> wrote:
    > wrote:
    > > #define print_int(var) printf( #var " is %d\n", var)

    >
    > > then each time I want to print out an interger, I issue:

    >
    > > print_int(number);

    >
    > > instead of

    >
    > > printf("number is %d \n", number);

    >
    > > The output string might be very long and used for various variables,
    > > so I need to wrap it into a subroutine or something else available for
    > > this purpose. Can I do this with Perl? Many thank for your hints..

    >
    > Thats not a very good idea in Perl, the problem
    > here is to take a name and get its value via
    > 'symbolic reference',like
    >
    > ...
    >
    > sub print_int {
    > no strict 'refs';
    > printf "\$$_[0] is %d\n", ${$_[0]}
    > }
    >
    > our $number = 42;
    > print_int('number');
    >
    > ...
    >
    > These things are not recommended, better
    > work around possible problems by simply
    > "duplicating" the argument
    >
    > ...
    > sub print_all { local$"=' is '; print "@_\n" }
    >
    > my $number = 42;
    > print_all('number', $number);
    > ...
    >
    > Regards
    >
    > M.


    Hi, Mirco:

    Thank you for your suggestions. do you think I can use a variable
    subroutine argument, like:

    print_int($number);

    instead of a constant argument

    print_int('number');

    to do such things? I am pretty sure that symbolic reference stuff is
    not what I really needed. many thanks.. :)

    Regards,
    Lihao
    , Apr 22, 2007
    #5
  6. Mirco Wahab Guest

    wrote:
    > do you think I can use a variable
    > subroutine argument, like:
    >
    > print_int($number);
    >
    > instead of a constant argument
    >
    > print_int('number');
    >
    > to do such things? I am pretty sure that symbolic reference stuff is
    > not what I really needed. many thanks.. :)


    Not easily - as far as I know (I'm somehow intermediate).
    Another problem here is 'my'-Variables, which don't have
    any entry into the symbol table (names) of the program,
    they use a so called 'local scratchpad'.

    The closest thing I can come up with is something
    with symbolic references - which may work but is
    not encouraged:

    ...
    sub print_all {
    no strict 'refs';
    print "$_[0] is ", ${substr($_[0],1)} if $_[0]=~/^\$/
    }


    our $number = 42;
    print_all q($number);
    ...


    Note the q(...) operator, which doesn't
    evaluate the variable $number before
    the function call.

    As said, you can't use block scoped lexicals
    (my) here, they won't be found in the programs
    symbol table (we use package globals here ==> our).

    Maybe the gurus can help out.

    Regards

    M.
    Mirco Wahab, Apr 22, 2007
    #6
  7. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > Hi, folks:
    >
    > Is there a way in Perl to return the variable name instead of it's
    > values, like in C, we can use the '#' token with macro:
    >
    > #define print_int(var) printf( #var " is %d\n", var)
    >
    > then each time I want to print out an interger, I issue:
    >
    > print_int(number);
    >
    > instead of
    >
    > printf("number is %d \n", number);
    >
    > The output string might be very long and used for various variables,
    > so I need to wrap it into a subroutine or something else available for
    > this purpose. Can I do this with Perl? Many thank for your hints..


    The standard DB module has a feature the allows something similar.
    Put this in a Perl module:

    package Report;
    use strict; use warnings;
    use base 'Exporter';
    our @EXPORT = qw( report);

    {
    package DB;
    sub report {
    for my $expr ( @_ ) {
    my $val = eval $expr;
    $val = " = $val" if defined $val;
    $val = '-invalid-' if $@;
    $val = '-undef-' unless defined $val;
    print "$expr $val\n";
    }
    }
    }

    *report = \ &DB::report;

    1;

    Then in another program you can do this:

    use Report;

    my ( $x, $y) = 123;
    our $z = 456;
    my %h = (
    abc => 789,
    );

    report( qw( $x $y $z $gibsnich $h{abc}));

    That prints

    $x = 123
    $y -undef-
    $z = 456
    $gibsnich -invalid-
    $h{abc} = 789

    Anno
    -berlin.de, Apr 23, 2007
    #7
    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. ding feng
    Replies:
    2
    Views:
    2,789
    ding feng
    Jun 25, 2003
  2. Bobby Chamness
    Replies:
    2
    Views:
    2,379
    Joe Smith
    Apr 22, 2007
  3. mfglinux
    Replies:
    11
    Views:
    684
    Roberto Bonvallet
    Sep 12, 2007
  4. Pete Mahoney

    How to get Variable value not Variable name.

    Pete Mahoney, Jul 24, 2003, in forum: Javascript
    Replies:
    11
    Views:
    253
    Richard Cornford
    Jul 25, 2003
  5. David Filmer
    Replies:
    19
    Views:
    226
    Kevin Collins
    May 21, 2004
Loading...

Share This Page