in reply to A cleaner way of scoping variables

Use indentation to remove ugliness:
{ my $bar; if ($foo) { $bar = "true"; } print $bar; }
I suppose you could hide $bar in a subroutine:
sub test { my $arg = shift; my $bar; if($arg) { $bar = "true"; } return $bar; } my $foo = 1; print test($foo);
But that's an extreme solution.

Update:
This approach eliminates the global and the curlies:

use strict; my $foo = 1; if ($foo) { package Strange; our $bar = "true"; } print $Strange::bar || "false";

:-)

Replies are listed 'Best First'.
Re^2: A cleaner way of scoping variables
by hmerrill (Friar) on Aug 09, 2004 at 13:54 UTC
    Excellent ideas!

    I humbly submit that the way perl does it is correct - the original op just needs to get used to doing it the "right" way. I can't see how variable scoping in perl is incorrect in any way.

      Agreed; it was just a little brainstorming exercise.
Re: A cleaner way of scoping variables
by bradcathey (Prior) on Aug 09, 2004 at 16:40 UTC

    Yes, thank you. That last example was especially thought-provoking, though probably not practical in real life conditions. But interesting.


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