[Bioperl-l] get_nof_contigs returns undef

Wes Barris wes.barris at csiro.au
Sun Oct 2 19:21:16 EDT 2005

Heikki Lehvaslaiho wrote:

> Ah, thanks, the extra square brackets do the trick!
> return scalar( @{[moose()]} );
> sub get_nof_contigs {
>       my $self = shift;
>       return scalar( @{[$self->get_contig_ids()]} );
> }
> Talk about clean syntax! ;-)

Yes, one might get a headache trying to figure out what that line is

I notice that this fix has not made it into the code yet.  Should I
submit a bug report to get this fix pushed through?

> Seriously, I do agree with you that it is better to be wordy and clear than 
> terse and hard to read.
>  -Heikki
> On Sunday 25 September 2005 19:42, George Hartzell wrote:
>>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).
>>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.... ;)
>>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.
>>Bioperl-l mailing list
>>Bioperl-l at portal.open-bio.org

Wes Barris
E-Mail: Wes.Barris at csiro.au

More information about the Bioperl-l mailing list