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 | |
by DiabeticJim (Initiate) on Jun 23, 2012 at 02:20 UTC | |
|
Re: Why is my loop dropping alphabetic strings?
by toolic (Bishop) on Jun 23, 2012 at 00:32 UTC | |
by aaron_baugher (Curate) on Jun 23, 2012 at 01:26 UTC | |
by DiabeticJim (Initiate) on Jun 23, 2012 at 01:42 UTC | |
by aaron_baugher (Curate) on Jun 23, 2012 at 02:22 UTC | |
|
Re: Why is my loop dropping alphabetic strings?
by Jenda (Abbot) on Jun 23, 2012 at 23:41 UTC |