no strict 'refs' need help

B

bpatton

I'm trying to get this function to work. It's a snippett of my
original code, but functional. I'm at home and don't have my perl
book with me to look it up.
Here's the code.
package foo;
use strict;
use warnings;
use Data::Dumper;
use vars qw ( $BY $INTO $VALUE );


my %input = ( by => 'by' ,
into => 'into' ,
value => 'value',
);

setKnownVariables(\%input);
print "by = $::BY\n";
print "into = $::INTO\n";
print "value = $::VALUE\n";

sub setKnownVariables($) {
my ($input) = @_;
my ($var,$uvar);
foreach $var (
'allow45',
'by',
'coincidenceOK',
'comment',
'filter',
'into' ,
'layer',
'lineEnd',
'lpad',
'lpadRedundant',
'metalSys' ,
'overlap',
'overlapOK',
'process' ,
'ruleGroupNumber' ,
'ruleName',
'sideLength',
'sideSpace',
'square',
'technology' ,
'touching',
'value',
'width',
) {
no strict 'refs';
$uvar = $var;
$uvar =~ tr/[a-z]/[A-Z]/;
if (exists $input->{$var}) {
$::${$uvar} = $input->{$var};
delete $input->{$var};
} else {
$::{$var} = undef;
}
}
}
 
B

Brian McCauley

I'm trying to get this function to work.

In what way does it fail?
I'm at home and don't have my perl
book with me to look it up.

Do you have a computer with Perl on it? If so you could use the
standard reference manuals that are installed as part of Perl.
{
no strict 'refs';
$uvar = $var;
$uvar =~ tr/[a-z]/[A-Z]/;
if (exists $input->{$var}) {
$::${$uvar} = $input->{$var};
delete $input->{$var};
} else {
$::{$var} = undef;
}
}

A non-existent element of a hash is undef anyhow.

Did you really mean to create the upper case variable if the element
in %input was there and the lower case one if not? I'm guessing not.

There's a built-in uppercase function.

$::${$uvar} is not valid syntax.

{
no strict 'refs';
${"::\U$var"} = delete $input->{$var};
}

BTW: You need to have a damn good reason to be playing with symrefs in
the main:: namespace. I'd guess it's ~90% probable that you don't.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top