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

Folks,

I found that I'm not clear about the following:

do { push my @array, qw/x y z/ if $cond; push @array, qw/a b c/; ...some more things with @array... }

What is the expected scope of @array if $cond is false? I'd appreciate if someone could share some thoughts about this.

Background: on a very old Perl version, I ran into a problem where the old content of @array shows up after the first push (not before it!) when I enter the block a second time. Unfortunately I cannot reproduce it in a short script. The effect is only visible in a ~5k lines script. But there I can reproduce it every time I run the code.

Replies are listed 'Best First'.
Re: Variable scope
by ikegami (Patriarch) on Apr 03, 2018 at 14:17 UTC

    The behaviour of your code is undefined. You're not allowed to use a lexical variable without first executing its declaration.

      my @array; should occur within the do loop before either statement occurs, neither of which should use my.
Re: Variable scope
by shmem (Chancellor) on Apr 05, 2018 at 11:48 UTC

    This is related to the hack used to implement state variables in subroutines, before state variables have been implemented in perl 5.10.0, which emits a warning on recent perls. It allocates the lexical variable on the subroutine's pad (or better: the pad of the current scope), but prevents it from being cleared:

    #file state.pl sub foo { my $bar if 0; $bar++; print "foo called $bar times\n"; } foo for 'a'..'f'; __END__ Deprecated use of my() in false conditional at state.pl line 2. foo called 1 times foo called 2 times foo called 3 times foo called 4 times foo called 5 times foo called 6 times

    This works also for lexical arrays.

    However, if the conditional is declared beforehand as a variable, there is no such warning, since that warning happens at compile time, but the conditional is resolved at runtime:

    # file state.pl my $cond; sub foo { my @bar if $cond; push @bar, @_; print "array \@bar = (@bar)\n"; } foo $_ for 'a'..'f'; $cond = 1; foo $_ for 1..6; __END__ array @bar = (a) array @bar = (a b) array @bar = (a b c) array @bar = (a b c d) array @bar = (a b c d e) array @bar = (a b c d e f) array @bar = (a b c d e f 1) array @bar = (2) array @bar = (3) array @bar = (4) array @bar = (5) array @bar = (6)

    Huh? We have array @bar = (a b c d e f 1) here? Well, my variables are cleared at the end of their scope, so setting $cond = 1 doesn't clear the (state) array immediately.

    If you don't have a sophisticated use (read: obfuscated use) for altering the behavior of my variables, you should use:

    use feature qw(state); # also use 5.10.0; sub foo { state @bar; ... }
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      From my experience, using state in a named subroutine is a code smell. Sooner or later, you'll need to call the same subroutine for a different purpose, and you'll need a way to reinitialize the state variable with a different value. I usually use two closures over the same my variable, one of them being the setter.
      my $state; sub init_state { $state = shift } sub foo { if ($state) { ...

      Where state makes sense, in my opinion, is in anonymous subs, because an anonymous sub has a defined life span:

      for my $i (1 .. 3) { my $s = sub { state $x = $_; say "$i $x" }; $s->($_) for qw( a b ); }

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        From my experience, using state in a named subroutine is a code smell.

        That depends on the life span of the code. The older it gets, the more it reeks of foul.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

        Static variables have their uses. For example, one might generate a crc32 table on-demand. This has the advantages of: (1) not using globals, (2) not relying on BEGIN, (3) on-demand construction, (4) conciseness. It replaces the less expressive construct

        { my $table; sub foo { $table //= init_tables(0x123); ... } }
        There's nothing wrong with procedural programming.

        Oh, and another use for state variables — caching/memoization. For example:

        sub sort_network { state @mem; my $cmp = $mem[@_] //= do { ... }; my @res = @_[ @$cmp ]; }
        I'm sure there are plenty more uses.

        Hm. One more example. Here state is used to enumerate unique string instances (over the whole lifetime of a program):

        #! /usr/bin/perl use warnings; use 5.010; sub enumerate { my ($property, $key) = @_; state (%hash, %enum); $hash{$property}{$key} //= ++$enum{$property}; } print "\n", "Edibles:\n"; print "$_ => @{[enumerate edible => $_]}\n" for qw( apple spam pear orange spam spam spam kebab spam caterpill +ar ); print "\n", "Colors:\n"; print "$_ => @{[enumerate color => $_]}\n" for qw( red yellow red red red green );

Re: Variable scope
by Anonymous Monk on Apr 03, 2018 at 13:24 UTC
      Great, thanks!
Re: Variable scope
by karlgoethebier (Abbot) on Apr 05, 2018 at 10:29 UTC

    I made it compile:

    #!/usr/bin/env perl use strict; use warnings; use feature qw(say); my $cond; do { push my @array, qw/x y z/ if $cond; push @array, qw/a b c/; }; say q(Ouch!); __END__ Ouch!

    Now compare it with this (your) version:

    #!/usr/bin/env perl use strict; use warnings; use feature qw(say); my $cond; do { push my @array, qw/x y z/ if $cond; push @array, qw/a b c/; } say q(Ouch!); __END__ syntax error at ./doh.pl line 15, near "say" Execution of ./doh.pl aborted due to compilation errors.

    Mmh. And i wonder what this construct should be good for.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help