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#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 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 ...] 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 ?!sub getSystemData { [..snip..] rmap {print} $system_data; return $system_data; }
outputs "HASH" for both the command line and in the perl module ...print ref $system_data
In reply to Code behaves differently in script and on command line by monsieur
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |