in reply to Re: eval, Read environment variable and expand using eval
in thread eval, Read environment variable and expand using eval

There must be something which I don't know, In second code the output I am getting is,
$r = (defined $ENV{INFORMIXSQLHOSTS} ? $ENV{INFORMIXSQLHOSTS} : 'sqlhost')
But I am expecting,
sqlhost
which I am getting from first code. Cheers, Thanks
  • Comment on Re^2: eval, Read environment variable and expand using eval

Replies are listed 'Best First'.
Re^3: eval, Read environment variable and expand using eval
by ikegami (Patriarch) on Feb 19, 2010 at 07:03 UTC
    You should be getting that since you're printing $stmt. You should *also* be getting the value of $ENV{HOST} or sqlhost.
      Remember that an environment variable can be defined yet empty. In sh:
      MYVAR=
      In perl:
      $ENV{MYVAR} = "";
Re^3: eval, Read environment variable and expand using eval
by bot403 (Beadle) on Feb 19, 2010 at 14:58 UTC

    There is a subtle yet important difference between defined and exists on hash and array elements. Defined will autovivify your hash element and make its value undefined.

    Perhaps you meant this?

    $r = (exists $ENV{INFORMIXSQLHOSTS}) ? $ENV{INFORMIXSQLHOSTS} : 'sqlho +st')

    If you want to check every possiblity this would be the best. However I usually just do the exists() check.

    $r = ((exists $ENV{INFORMIXSQLHOSTS} && defined($ENV{INFORMIXSQLHOSTS} +) && length($ENV{INFORMIXSQLHOSTS}) ) ? $ENV{INFORMIXSQLHOSTS} : 's +qlhost'));
      No, defined does not autovivify
      my %f; warn snot => defined $f{snot}; warn snot => exists $f{snot}; __END__ snot at - line 3. snot at - line 4.
        Darnit. I thought defined() autovivified simple hashes. Both exists and defined still can autovivify on deep hashes though.
        #!/usr/bin/perl # use warnings; use Data::Dumper; my $h = { 'foo' => 1 }; if(exists($h->{notexist}{baz})){ } if(defined($h->{notdefined}{baz})){ } print Dumper($h); exit 0;
        $VAR1 = { 'notexist' => {}, 'foo' => 1, 'notdefined' => {} };