Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: How to localize conditionally?

by JavaFan (Canon)
on Mar 13, 2011 at 20:40 UTC ( [id://892994]=note: print w/replies, xml ) Need Help??


in reply to How to localize conditionally?

local $ENV{BLAH} = condition ? 42 : $ENV{BLAH};

Replies are listed 'Best First'.
Re^2: How to localize conditionally?
by ikegami (Patriarch) on Mar 14, 2011 at 14:28 UTC
    The library checks for existence, so that doesn't work. You'd need
    local %ENV = condition ? (%ENV, BLAH=>42) : %ENV;

      Did you test it? It works for me.

      - tye        

        No, but how can one assign to a hash element that does not exist? Well, %ENV is magical, so let's test it out.

        $ perl -wE' my $condition = 0; say exists($ENV{BLAH}) || 0; local $ENV{BLAH} = $condition ? 42 : $ENV{BLAH}; say exists($ENV{BLAH}) || 0; ' 0 1

        So I was right

        This is perl 5, version 12, subversion 2 (v5.12.2) built for i686-linu +x

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://892994]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-25 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found