in reply to Retain global value across files

If you want a variable not to be reset to 1, don't assign one to it. Initialize it outside the subroutine (and you can still use my. In recent Perls, you can use state.

a.pl:

#!/usr/bin/perl use warnings; use strict; use MyLib qw{ verify }; verify() for 1 .. 10;

MyLib.pm:

package MyLib; use parent 'Exporter'; our @EXPORT_OK = qw{ verify }; my $counter = 1; sub verify { print $counter++, "\n"; }

Output:

1 2 3 4 5 6 7 8 9 10
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Retain global value across files
by Anonymous Monk on Nov 28, 2014 at 05:05 UTC

    Thanks. That worked!