in reply to Why is my loop dropping alphabetic strings?

Tip #7 of the Basic debugging checklist gives us more information about what is going on (check $@):
use warnings; use strict; my $NB_ORA_CINC; my $NB_ORA_CLIENT; my @NB_VAR_LIST=("NB_ORA_CINC", "NB_ORA_CLIENT"); my $NB_VAR; $ENV{NB_ORA_CLIENT} = 'client01'; $ENV{NB_ORA_CINC} = 1; foreach $NB_VAR ( @NB_VAR_LIST ) { if ( defined $ENV{$NB_VAR} ) { print "Variable $NB_VAR is $ENV{${NB_VAR}}\n"; eval("\$${NB_VAR} = $ENV{${NB_VAR}}"); warn $@ if $@; } } if ( defined $NB_ORA_CINC ) { print "\$NB_ORA_CINC is $NB_ORA_CINC +\n"; } if ( defined $NB_ORA_CLIENT ) { print "\$NB_ORA_CLIENT is $NB_ORA_CLIE +NT\n"; } __END__ Variable NB_ORA_CINC is 1 Variable NB_ORA_CLIENT is client01 Bareword "client01" not allowed while "strict subs" in use at (eval 2) + line 1. $NB_ORA_CINC is 1
I have no idea what that means, but it is a clue for more experienced eval monk.

Replies are listed 'Best First'.
Re^2: Why is my loop dropping alphabetic strings?
by aaron_baugher (Curate) on Jun 23, 2012 at 01:26 UTC

    On the second trip through the loop, eval is passed this interpolated string:

    $NB_ORA_CLIENT = client01

    That's the bareword that strict is complaining about. When Perl sees a bareword, it looks to see if it's a subroutine name (which is why "strict subs" applies to it) and tries a few other possibilities (like is it being used as a hash key). If it can't figure out what it's supposed to be, it falls back to treating it as a string (like a shell does). So the string "client01" gets assigned to the variable by eval, but Perl isn't happy about it.

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      The problem is Perl isn't assigning it to the variable. The if statements at the end are there to check and make sure the values were assigned to the variable. In the case of 'client01' it's failing while with a numeric value the variable is being defined. Is there better way of assigning the variable other than using the eval statement?

        Sorry, I wasn't clear: since you are using strict, that prevents Perl from treating the bareword as a string. Since you're doing it within an eval, your program doesn't die as it would if you did the same thing in non-eval code. It just fails to work. The post I replied to showed how to see any errors happening within the eval, so you can see why it's not working.

        The numeric assignment works because numbers don't ever have to be quoted like strings. Your eval sees two assignments, like these:

        $foo = 1; # no problem $bar = string; # bareword, fails under 'strict subs'

        And as Grandfather said, unless you're doing this as an exercise to learn how eval works, it's probably a really, really bad idea. Unless you have a very good reason that these values need to be in scalar variables instead of the %ENV hash (and there's nothing in your example to indicate that), just access the values from the hash where they belong. Anywhere that you're planning to use $FOO, there's no reason you can't use $ENV{FOO}.

        Aaron B.
        Available for small or large Perl jobs; see my home node.