Wibble has asked for the wisdom of the Perl Monks concerning the following question:

This is allowed and works:
local ($a=1, $b=2, $c=3);
But this isn't and gives an error:
my ($a=1, $b=2, $c=3);
The following is what I would normally use myself:
my ($a, $b, $c) = (1,2,3);
I have seen the first construct used in someone else's code, but I'm not sure if it is a Perl crime or not - anyone know?

Replies are listed 'Best First'.
Re: Embedded variable assignments inside local declarations
by broquaint (Abbot) on Jan 26, 2003 at 17:03 UTC
    This is allowed and works ...
    It parses, but doesn't do what you might think it does e.g
    $foo = "a string"; { local( $foo = "localised" ); print $foo,$/; } print $foo,$/; __output__ localised localised
    The output from B::Deparse also gives it away (assuming above code passed through perl -MO=Deparse,-p -)
    ($foo = 'a string'); { ($foo = 'localised'); print($foo, $/); } print($foo, $/); - syntax OK

    HTH

    _________
    broquaint

•Re: Embedded variable assignments inside local declarations
by merlyn (Sage) on Jan 26, 2003 at 17:07 UTC
      $a = 1, $b = 2, $c = 3; works, and that's exactly why my $a = 1, my $b = 2, my $c = 3; would work.

      Although it looks odd, but from a Perl internal view, it is the same animal as the following (,which we see from day to day):

      foreach $line (@lines) { ... }
      works, that's why
      foreach my $line (@lines) { ... }
      Basically, Perl allows you to my variables on fly.

      One more example for the same animal:
      for (my $i = 1; $i < 10; $i ++) { print $i; }
Re: Embedded variable assignments inside local declarations
by integral (Hermit) on Jan 28, 2003 at 19:55 UTC
    The difference has probably been caused by the fact that local and my were implemented at different times, and so local takes a list expression, while my is restricted to just plain unqualified variable names. In addition local is more flexible than my, since my only allows unqualified variables, whilst local allows things like hash elements (more than just variables anyways).

    --
    integral, resident of freenode's #perl