strict ref problem

D

Dave

Hi,

When I run the code below, I get the following error message "Can't use
string ("postf1") as a SCALAR ref while "strict refs" in use at
test.cgi line 20"

I searched google, this group's archives, and perldoc. I found a couple
of articles in this group's archives that I think could apply, but I
did not entirely understand them.

Could someone let me know what I need to do in order to dynamically
load variable names and then reference them? In the example below @rows
actually represents a call to a database field which could have an
infinate number of lines, separated by \n. The field is split using \n
and then variables assigned to each part of each row. Is there a
different way to do that?

Thanks,
Dave

-------------------

#!/usr/bin/perl
use strict;
use warnings;

my @rows=('f1|v1','f2|v2');
my ($postf1,$postv1,$postf2,$postv2);

my $rowcount=@rows;

if ($rowcount>0)
{
my $x=0;
foreach my $row(@rows)
{
$x++;
my $field="postf$x";
my $value="postv$x";
my ($name,$content)=split(/\|/,$row);
${$field}=$name;
${$value}=$content;
}
}

print "f1: " . $postf1 . " v1: " . $postv1 . "\n";
print "f2: " . $postf2 . " v2: " . $postv2 . "\n";
 
D

Dave Weaver

Hi,

When I run the code below, I get the following error message "Can't use
string ("postf1") as a SCALAR ref while "strict refs" in use at
test.cgi line 20"

That is because you are trying to use "symbolic references" (i.e. use
the value of a variable as another variable's name) - this is a Bad
Thing, for many reasons.

See `perldoc -q "variable name"` for a discussion of the issue.

Could someone let me know what I need to do in order to dynamically
load variable names and then reference them?

Use that most handy of Perl constructs, a hash; you don't need to
create a variable called 'postf1', just use a hash entry that has the
key 'postf1';
#!/usr/bin/perl
use strict;
use warnings;

my @rows=('f1|v1','f2|v2');
my ($postf1,$postv1,$postf2,$postv2);

my $rowcount=@rows;

if ($rowcount>0)

You could have replaced those 2 lines with:
if ( @rows )

but the check is redundant anyway, since the foreach loop below won't
be iterated anyway if @rows is emtpy
{
my $x=0;
foreach my $row(@rows)
{


#!/usr/bin/perl
use strict;
use warnings;

my @rows = qw( f1|v1 f2|v2 );
my %var;

for my $x ( 1 .. @rows ) {
($var{"postf$x"}, $var{"postv$x"}) = split /\|/, $rows[ $x - 1 ];
}

use Data::Dumper;
print Dumper \%var;
 
A

Anno Siegel

Dave Weaver said:
[...]
Could someone let me know what I need to do in order to dynamically
load variable names and then reference them?

Use that most handy of Perl constructs, a hash; you don't need to
create a variable called 'postf1', just use a hash entry that has the
key 'postf1';
[...]

my @rows = qw( f1|v1 f2|v2 );
my %var;

for my $x ( 1 .. @rows ) {
($var{"postf$x"}, $var{"postv$x"}) = split /\|/, $rows[ $x - 1 ];
}

use Data::Dumper;
print Dumper \%var;

Still simpler:

my %var = map "post$_", map split( /\|/), @rows;

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top