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

OK...more of a 'Is it possible to do this' sorta question:

my %required = ( path => if ( $env{'HTTP_HOST'} = 'www.shrum.net' ) { "g:/websites/ +shrum.net/" || "d:/users/http/html/"; }, );

I know that the above won't work but you get the idea.

Is it possible to set up a conditional eval like this or do I need to do this outside the hash declaration?

On the other hand...is there a better way of dealing with pathnames...my code originally was written for Apache but IIS doesn't use environment variables like DOCUMENT_ROOT and so forth.

TIA

======================
Sean Shrum
http://www.shrum.net

Replies are listed 'Best First'.
Re: How do I...Conditional hash value
by myocom (Deacon) on Jul 26, 2002 at 22:20 UTC

    First off, beware the "= vs. eq vs. ==" bug. Remember that = does an assignment, == compares numeric values, and eq compares string values. Having said that, what you're probably looking for is the ternary ?: operator (documented in perlop). I'd write what you're looking for like this:

    my %required; $required{path} = ($ENV{HTTP_HOST} eq 'www.shrum.net') ? 'g:/websites/shrum.net/' : 'd:/users/http/html/';

    Then again, if you have several keys to set, you're probably better off doing:

    my %required; if ($ENV{HTTP_HOST} eq 'www.shrum.net') { # We now know which # platform we're on... $required{path} = 'g:/websites/shrum.net/'; $required{someotherkey} = 'some other string'; } else { $required{path} = 'd:/users/http/html/'; $required{someotherkey} = 'some different string'; }
    "One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison
Re: How do I...Conditional hash value
by dws (Chancellor) on Jul 26, 2002 at 22:09 UTC
    Would this work?
    my %required = ( path => ($ENV{'HTTP_HOST'} eq "www.shrum.net") ? "g:/websites/shrum.net/" : "d:/users/http/html/" );

      Yes that should work... Also see:

      use Data::Dumper; my $i1 =1; my %hash = (k1 => {k2 => ($i1 ? 'this' : 'that') }); print Dumper(\%hash) . "\n"; # this $i1 = 0; %hash = (k1 => {k2 => ($i1 ? 'this' : 'that') }); print Dumper(\%hash) . "\n"; # that
      --Div