> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, August 16, 2000 2:17 AM
> To: [EMAIL PROTECTED]
> Subject: getting rid of nested sub lexical problem
>
> Below is a processed version of the increment_counter example from the
> guide that works as expected.
>
> --Chris
>
>   #!/usr/bin/perl -w
>   use strict;
>
>   for (1..3){
>       print "run: [time $_]\n";
>       run();
>   }
>
>   sub run {
>
>       my $counter = 0;
>
>       increment_counter();
>       increment_counter();
>
>       sub increment_counter {&{sub{
>           $counter++;
>           print "Counter is equal to $counter !\n";
>       }}}
>
>   } # end of sub run
>
>

That gives me a 'Variable "$counter" may be unavailable at ./tst2 line 17.'
warning. And its sort of obfuscated (to me, anyway). I'd probably do it this
way:

#!/usr/local/bin/perl -w

use strict;

#!/usr/bin/perl -w
use strict;

for (1..3){
    print "run: [time $_]\n";
    run();
}

{
 my $counter;

 sub run {
    $counter = 0;

    print $counter,"\n";

    increment_counter();
    increment_counter();

 }
 sub increment_counter {
       $counter++;
       print "Counter is equal to $counter !\n";
 }
}

Reply via email to