Steve Bertrand wrote:
> Shawn H. Corey wrote:
> 
> [..snip..]
> 
>> Here's an example of what they're talking about.  Notice that when
>> CONFIG_VAR is called, a new $new is created and set to zero.  For it's
>> second call and those that follow, the $num inside callback remains
>> unchanged.
>>
>> sub CONFIG_VAR {
>>     my $num = 0;
>>     sub callback {
>>         return ++$num;
>>     }
>>     print "CONFIG_VAR: \$num = $num\n";
>> }
> 
> [..snip..]
> 
>> Since this is the behaviour you want, the error message seems
>> superfluous

I think I spoke too soon in saying what worked and what didn't...

My original goal was to create a table of named callbacks that have
their own global data to work with. Correct my terminology if necessary.

I created all functions that will be known to the project into it's own
block. Children may learn of them via the config, or maybe not. Either
way, the named subs are within it's own block, and it seems to work.

Here is what I came up with. The code does what I want it to do, so
hopefully it is self-documenting (I get a 'void context' warning on line
16, but the code does exactly what I want it to do):

#!/usr/bin/perl

use warnings;
use strict;

{
    my $num = 0;
    my @letters = ('a'..'z');

    sub CONF_VAR1 {
        return ++$num;
    }

    sub CONF_VAR2 {
        my $letter = $letters[$#letters];
        $letters[--$#letters];                  # line 16
        return $letter;
    }
}

print CONF_VAR1() . "\n";
print CONF_VAR2() . "\n";
print CONF_VAR1() . "\n";
print CONF_VAR2() . "\n";
print CONF_VAR1() . "\n";
print CONF_VAR2() . "\n";
print CONF_VAR1() . "\n";
print CONF_VAR2() . "\n";
print CONF_VAR1() . "\n";
print CONF_VAR2() . "\n";

..here's to learning ;)

Steve




Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to