in reply to eval, Read environment variable and expand using eval

it didn;t work
Please elaborate. It seems to work for me when I run your 2nd code example. Here is the output I get:
$r = (defined $ENV{HOST} ? $ENV{HOST} : 'sqlhost') sqlhost
What do you expect the output to be? First, you print the statement verbatim, then you print the eval'd statement. I do not have the HOST variable set.

Replies are listed 'Best First'.
Re^2: eval, Read environment variable and expand using eval
by tart (Beadle) on Feb 19, 2010 at 04:34 UTC
    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
      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} = "";

      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.