[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Getting Need Explicit Package



Reading the book: Advanced Perl Programming - Sriram Srinivasan. On page 62
there is an example of Closures.  When I run it, I get the following error
message:

# Global symbol "odd_iter" requires explicit package name.
Code Is:
#----------------------------------------------------------------------
#!perl -w
use  strict;
#
#   Advanced Perl Programming - O'Reilly & Associates, 1997
#
#   Exercise/Page:   062.a
#
#   Description:    Closure
#
#   Default
#

sub even_odd_print_gen {
#   $last is shared between the two procedures
    my ($rs1, $rs2);
    my ($last) = shift; Shared by the two closures below
    $rs1 = sub {    # even number printer
         if ($last % 2) {
            $last++;
          }else {$last += 2};
         print "$last \n";
     };  # end of even number printer

    $rs2 = sub {    # odd nubmer printer
         if ($last % 2) {
            $last += 2;
          }else {$last++};
         print "$last \n";
     };  # end of odd number printer
    return ($rs1, $rs2) # returning two anon sub references
 }  #end of even_odd_print_gen

($even_iter,$odd_iter) = even_odd_print_gen(10);

&$even_iter ();  #prints 12
&$odd_iter  ();  #prints 13
&$odd_iter  ();  #prints 15
&$even_iter ();  #prints 16
&$odd_iter  ();  #prints 17

#----------------------------------------------------------------------

 Not certain what is happening and unsure where to look to find out what is
really wrong.

Any assistance would be greatly appreciated.

Wags

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch