[Bioperl-l] get_nof_contigs returns undef

George Hartzell hartzell at kestrel.alerce.com
Sun Sep 25 14:42:44 EDT 2005


Heikki Lehvaslaiho writes:
 > 
 > 
 > So the question is how to force list context for a subroutine. This is 
 > something I've often wondered and have not found a clean solution.
 > [...]

No, [I think] that's not the problem here.

The problem is that there's a call to sort() buried in the
get_nof_contigs return statement and sort is a Surprising function (it
returns undef when called in a scalar context).

<windmill-tilting>
The cleanest fix would be to petition the Perl community to change the
semantics of the sort() function so that it's not so Surprising.... ;)
</windmill-tilting>

In the meantime, I tend to avoid doing real work in a return
statement, I'd do the work w/in the body of the function, assign the
results to an array (e.g. @results), then return that.

It looks a bit wordy and people think I'm paranoid, but it's just so
much easier for me to be safe than to try to remember whether Perl's
going to Do The Right Thing or just bite me in the ass....

If you really want to force something into a list context, the second
paragraph of the documentation for scalar() says this:

   There is no equivalent operator to force an expression to be
   interpolated in list context because in practice, this is never
   needed.  If you really wanted to do so, however, you could use
   the construction "@{[ (some expression) ]}", but usually a sim-
   ple "(some expression)" suffices.


g



More information about the Bioperl-l mailing list