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

UPDATE : Solved it myself. The problem was that the rmap() sub was declared after the getSystemData() sub that tried to call it. The error message wasn't all that helpful about it, that's all :) Hello ! I have this function :
#rmap BLOCK $struct #Takes a reference to an arbitrary struct and applies a given block of + code to each of its scalar elements. #This is basically a recursive version of the normal perl map function + (so $_ is set to each element and can be modified to modify the orig +inal struct!). #Good for cleaning up whitespace in data --> rmap {s/\s*//g} $system_ +data #rmap {print $_} $system_data sub rmap(&@) { my $code=shift; foreach ( @_ ) { my $ref = ref $_; if ($ref eq 'HASH') { while (my($key,$val)=each %{$_}) { $ref = ref $val; $_->{$key}=&rmap($code,$val); } } elsif ( $ref eq 'ARRAY' ) { my $i; for( $i=0 ; $i < scalar @{$_} ; $i++ ) { @{$_}[$i]=&rmap( $code, @{$_}[$i] ); } } elsif ( $ref eq '' ) { #It is just a normal scalar, so run the code block ! return $_ if &{$code}(); } else { die("Unhandled type of reference : '$ref'"); } return $_; } }
And it works fine from the command line, but it does not work inside a perl module that I have and I can't figure out the difference. The perl module is a bit long, but basically there is a sub doing
sub getSystemData { [... snip ...] return $system_data; }
and If I run this on the command line : perl -e 'use strict;use warnings;use My::Module; my $s = getSystemData; rmap {print} $system_data;' All scalars are printed out (which is what I want). However, if I add this exact same code in the getSystemData function so it instead reads:
sub getSystemData { [..snip..] rmap {print} $system_data; return $system_data; }
Perl exits with an error : Can't call method "rmap" without a package or object reference at ... So I'm sort of baffled at what the difference is ?!
print ref $system_data
outputs "HASH" for both the command line and in the perl module ...

Replies are listed 'Best First'.
Re: Code behaves differently in script and on command line
by ikegami (Patriarch) on Jun 26, 2009 at 09:32 UTC

    The error message wasn't all that helpful about it, that's all :

    Because you hadn't changed the parsing rules (i.e. declared the prototype) yet,

    rmap {print} $system_data;

    was taken to meant

    print()->rmap($system_data);

    Perl is rightfully complaining that print isn't returning an object.

      Hmm.. so you are saying that perl was trying to run a function called rmap, returned in an object from a call to print ?

      EDIT: Nevermind really, I see that you are correct and I understand that this is tricky business for the interpreter since I want to use a raw code block like that ...
        It was executing method rmap of the object it expected print to return. method object @args is called the indirect method call notation.