How to get the variable name, not values?

L

lihao0129

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
 
J

Jürgen Exner

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
 
L

lihao0129

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
 
M

Mirco Wahab

#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.
 
L

lihao0129

#define print_int(var) printf( #var " is %d\n", var)
then each time I want to print out an interger, I issue:

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
 
M

Mirco Wahab

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.
 
A

anno4000

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top