mnperez has asked for the wisdom of the Perl Monks concerning the following question:

I have two perl programs, one is defining a hash (%oid) and passing a portion of it to a function in another module.

Both are using strict, the first, test.pl, seems to work fine -- the second, base.pm is where I am having difficulties.

The object is to pass a portion of the hash through to a function in the base module and have the base module create a variable, $oid_<itemfromhash>. When using strict and predeclaring all the possible variables that will be created at the top of the module, printing the variables after they are set yields nothing.

If I take off strict and either don't predeclare or predeclare with local(), I get data in the variables.

What am I doing wrong?

Files attached, thanks.

- Mike

test.pl: use lib qw(.); use base; use strict; my %OID = ( 'myoid' => { 'name' => 'myoid', 'type' => 'string', }, 'myoid2' => { 'name' => 'myoid2', 'type' => 'string2', }, ); my $buh = "myoid"; test($OID{$buh}); base.pm: use Exporter; use strict; no strict 'refs'; use vars qw(@EXPORT @ISA @EXPORT_OK); @EXPORT = qw(test); @ISA = qw(Exporter); my $oid_name; my $oid_type; sub test { my ($in) = @_; foreach my $item (keys %{ $in }) { print "(ITM) -> $item\n"; print "(VAL) -> $in->{$item}\n"; ${ "oid_$item" } = $in->{$item}; } print "oid_name is '$oid_name'\n"; print "oid_type is '$oid_type'\n"; } 1;

Replies are listed 'Best First'.
Re: Odd problem with referencing
by jynx (Priest) on Mar 15, 2001 at 01:49 UTC

    Well, for starters you're trying to use a string as a variable name under strict. That's not allowed. There are security issues with allowing strings as variable names, so strict croaks telling you that it doesn't like it.

    Personally, unless i'm trying to write a namespace-polluting import function, i stay away from using strings as variable names (and namespace-polluting is frowned upon). My guess is that you're doing this to try to be lazy with variable names. My advise is to not be lazy if that is the case. You'll probably create more problems than you'll solve.

    Hope This Helps,
    jynx

Re: Odd problem with referencing
by arturo (Vicar) on Mar 15, 2001 at 02:41 UTC

    Whenever you find yourself trying to get around strict 'refs' by using variables with dynamically generated names, ask yourself whether you wouldn't be better off using a hash instead (the answer is almost always that you are). E.g. in base.pm, rewrite the last bit as:

    my %values; foreach my $item (keys %$in ) { print "(ITM) => $item\n"; print "(VAL) => ", $in->{$item}, "\n"; $values{"oid_$item} = $in->{$item}; } foreach my $val ( keys %values ) { print "$val is $values{$val}\n"; }

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor