in reply to Array Reference Question

Much simpler would be
sub recReverse { my $ref2array = shift; @$ref2array = reverse @$ref2array; for (@$ref2array) { recReverse $_ if ref $_ eq 'ARRAY' } }

---- edit ----

The last code was written without any perl available. On closer look i need to do the check for an array ref before trying to reverse. A working rewrite is:

sub recReverse { my $ref2array = shift; return unless ref $ref2array eq 'ARRAY'; @$ref2array = reverse @$ref2array; recReverse($_) for @$ref2array }

Now I have a question. If I write recReverse $_ for @$ref2array instead of the parenthesised form of the last line, I would have thought that this was equivalent. But I get an error message:

 Can't call method "recReverse" without a package or object reference

Replies are listed 'Best First'.
Re^2: Array Reference Question
by Hameed (Acolyte) on Sep 09, 2014 at 06:31 UTC
    Yes. Very neat. Thank you. I do end up with some unnecessary and redundant code at times.
Re^2: Array Reference Question
by Hameed (Acolyte) on Sep 10, 2014 at 00:40 UTC
    According to perldiag
    Can't call method "%s" without a package or object reference (F) You used the syntax of a method call, but the slot filled by the object reference or package name contains an expression that returns a defined value which is neither an object reference nor a package name. Something like this will reproduce the error:
    $BADREF = 42; process $BADREF 1,2,3; $BADREF->process(1,2,3);
    I think the error message is ambiguous or somehow it expects $_ to be a reference to an object :s. From what I have read Perl is strictly a 1-time parser. It doesn't scan the file first for subs and then go and compile. In order to be able to call user methods without parentheses you have to predeclare it or define the sub first and then call it last in the file. The latter only works with non-recurisive methods. With recursive obviously you will have to predeclare it. I just tried it and it worked.

    I hope it makes sense.

    Reference:
    http://stackoverflow.com/questions/5992715/why-is-parenthesis-optional-only-after-sub-declaration
    http://perldoc.perl.org/perldiag.html