Home > Archive > PERL Miscellaneous > April 2007 > How to get the variable name, not values?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to get the variable name, not values?
|
|
| lihao0129@gmail.com 2007-04-22, 10:01 pm |
| 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ürgen Exner 2007-04-22, 10:01 pm |
| lihao0129@gmail.com 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
| |
| lihao0129@gmail.com 2007-04-22, 10:01 pm |
| On Apr 22, 3:50 pm, "J=FCrgen Exner" <jurge...@hotmail.com> wrote:
> lihao0...@gmail.com wrote:
>
>
>
> 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
| |
| Mirco Wahab 2007-04-22, 10:01 pm |
| lihao0129@gmail.com 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.
| |
| lihao0129@gmail.com 2007-04-22, 10:01 pm |
| On Apr 22, 4:23 pm, Mirco Wahab <wahab-m...@gmx.de> wrote:
> lihao0...@gmail.com wrote:
>
>
>
>
>
>
> 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
| |
| Michele Dondi 2007-04-22, 10:01 pm |
| On 22 Apr 2007 13:02:35 -0700, "lihao0129@gmail.com"
<lihao0129@gmail.com> wrote:
[...][color=darkred]
>This '#' token with macro definition is exactly in ISO C(not in
>traditional C though.).
Well, that it is defined in the same standard as ISO C doesn't mean
that what Juergen wrote is incorrect, i.e. that it is still a feature
of the preprocessor rather than of the compiler. In Perl you generally
don't use a preprocessor because one doesn't feel the need for it as
much as with C. Now, the situation you have in mind may be one in
which it could actually be useful. However, I suppose that with some
intricate trickery it may actually be possible to do what you want in
current Perl. Not everyday's job anyway. And I don't know if dedicated
modules exist to do that. OTOH Perl 6 is supposed to support real
macros. Perhaps they will make it easier to do "this kinda things".
However you will have to wait for the real beast to come alive...
OTOH, you could adopt an intermediate work around by means of string
eval(). But since string eval() is Bad(TM), I recommend you do some
strict check on the input. If it bothers you to have to specify a
string, you may want to write a source filter. Since the task in this
sense is relatively easy, you could use Filter::Simple.
HTH,
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
| |
| Mirco Wahab 2007-04-22, 10:01 pm |
| lihao0129@gmail.com 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.
| |
| anno4000@radom.zrz.tu-berlin.de 2007-04-23, 4:03 am |
| lihao0129@gmail.com <lihao0129@gmail.com> 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
| |
| Michele Dondi 2007-04-28, 8:01 am |
| On Sun, 22 Apr 2007 23:28:31 +0200, Mirco Wahab <wahab-mail@gmx.de>
wrote:
>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'.
But then there's PadWalker.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
|
|
|
|
|