in reply to Can't call method "abs" on an undefined value.

(Two people so far have failed to realise that $a and $b are used by List::Util::reduce in the same way as sort.)

Anyway, your immediate problem is that you don't include reduce in the 'use List::Util' import list. But after fixing that, your next problem is that $a and $b start off as objects, but after the first reduce, $a is now a simple number rather than an object, so $a->abs fails.

Dave.

  • Comment on Re: Can't call method "abs" on an undefined value.

Replies are listed 'Best First'.
Re^2: Can't call method "abs" on an undefined value.
by HollyKing (Pilgrim) on May 05, 2006 at 23:26 UTC

    Ah ha! I knew it was something simple I was missing. Thanks that gives me enough to take a stab at it again.

    reduce is included on the import list for List::Util in the main program I just forgot it in my sample. Adding it still gives the warning about $a and $b only being used once. I've updated the sample and my post.

    Owl looked at him, and wondered whether to push him off the tree; but, feeling that he could always do it afterwards, he tried once more to find out what they were talking about.

      In this case, the warnings are wrong. Those variables *are* used more than once, but List::Util sets them in a fashion that Perl can't detect at compile-time. (Something like *{caller() . '::a' = ...;.) You can get rid of the warnings by including the following in your package:

      $a = $a; $b = $b;

      Ignore the warnings when trying to find your error, they are misleading you. (But do change the variable name in my $a = ...; to something else!)

        Thanks! That took care of the warnings.

        Owl looked at him, and wondered whether to push him off the tree; but, feeling that he could always do it afterwards, he tried once more to find out what they were talking about.

Re^2: Can't call method "abs" on an undefined value.
by HollyKing (Pilgrim) on May 05, 2006 at 23:48 UTC

    Thanks! Adding reduce to the import list for List::Util changed the error message and it made a lot more sens.

    Owl looked at him, and wondered whether to push him off the tree; but, feeling that he could always do it afterwards, he tried once more to find out what they were talking about.

Re^2: Can't call method "abs" on an undefined value.
by ikegami (Patriarch) on May 05, 2006 at 23:25 UTC
    That's not it either. If so, you'd get a different error message:
    >perl -e "$obj = 4; $obj->test()" Can't call method "test" without a package or object reference at -e l +ine 1.
      The OP gets the error message 'Can't call method "abs" on an undefined value' because reduce hasn't been imported, so isn't called, so perl just calls a block of code with $a being undef.

      I was saying that *if* the OP fixed her code so that reduce was actually called, then the original error would go away, but a new error message would appear similar to the one you describe, due to a further bug in her code

      Dave.

        He did change his code and did get a different error message like you described. Thanks for the help I understand the problem now.

        Owl looked at him, and wondered whether to push him off the tree; but, feeling that he could always do it afterwards, he tried once more to find out what they were talking about.