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

I am running a script that depends on variables from an external process. I've written a foreach loop that determines if each variable is defined and if so then sets a local variable. The problem is the loop is only setting numeric variables. The non-numeric values are seemingly discarded.

#!/usr/bin/perl # use strict; use warnings; my $NB_ORA_CINC; my $NB_ORA_CLIENT; my @NB_VAR_LIST=("NB_ORA_CINC", "NB_ORA_CLIENT"); my $NB_VAR; 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"; }

The result is:

Variable NB_ORA_CINC is 1 Variable NB_ORA_CLIENT is client01 $NB_ORA_CINC is 1

I'm simulating the external process by defining the variables in a shell script that executes the perl script. I don't understand why the loop appears to be discarding the alphabetic character. Any help would be appreciated. Thanks Update: I added the "warn $@ if $@;" to my loop, thank you for the tip,and Perl is reporting "Bareword "client01" not allowed while "strict subs" in use at (eval 3)". When I comment out 'use strict;' the script completes as expected. At this point I'm not really sure how to remediate the problem so that it works while using 'use strict;'.

Replies are listed 'Best First'.
Re: Why is my loop dropping alphabetic strings?
by GrandFather (Saint) on Jun 23, 2012 at 01:45 UTC

    Whatever you are trying to do, that's not the way to do it! You seem to be trying to perform an end run around using symbolic references, but hiding that in a string eval doesn't actually "fix" the problem, and introduces all the nastiness of string eval on top of the nastiness of symbolic references.

    Instead simply use %ENV wherever you would have used your "local" variables. The content of %ENV is "local" in any case - any changes you make to the content of %ENV only affects your executing script.

    By attempting to copy environment variables to local variables you are introducing two types of nasty coding practise, you are obscuring where the values actually come from, and are making it harder to change the script in the future by requiring the introduction of "local" variables for each environment variable you may want to use. So don't do that.

    The immediate problem with your code is that the right hand side of the assignment operator is whatever the contents of $ENV{$NB_VAR} is. If the contents are not numeric then the rhs of the assignment will be at best a bareword and often will simply be syntactically incorrect.

    True laziness is hard work

      Thank you. I appreciate your help and your advice.

Re: Why is my loop dropping alphabetic strings?
by toolic (Bishop) on Jun 23, 2012 at 00:32 UTC
    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.

      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?

Re: Why is my loop dropping alphabetic strings?
by Jenda (Abbot) on Jun 23, 2012 at 23:41 UTC

    Why don't you just

    my $NB_ORA_CINC = $ENV{NB_ORA_CINC}; my $NB_ORA_CLIENT = $ENV{NB_ORA_CLIENT};

    It's simpler, safer and clearer.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.