Scope of variables in a function

Anthony Lucas anthonyjlucas at gmail.com
Mon Jun 3 00:28:11 BST 2013


Well, you shouldn't have to make that case.

Since no one else is speaking up for it, I will.


The current behaviour makes sense. The only way this could cause you a
problem is if you were already doing a number of other things wrong.
The current behaviour is exactly as I would expect reading the code.

1. Probably helps to understand the way scope works in the language you're
using
2. Write what you mean (fair enough, *maybe perlcritic could say something*?
or warnings.pm?)
3. People confuse themselves with shorthand conditional statements?
4. Be more explicit (set things to 0 or undef if they need to be empty or
untrue, so I don't have to go searching your code to figure out what's left
in your variable)

So far all I've heard is "artefact", which is all too often banded around
as an argument itself lately.
What's actually wrong with the current behaviour, assuming you've read the
manual for what you're trying to use (i.e. my)?

What people seem to be asking for is exactly the same as Dave described,
which would be bad, as he also described.



On 2 June 2013 22:43, Gordon Banner <tech at gordonbanner.me.uk> wrote:

> Also that would break
>
> my $var = "outer";
> {
>     my $stashed_value = $var;
>     my $var = "inner";
>     ...
> }
>
> I don't have an instant example of why doing that would be a good thing,
> but it's quite clearly supported at present.
> Gordon
>
>
> On 02/06/2013 12:49, Dave Mitchell wrote:
>
>> The original patch was a proof-of-concept that hoisted the run-time
>> effects of my to the start of the containing scope. So that from a
>> run-time point of view,
>>
>>      {
>>         AAA;
>>         my $x = ...;
>>         BBB;
>>         my $y = ...;
>>         CCC;
>>         my $z = ...;
>>         DDD;
>>
>>      }
>>
>> is executed like
>>
>>      {
>>         my ($x,$y,$y);
>>         AAA;
>>         $x = ...;
>>         BBB;
>>         $y = ...;
>>         CCC;
>>         $z = ...;
>>         DDD;
>>      }
>>
>> The main problem with it was that in something like the following:
>>
>>      while (<>) {
>>         next unless /rare condition/;
>>         my ($lots, $of, $lexicals) = split;
>>         ...
>>      }
>>
>> all those lexicals would be initialised and cleared every time round the
>> loop, rather than just on rare occasions: which could be quite a
>> performance hit.
>>
>>
>>
>


More information about the london.pm mailing list