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.

#!/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 );
the error I'm getting is:
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.

In reply to passing subroutine references by joe76

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.