in reply to A cleaner way of scoping variables

Two examples that eliminate the need for $bar entirely.

my $foo = 1; print +($foo)? "True" : "";

Or, if it's more involved than that...

my $foo = 1; print do{ if( $foo ) { "True" } }

In your last example, you asserted that it would be nice if $bar were accessible outside of the if(){} block. But you previously stated that you didn't want to use a global variable (in this case what you really meant was a lexical scoped to package level). The problem is that if $bar is accessible outside of the if(){} block, it's no different from a package-scoped lexical. You don't want your lexicals leaking outside of the block they're defined in; that defeats the purpose of a lexical variable.


Dave

Replies are listed 'Best First'.
Re^2: A cleaner way of scoping variables
by bradcathey (Prior) on Aug 09, 2004 at 17:06 UTC
    davido, thanks. Your last paragraph pretty much sums up what I've learned and take away from this thread. See my comments to Crackers2 above.

    —Brad
    "Don't ever take a fence down until you know the reason it was put up. " G. K. Chesterton