[Bioperl-l] get_nof_contigs returns undef

Heikki Lehvaslaiho heikki.lehvaslaiho at gmail.com
Mon Sep 26 02:10:32 EDT 2005


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! ;-)

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).
>
> <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
>
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l at portal.open-bio.org
> http://portal.open-bio.org/mailman/listinfo/bioperl-l

-- 
______ _/      _/_____________________________________________________
      _/      _/                      http://www.ebi.ac.uk/mutations/
     _/  _/  _/  Heikki Lehvaslaiho    heikki at_ebi _ac _uk
    _/_/_/_/_/  EMBL Outstation, European Bioinformatics Institute
   _/  _/  _/  Wellcome Trust Genome Campus, Hinxton
  _/  _/  _/  Cambridge, CB10 1SD, United Kingdom
     _/      Phone: +44 (0)1223 494 644   FAX: +44 (0)1223 494 468
___ _/_/_/_/_/________________________________________________________



More information about the Bioperl-l mailing list