Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Get variable from its name string or vice versa?

Reply
Thread Tools

Get variable from its name string or vice versa?

 
 
Jerry Krinock
Guest
Posts: n/a
 
      05-09-2008
I have written a function to log variables like this:

" varName: varValue"

but it takes two arguments: the variable name as a string, and the
variable symbol:

logVar ("myVar", $myVar) ;

sub logVar {
my $varName = shift ;
my $varValue = shift ;
if (!defined($varValue)) {
$varValue = "<undef>" ;
}
printf ("%16s: %s\n", $varName, $varValue) ;
}

Is there any way to get "myVar" from $myVar or vice versa, without
foregoing 'strict' and 'warnings'?

Note: My actual code uses Std::Log but the problem is the same.

Thanks,

Jerry Krinock

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-09-2008
Jerry Krinock <> wrote in news:a2761c7e-8820-4f16-
98d7-:

> I have written a function to log variables like this:
>
> " varName: varValue"


This is a FAQ:

perldoc -q "How can I use a variable as a variable name"

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/
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-09-2008
A. Sinan Unur <> wrote:
> Jerry Krinock <> wrote in news:a2761c7e-8820-4f16-
> 98d7-:


> > I have written a function to log variables like this:
> >
> > " varName: varValue"


> This is a FAQ:


> perldoc -q "How can I use a variable as a variable name"


> Sinan


I think the OP is looking for something a bit different, i.e.
a way to get the name of variable from the variable itself.
I.e. some hypothetical code like

my $x = 10;
logvar( \$x );

sub logvar {
my $varref;
print get_name_from_reference( $var ) . " " . $$var . "\n";
}

It's clear that for something like this to work a reference to
the variable has to be passed to the function. But I have no
idea how to write a function like get_name_from_reference()
and also have my doubts that it is possible at all (but then
this is Perl and there's so much magic that it is hard to be
sure
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
advice please wireless 802.11 on RH8
Guest
Posts: n/a
 
      05-09-2008
On May 9, 1:21 pm, Jerry Krinock <je...@sheepsystems.com> wrote:
> I have written a function to log variables like this:
>
> " varName: varValue"
>
> but it takes two arguments: the variable name as a string, and the
> variable symbol:
>
> logVar ("myVar", $myVar) ;
>
> sub logVar {
> my $varName = shift ;
> my $varValue = shift ;
> if (!defined($varValue)) {
> $varValue = "<undef>" ;
> }
> printf ("%16s: %s\n", $varName, $varValue) ;
>
> }
>
> Is there any way to get "myVar" from $myVar or vice versa, without
> foregoing 'strict' and 'warnings'?
>
> Note: My actual code uses Std::Log but the problem is the same.
>
> Thanks,
>
> Jerry Krinock


sounds like you need the (non-existent) Perl func uneval()

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      05-09-2008

Quoth Jerry Krinock <>:
> I have written a function to log variables like this:
>
> " varName: varValue"
>
> but it takes two arguments: the variable name as a string, and the
> variable symbol:
>
> logVar ("myVar", $myVar) ;
>
> sub logVar {
> my $varName = shift ;
> my $varValue = shift ;
> if (!defined($varValue)) {
> $varValue = "<undef>" ;
> }
> printf ("%16s: %s\n", $varName, $varValue) ;
> }
>
> Is there any way to get "myVar" from $myVar or vice versa, without
> foregoing 'strict' and 'warnings'?


You can do this with PadWalker. If you need more hints than that, you
probably shouldn't be trying...

Ben

--
I have two words that are going to make all your troubles go away.
"Miniature". "Golf".
[]
 
Reply With Quote
 
jerrykrinock@gmail.com
Guest
Posts: n/a
 
      05-09-2008
On May 9, 11:21*am, j...@toerring.de (Jens Thoms Toerring) wrote:

> I think the OP is looking for something a bit different...


Yes, I am.

On May 9, 10:43 am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> This is a FAQ:
>
> perldoc -q "How can I use a variable as a variable name"


I'd read that, and now studied it further, but I get the impression
that the only solution is to always define all of my variables in my
own hash, just in case I ever decided that I wanted to log one of
them.

That's not going to be any fun, and not very readable by earthlings.
Indeed, this code works:

#!/usr/bin/perl
use strict ;
use warnings ;

my %MY_VARS ;

# New Way to declare a Loggable Variable...
# Instead of just "my $fred",
# I now have to write:
my $fred = $MY_VARS{"fred"} ;

# New Way to assign a Loggable Variable
# Instead of just "$fred = 23",
# I now have to write:
$MY_VARS{"fred"} = 23 ;

# Well, after all that yuck, indeed, as desired,
# I can log it with only one argument, :

logVar("fred") ;

# Using this handy function
sub logVar {
my $varName = shift ;
my $varValue = $MY_VARS{$varName} ;
if (!defined($varValue)) {
$varValue = "<undef>" ;
}
printf ("%32s: %s\n", $varName, $varValue) ;
}

But it's hardly worth all that massive obfuscation. Can someone
confirm that there is indeed no way to simply log a "regular" variable
and its name without having to type both of them, or is Jens correct
that it can't be done?

Jerry
 
Reply With Quote
 
nolo contendere
Guest
Posts: n/a
 
      05-09-2008
On May 9, 1:43*pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> Jerry Krinock <je...@sheepsystems.com> wrote in news:a2761c7e-8820-4f16-
> 98d7-64c1bc9ef...@k13g2000hse.googlegroups.com:
>
> > I have written a function to log variables like this:

>
> > * * *" * * * * varName: varValue"

>
> This is a FAQ:
>
> perldoc -q "How can I use a variable as a variable name"
>


I don't think this is quite it...perhaps the Debugger's DumpPackages?
This displays the symbol tables of packages.
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-09-2008
wrote in news:2643728c-7f39-43d9-8c46-
:

> On May 9, 11:21*am, j...@toerring.de (Jens Thoms Toerring) wrote:
>
>> I think the OP is looking for something a bit different...

>
> Yes, I am.


I jumped the gun, sorry!

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/
 
Reply With Quote
 
jerrykrinock@gmail.com
Guest
Posts: n/a
 
      05-10-2008
On May 9, 12:37 pm, Ben Morrow <b...@morrow.me.uk> wrote:

> You can do this with PadWalker.


Thank you. I haven't tried it yet, but documentation of PadWalker
from CPAN declares a var_name() function that seems to be exactly what
I want.

Apparently this was a tricky feat. From the documentation: "I
wouldn't recommend using PadWalker directly in production code, but
it's your call. Some of the modules that use PadWalker internally are
certainly safe for and useful in production."

And from a review: "PadWalker is really, really useful if you need to
debug something really, really weird. I hope you never have to use
this module, but if you do, use it boldly."



Thanks for all the help. I'll probably give PadWalker a try next time
I am feeling bold, and have a little time to spare before production.

Jerry Krinock

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-10-2008
>>>>> "j" == jerrykrinock <> writes:

j> On May 9, 12:37 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>> You can do this with PadWalker.


j> Thank you. I haven't tried it yet, but documentation of PadWalker
j> from CPAN declares a var_name() function that seems to be exactly what
j> I want.

j> Apparently this was a tricky feat. From the documentation: "I
j> wouldn't recommend using PadWalker directly in production code, but
j> it's your call. Some of the modules that use PadWalker internally are
j> certainly safe for and useful in production."

j> And from a review: "PadWalker is really, really useful if you need to
j> debug something really, really weird. I hope you never have to use
j> this module, but if you do, use it boldly."

j>

j> Thanks for all the help. I'll probably give PadWalker a try next time
j> I am feeling bold, and have a little time to spare before production.

and i doubt you have a legit reason to need this in production code. i
smell an XY problem here. when someone needs to use such a dark magic
solution, i say the problem is poorly specified or similar. tell us what
the real problem is, don't ask how to do something requiring magic. i
can find no good reason (other than debugging or wacko stuff) for
needing the name of a variable. and if you have anon references, you
can't get any name.

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
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
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Determining method type given its string name presentation and its corresponding object reference. Apple Python 3 08-01-2005 03:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57