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 ...

In reply to Code behaves differently in script and on command line by monsieur

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.