l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
April 21: Google Glass
Next Installfest:
TBD
Latest News:
Mar. 18: Google Glass at LUGOD's April meeting
Page last updated:
2001 Dec 30 17:04

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] perl question - variable holding a variable name
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] perl question - variable holding a variable name



I was talking about PHP, actually (not Python).  In PHP, you can access
global variables directly by using the variable "$GLOBALS", like:

   $GLOBALS["James"] = "fool";

which sets the variable $James to the string "fool" (You can also read
global variables that way).  It's a nice feature and comes in very handy
in a situation like Peter's.  I'm personally writing a parser (or *was*
writing a parser until I decided to put it off) in PHP that can access PHP
variables directly, and it's been a valuable tool.  PERL's interface seems
to be a little bit complex but it sounds like you can write a function for
it:

   &setGlobalVar("James", "fool");
   &setGlobalArray("JamesArray", @foolArray);
   &setGlobalHash("JamesHash", #foolHash);
   $JamesCopy = &getGlobalVar("James");
   @JamesArrayCopy = &getGlobalArray("JamesArray");
   #JamesHashCopy = &getGlobalHash("JamesHash");

or whatever...  anyway, I'm not a perl person so perhaps someone else can
come up with a better interface and actually write such a code...!

-Mark

On Mon, 9 Apr 2001, Micah Cowan wrote:

> On Mon, Apr 09, 2001 at 04:38:42PM -0700, Mark K. Kim wrote:
> > Oops!  My bad.  I didn't realize the hash you were referring to was a
> > global variable hash -- I thought you were telling Peter to create a hash
> > to store the variables for his language.  So how would you access such a
> > hash in PERL?
>
> I wasn't talking about the global variable hash - but then I don't
> know Python, so I don't know what you meant by automatic hashing.
> Perl /does/ store globals in a hash, but it doesn't matter, because
> whether it does or doesn't, you still are mucking with the program's
> symbol table when you're doing this sort of thing.
>
> The hash is named %<package-name>:: , and the values are /typeglobs/.
> So, you use ${$main::James} to access the global variable $James,
> and you use @{$main::James} to access the global variable @James.
>
> No big deal so far, except that you can also /set/ values in this
> hash, which allows you to pull some pretty slick tricks with the
> symbol table.  I am not very familiar with these tricks, so I'll end
> here.  I wouldn't really recommend using them, anyway.  Among other
> things, though, it's a possible way of implementing constants.  But
> that's what "use constant" is for, anyhow.
>
> Micah
>

---
Mark K. Kim
http://www.cbreak.org/mark/
PGP key available upon request.


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.