Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic scalar reference and "strict"

Status
Not open for further replies.

lax4mike

Programmer
May 10, 2005
6
US
Hi-

I'm trying to get the value of scalars dynamicly using an array. However, I have to be using strict.

This following code is exactly what I want, except this codes doesn't use "strict"
Code:
$month = 5;
$day = 7;
$year = 2005;

my @list = qw( month day year );

foreach my $name (@list){
	print ${$name};
}
I give it the name of the scalar, then it retreives the value.

This code is the same, but it uses strict and doesn't work. I get the error: "Can't use string ("month") as a SCALAR ref while "strict refs" in use"
Code:
use strict;

my $month = 5;
my $day = 7;
my $year = 2005;

my @list = qw( month day year );

foreach my $name (@list){
	print ${$name};
}

sooo, is there a way to get around all this "my" stuff while still using strict?
 
Using variables as variable names, as you're doing, is widely regarded to be a Bad Thing - (see this link for more info on that). If you have variables that are linked in some way (and where it might make sense to use variable variable names), you're better off to use a hash to store them:
Code:
my %date = (
   month => 5,
   day => 7,
   year => 2005
);

foreach my $name ( keys %date ) { # or you can use your @list foreach loop here either
   print $date{ $name };
}
 
I'm getting these variables from cgi->param:
Code:
my $month = $cgi->param('month') || $cgi->url_param('month');
and I have a million of those. so it would be a lot of typing to put those all in a hash. I'm just trying to think of an easier way to do this.

There are two spots where i want to use this. first is to make a parameter string to throw at the end of a window.location to go to step 2 of the input form. e.i. ?month=5&day=7&year=2005.
something like this:
Code:
$param_string .= "&$name=${$name}";

then, the step2.cgi can print them all via the <input type=hidden> tag so they get submitted along with the form. so inside the loop, I could put:
Code:
 print "<input type=hidden name=$name value=${$name}>";
 
ok, here is another approach... can i read the name of a variable? I changed the list from quoted names, to the variables themselves:
Code:
use strict;

my $month = 5;
my $day = 7;
my $year = 2005;

my @list =  ( $month, $day, $year );

foreach (@list){
	print $_;
}[code]

now is there a way for it to print "month", "day", and "year"?
 
It seems to me you already have the control you want and you haven't noticed. You don't need the variables at all if you want to use the array (although I'm not sure this is a wise approach).

Code:
my @list = qw/month day year/;
foreach (@list) {
  print $cgi->param($_);
}

If you need the data in a var for string manipulation or something you can assign it to a temporary scalar:

Code:
my @list = qw/month day year/;
foreach (@list) {
  my $tempvar = $cgi->param($);
  print "Some string: $tempvar\n";
}


Trojan

 
well, i have all the data in their own scalars and they are used throughout the (big) cgi. i just need to package them up and ship them to the next cgi at the end. it looks like i'm just going to have to make a giant hash and then loop through the hash.
Code:
use strict;

my $month = 5;
my $day = 7;
my $year = 2005;

my %list =  ( 	"month" => $month,
		"day" 	=> $day,
		"year" 	=> $year );

foreach (keys %list){
	print "<input type=hidden name=$_ value=$list{$_}>\n";
}
just like that, except a lot more variables... i just figured it's way easier to type out a list than to type out a hash because i have so many variables.
 
If you really have to do it that way, why don't you write a little perl script to generate the code for you?


Trojan
 
i found a solution:
Code:
my @param = $cgi->param;
now i can loop through via @param
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top