T
Tim McDaniel
I just had a case where there's a block of code, I split up the work
into assignments to intermediate variables, but I only wanted the
final value. I very much like to restrict the scope of variables,
because it's then obvious what's a temporary and what has more
significance -- it's like an intermediate stage between inline code
and a sub. How I coded it was
my $permanent_variable;
{
my $this = ...;
my $that = ... $this ...;
yadda yadda;
$permanent_variable = ... final computation ...;
}
It occurred to me that I could code it as
my $permanent_variable = do {
my $this = ...;
my $that = ... $this ...;
yadda yadda;
... final computation ...;
};
It does do the scope encapsulation that I like, and it makes it
vividly obvious that the block has one purpose, to set
$permanent_variable.
I looked thru the codebase at work and found a few instances of it.
But mostly "do" was used to implement a slurp function, or otherwise
to "local()" a variable for one statement or call.
For people who have looked at lots of different codebases: do people
think that
$var = do { several statement; }
is just an odd construct?
into assignments to intermediate variables, but I only wanted the
final value. I very much like to restrict the scope of variables,
because it's then obvious what's a temporary and what has more
significance -- it's like an intermediate stage between inline code
and a sub. How I coded it was
my $permanent_variable;
{
my $this = ...;
my $that = ... $this ...;
yadda yadda;
$permanent_variable = ... final computation ...;
}
It occurred to me that I could code it as
my $permanent_variable = do {
my $this = ...;
my $that = ... $this ...;
yadda yadda;
... final computation ...;
};
It does do the scope encapsulation that I like, and it makes it
vividly obvious that the block has one purpose, to set
$permanent_variable.
I looked thru the codebase at work and found a few instances of it.
But mostly "do" was used to implement a slurp function, or otherwise
to "local()" a variable for one statement or call.
For people who have looked at lots of different codebases: do people
think that
$var = do { several statement; }
is just an odd construct?