in reply to Variable being clobbered by a seemingly unrelated while loop

That last for loop is only executing once and it's assigning $runlvl to $_. The matches within the loop are matching on $_, and apparently the call to process is indeed bashing $_. If you change that last loop to

for my $xx ($runlvl) { $xx =~ /2/ and process($0); $xx =~ /3/ and finalize(); }

your problem likewise goes away.

(which surprises me a bit since I thought $_ was always implicitly localized but apparently not, since if we instead put a local $_; declaration in process() the problem likewise disappears.)

Replies are listed 'Best First'.
Re^2: Variable being clobbered by a seemingly unrelated while loop
by tobyink (Canon) on Oct 08, 2014 at 09:48 UTC

    $_ is localized in for loops, but not while loops. (Indeedy any variable is localized in a for loop. Even lexical variables.)

      Even lexical variables.
      But,
      my $x = 3; local $x'
      leads to
      Can't localize lexical variable $x at 1.pl line 2.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        I guess tobyink means:

        my $x='foo'; for $x ('bar') { print "$x\n"; } print "$x\n"; __END__ bar foo
      Right. No I was thinking $_ was implicitly localized in subs, but now I see perlsub explicitly warns against expecting this.