Fellow monasterians,
This is something very elementary that bugs me everytime I code it, and thought I should nip it here an now. I guess it's more about verbose-looking code than anything and wondering if this is just how things are in Perldom. Here goes:
#!/usr/bin/perl use strict; my $foo = 1; if ($foo) { my $bar = "true"; } print $bar;
Of course, this bit of code results in a strict error, so declare $bar ahead of the conditional...
my $bar; if ($foo) { $bar = "true"; } print $bar;
But now I have a global. So I add braces to reign it in...
{ my $bar; if ($foo) { $bar = "true"; } print $bar; }
Which is usually how I code it, but it seems "ugly." Yes, I know that is one of the features of Perl, and this might all sound like whining, but is there a better way? I guess what I'm thinking should be possible is:
if ($foo) { my $bar = "true"; } print $bar;
Thanks. I feel better having finally asked this.
In reply to A cleaner way of scoping variables by bradcathey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |