princepawn has asked for the wisdom of the Perl Monks concerning the following question:

I woul like to know why Perl thinks my call to walk within the subroutine is an indirect method call while the one at the bottom of the program is interpreted as a function call.
Uncaught exception from user code: Can't call method "walk" on unblessed reference at treewalk.pl lin +e 28. main::walk('ARRAY(0x15b220)') called at treewalk.pl line 34
#!/bin/env perl use strict; use warnings; use diagnostics; my $tree = [ "Accomplishments in the past week" => 'accomplishments.txt', "Plans for the next week" => [ General => 'plans.txt', Vacations => 'vacations.txt', Classes => 'classes.txt' ], "Outstanding issues" => 'issues.txt' ]; sub walk { my $t = shift ; while (my ($key, $val) = splice @$t, 0, 2) { if (!ref($val)) { print "$key => $val\n"; } else { walk $val; ##### PROBLEM LINE } } } walk $tree;

Edit: 2001-03-03 by neshura

Replies are listed 'Best First'.
Re: Why was indirect object syntax inferred here?o
by japhy (Canon) on Feb 17, 2001 at 01:40 UTC
    Because when Perl is parsing your code, it hasn't completed the definition of the walk() function, so it doesn't know the bareword walk is a function. If you just say:
    sub walk; sub walk { ... }
    then the code will work as you'd like.

    japhy -- Perl and Regex Hacker
Re: Why was indirect object syntax inferred here?o
by eg (Friar) on Feb 17, 2001 at 01:38 UTC

    You need to prototype your sub if you want to use it like a builtin (see perlsub) inside of the sub definition.

    sub walk($); sub walk($) { my $t = shift(); ...

    Either that or put parens around the argument in the sub.

    ... else { walk( $val ); } ...

    I guess the problem is that, inside of the sub definition, walk is not yet defined.