On 4/25/06, JupiterHost.Net <[EMAIL PROTECTED]> wrote: > for my $obj( > sort { > $a->value() cmp $b->value() > || > $a->part('Name')->value() cmp $b->part('Name')->value() > } > grep { defined } @objects > ) { > > ... > > $a/$b->value() always works since if $a/$b is defined its an object with > a method called value() > > $a/$b->part('Name') returns an object that also has a method called > value() or undefined if there is no "Name" part > > The sort works perfect for what I need, *except* when > $a/$b->part('Name') does not return an object itself. Then it dies with > "Can't call method value() on undefined value." > > So how can I keep my sort() but avoid/work with the "can't call method > on undefined value" error when $a/$b->part('Name') returns undef instead > of an object? > > I've put them in valriables depending on defined()ness and done > > $name_a cmp $name_b > > Which is error free but also doesn't appear to sort it properly :(
How does it not sort properly? We need some sample data for that. Generally, though, you have two options. One is to trap the error with eval or some other method. The other is to insert some logic to check that the methods are defined before you call them...and decide how you want to handle object that have no value. Perl is basically saying "I can't sort it by value if it doesn't have a value". Either check for definedness in the sort, or verify the data before you sort it and set the bad data aside to deal with later. This is one of the quirks of dealing with methods. If a method isn't defined, it's more like an undefined subroutine call than an undefined variable, in that attempting to call a method on an object for which it's not defined is fatal. HTH, --jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!