Update: Thanks to dave_the_m, ikegami and hossman I have found the problems and fixed them in my code. The warnings on $a and $b were not the issue. Using ikegami's suggestion removed those warnings.

The problem was that the abs function returned a scalar and not an object. This is the intended result for that function. After the first pass through reduce $a was a scalar and couldn't call abs().


Greetings! I'm getting some warnings and errors with a module function. I've searched around but haven't found any node or web page with a solution. Hopefully some eyes from a distance can see where I've gone wrong.

I'm using build 518 of ActiveState's Perl on Windows XP. I've included the smallest sample that recreates the problem in the read more section below. The messages I'm receiving are:

Name "Math::Interval::a" used only once: possible typo at C:\SRC\ivpm. +pl line 39. Name "Math::Interval::b" used only once: possible typo at C:\SRC\ivpm. +pl line 39. Can't call method "abs" on an undefined value at C:\SRC\ivpm.pl line 3 +9.

I've used Data::Dumper to examine the value of $row_ref and it is an array of three Math::Interval objects like I expect. Also, I'm able to call abs() for any value in the matrix. (Ex. $x->[1][1]->abs()).

Any idea where I've gone wrong? Also any idea why I'm getting the warnings for $a and $b from using reduce? Any code improvements are welcome. TIA!

#!/usr/bin/perl package Math::Interval; use warnings; use strict; use List::Util qw( max min reduce ); sub _eps_for { my ($num, $epsilon) = (shift) x 2; # copy arg to both vars $epsilon /= 2.0 while $num + $epsilon / 2.0 != $num; return $epsilon; } sub _interval { my ($min, $max) = ( min(@_), max(@_) ); return bless [$min - _eps_for($min), $max + _eps_for($max)], __PAC +KAGE__; } sub lb { my ($x) = @_; return $x->[0]; } sub ub { my ($x) = @_; return $x->[1]; } sub abs { my ($x) = @_; return max( abs($x->lb()), abs($x->ub()) ); } sub matrix_norm { my ($mat_ref) = @_; my @row_sums; foreach my $row_ref (@$mat_ref) { my $sum = reduce { $a->abs() + $b->abs() } @$row_ref; push @row_sums, $sum; } return max @row_sums; } package main; # getting the absolute value of a single interval works my $a = Math::Interval::_interval(0.333333, 0.333334); print 'abs([0.333333; 0.333334] is ', $a->abs(), "\n"; # getting the matrix norm for a matrix of intervals doesn't my $x = [ [ Math::Interval::_interval(3), Math::Interval::_interval(2), Math::Interval::_interval(3) ], [ Math::Interval::_interval(5), Math::Interval::_interval(9), Math::Interval::_interval(8) ], ]; print 'mat norm ', Math::Interval::matrix_norm($x), "\n";

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 reply to Can't call method "abs" on an undefined value. by HollyKing

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.