Hi all,

I am interested in recursive programming in Perl.
There is a bit of code to determine whether a number
is even or odd.
That's fun, isn't it?


package IsEven;
use base qw(IsOdd);

sub new {
    my( $pkg, $x ) = @_;
    return 1 if $x == 0;
    $pkg->SUPER::new( $x - 1 );
}

package IsOdd;
use base qw(IsEven);

sub new {
    my( $pkg, $x ) = @_;
    return 0 if $x == 0;
    $pkg->SUPER::new( $x - 1 );
}

package main;
use strict;

for ( 1 .. 9 ) {
    print "$_ is even number!\n" if IsEven->new( $_ );
    print "$_ is odd number!\n"  if IsOdd->new( $_ );
}

--
SH
[EMAIL PROTECTED]


Reply via email to