Click here to get back home

Get variable from its name string or vice versa?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Get variable from its name string or vice versa? Jerry Krinock 05-09-2008
Posted by Jerry Krinock on May 9, 2008, 1:21 pm
Please log in for more thread options
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


Posted by A. Sinan Unur on May 9, 2008, 1:43 pm
Please log in for more thread options
98d7-64c1bc9ef4e3@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"

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by Jens Thoms Toerring on May 9, 2008, 2:21 pm
Please log in for more thread options
> 98d7-64c1bc9ef4e3@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"

> 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 ___ jt@toerring.de
\__________________________ http://toerring.de

Posted by jerrykrinock on May 9, 2008, 3:38 pm
Please log in for more thread options
On May 9, 11:21=A0am, j...@toerring.de (Jens Thoms Toerring) wrote:

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

Yes, I am.

> 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 =3D $MY_VARS ;

# New Way to assign a Loggable Variable
# Instead of just "$fred =3D 23",
# I now have to write:
$MY_VARS =3D 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 =3D shift ;
my $varValue =3D $MY_VARS ;
if (!defined($varValue)) {
$varValue =3D "<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

Posted by A. Sinan Unur on May 9, 2008, 5:48 pm
Please log in for more thread options
jerrykrinock@gmail.com wrote in news:2643728c-7f39-43d9-8c46-
ca63cee3018e@m73g2000hsh.googlegroups.com:

> 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

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Similar ThreadsPosted
Calling C++ from Perl and vice versa January 22, 2006, 12:08 pm
Can I call Perl module from TCL or vice versa? October 10, 2006, 11:51 pm
PERL ---> Move email from Inbox folder to another or vice versa in OUTLOOK August 4, 2006, 10:39 pm
Using a string as a variable name. October 5, 2004, 10:34 am
setting a variable from a string August 12, 2005, 2:48 am
replace string with variable May 29, 2006, 6:06 am
How to "convert" a string into a variable name? August 2, 2006, 11:58 am
Using a variable in a matching string November 21, 2006, 1:18 pm
How do i get the charactors inside the ( ) from a string variable March 25, 2006, 2:46 pm
Modifying and printing a string variable September 12, 2006, 2:31 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap