joe76 has asked for the wisdom of the Perl Monks concerning the following question:
I've been reading a book "Higher-Oder Perl" by Mark Jason Dominus and I came to one of his examples on recursion but I'm having problems with it and I can't seem to see what I've typed wrong.
When i try and run this code it fails to compile. it seems from the error I'm getting that I'm trying to pass a null value as my code reference. I can't seem to see why this is though. code some one please take a look and see where my mistake is. i thought I copied every thing out of the book verbatim and it looks the same. I need a fresh set of eyes.
I've checked his website for this book and he doesn't have an example of this version it's just he suggest the passing subroutine as a suggestion in the book.
the error I'm getting is:#!/usr/bin/perl use strict; use warnings; my $disk = 20; my @position = ( '', ('A') x $disk ); # disk are all initially on p +eg A sub check_move { my $i; my ( $disk, $start, $end ) = @_; if ( $disk < 1 or $disk > $#position ) { die "Bad disk number $disk. should be 1..$#position.\n"; } } sub hprint { my ( $disk, $start, $end ) = @_; print "Move disk #$disk from $start to $end\n"; } sub hanoi { my ( $n, $start, $end, $extra, $move_disk ) = @_; if ( $n == 1 ) { $move_disk->( 1, $start, $end ); } else { hanoi( $n - 1, $start, $extra, $end ); $move_disk->( $n, $start, $end ); hanoi( $n - 1, $extra, $end, $start ); } } hanoi( $disk, 'A', 'B', 'C', \&check_move ); hanoi( $disk, 'A', 'B', 'C', \&hprint );
Use of uninitialized value in subroutine entry at hanoi.pl line 33. Can't use string ("") as a subroutine ref while "strict refs" in use a +t hanoi.pl line 33.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: passing subroutine references
by ikegami (Patriarch) on Sep 18, 2009 at 20:56 UTC | |
by joe76 (Novice) on Sep 18, 2009 at 22:59 UTC | |
by gwadej (Chaplain) on Sep 19, 2009 at 03:01 UTC | |
by Jenda (Abbot) on Sep 19, 2009 at 10:01 UTC | |
by ikegami (Patriarch) on Sep 19, 2009 at 13:37 UTC | |
by Jenda (Abbot) on Sep 19, 2009 at 15:43 UTC | |
by ikegami (Patriarch) on Sep 20, 2009 at 17:12 UTC | |
| |
by joe76 (Novice) on Sep 20, 2009 at 17:02 UTC | |
by ikegami (Patriarch) on Sep 20, 2009 at 17:27 UTC | |
by Jenda (Abbot) on Sep 20, 2009 at 21:42 UTC | |
|
Re: passing subroutine references
by kennethk (Abbot) on Sep 18, 2009 at 21:00 UTC | |
|
Re: passing subroutine references
by Steve_BZ (Chaplain) on Sep 18, 2009 at 21:27 UTC | |
by ack (Deacon) on Sep 19, 2009 at 04:46 UTC |