CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
variable names

 



pauls
Novice

Mar 30, 2001, 10:21 PM

Post #1 of 6 (830 views)
variable names Can't Post

Is it possible to have a variable name to include another variable?

IF:
$foo = param('number'); # 0001
# the other variable would be:
$bar$foo = "something";
The second variable would be $bar0001 when called later.

thanks paul



Jasmine
Administrator

Mar 31, 2001, 12:49 AM

Post #2 of 6 (827 views)
Re: variable names [In reply to] Can't Post

The question to ask is if something like this is really necessary? Could a hash work instead?

Here's one way:


Code
${"$bar$foo"} = 123456; 

print ${"$bar$foo"};

Another way that I can think of to do this (and I'm sure there are others) is to use soft references. This will allow you to create variables and use them without explicitely knowing what the user will enter.


Code
$foo = '0001'; 
$bar = 'something';

$orother = "$bar$foo";
# orother contains "something0001"

$$orother = 12345;
# change the value of the reference
# $$orother is now synonymous with $something0001

print $orother;
# prints something0001

print $$orother;
# prints 12345

print $something0001;
# also prints 12345

Notice the 2 $$ in the last two print statements. This dereferences soft references.

Soft references are a fatal error when using strict unless you specifically enable them. For example, if you begin your script with the following:


Code
#!/usr/bin/perl 

use strict;
use vars qw /$foo $bar $orother $something0001/;
no strict 'refs';

soft references are allowed and your script won't die when being executed. Errr, run ;)

One important thing to remember is to perform checks on the user input. I noticed that $foo is being used directly from user input -- normally a bad idea, but even more so when using direct user input to create soft references. So make sure that $foo contains only letters, numbers or _, and does not end in anything but a number or letter.

Hope this helps!



parham_m_s
journeyman

Mar 31, 2001, 6:14 AM

Post #3 of 6 (823 views)
Re: variable names [In reply to] Can't Post

That was a great explanation Jasmine, i was wondering if that would work this morning too. Thanx



pauls
Novice

Mar 31, 2001, 9:23 AM

Post #4 of 6 (819 views)
Re: variable names [In reply to] Can't Post

Jasmine,

I'm currently checking into using a hash but I'm not sure either way will actually do what I need.
but thanks I'll keep trying..
paul



Jasmine
Administrator

Mar 31, 2001, 2:39 PM

Post #5 of 6 (814 views)
Re: variable names [In reply to] Can't Post

If you'll explain what it is you need precisely, we can try to help Smile



pauls
Novice

Mar 31, 2001, 5:37 PM

Post #6 of 6 (810 views)
Re: variable names [In reply to] Can't Post

I need to try it myself first. (it's the way i learn best) But if I get stuck I'll try to explain what I'm doing.
thanks again
paul


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives