For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > May 2005 > dynamic scalar values and "strict"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author dynamic scalar values and "strict"
duffman

2005-05-09, 4:12 pm

hi-

i have a bunch of variables that i get data into via cgi. because i'm using strict, they all have "my" infront of them.

what i'm trying to do is create a string that i can pass to another cgi of all the variables. eg ?&month=$month&day=$day&year=$year.

now, i was thinking i could very easily just create an array with the names of the scalars, and loop though and put the values in the string. something like this:
PHP:

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

my $params "?";

my @list = qwmonth day year );
foreach 
my $name (@list){
    
$params .= "&".$name."=".${$name};
}
print 
$params;



first of all, because of the "my"'s, the values aren't visible using this dynamic method or some reason. saying print $month inside the foreach works, but ${$name} does not, for some reason...
secondly, when i run it using "use strict", i get an error:

"Can't use string ("month") as a SCALAR ref while "strict refs" in use"

are there ways to get around this?
displeaser

2005-05-23, 9:15 am

Hi,

The following works as you appear to want it.

use strict;
use warnings;
my $month = 5;
my $day = 7;
my $year = 2005;

my $params = "?";

my @list = qw( month day year );

foreach my $name (@list){
$params .= "\&".$name."=\$".$name;
print "$params\n";
}
print $params;

#wanted: ?&month=$month&day=$day&year=$year
#printed: ?&month=$month&day=$day&year=$year
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com