Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to get the variable name, not values?

Reply
Thread Tools

How to get the variable name, not values?

 
 
lihao0129@gmail.com
Guest
Posts: n/a
 
      04-22-2007
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

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-22-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
lihao0129@gmail.com
Guest
Posts: n/a
 
      04-22-2007
On Apr 22, 3:50 pm, "Jürgen Exner" <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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



 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      04-22-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
lihao0129@gmail.com
Guest
Posts: n/a
 
      04-22-2007
On Apr 22, 4:23 pm, Mirco Wahab <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      04-22-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      04-23-2007
(E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
How to get Variable value not Variable name. Pete Mahoney Javascript 11 07-25-2003 01:02 AM



Advertisments