[Israel.pm] Testing for non-empty array

Ephraim Dan E.Dan at F5.com
Mon Apr 11 01:03:47 PDT 2005


> On Monday 11 April 2005 04:11, Issac Goldstand wrote:
> > Can people suggest elegant ways to see if a method call of an object
> > ($obj->method) returns a non-empty array (or conversely, returns an
> > empty one)?

If the only thing the method returns is a list, and you don't want the
return value (you only want to test it), then I would use:

if ( $obj->method() )
{
    # returned a non-empty list
}
else
{
    # returned an empty list
}

That's because a list in scalar context gives you the number of elements
in the list, which is what you are interested in.

- Ephraim




More information about the Perl mailing list