[Bioperl-l] $_ assignment question

Jonathan Barber jon at compbio.dundee.ac.uk
Fri Aug 1 07:31:11 EDT 2003


On Thu, Jul 31, 2003 at 04:01:51PM -0400, Aaron J Mackey wrote:
> 
> Uhh, let me put in my usual snobby and grumpy two cents and say that this
> form of code Nazi-ism is really annoying, and introduces massively
> difficult to follow CVS changes.  Next we'll need to change all
> occurrences of:
> 
>   $obj->dosomething unless $dont;
> 
> to:
> 
>   if(!$dont)
>   {
>     $obj->dosomething();
>   }
> 
> so that some Java programmers don't get confused.  I think this is wasted
> effort.  Indirect object method syntax is not evil when the method is a
> well understood and used "keyword" type of thing like "new", "open",
> "connect", etc.  Many of us use it to mentally differentiate between
> constructors and class methods:
> 
> Net::SMTP->debug(1);
> my $smtp = new Net::SMTP @args;
> 
> Yes, I know a constructor is a class method, but (at least to me), it's a
> different flavor that is very often seen written with indirect syntax in
> Perl.  On the flip side, you don't typically see:
> 
>   debug Net::SMTP 1;
> 
> If *that* is the type of usage you'd like to get rid of, I have less
> of a complaint.

Fair enough.

> Re: localized $_, I think you're barking up another pedantical tree.  If
> you'd like to do this cleanly, then I'd rather see you change this:
> 
> while ($_ = $self->_readline) {
>   if (m/foobar/) {
>     # ...
> 
> Into:
> 
> while (my $line = $self->_readline) {
>   if ($line =~ m/foobar/) {
>     # ...
> 
> Rather than:
> 
> while (local $_ = $self->_readline) {
>   if(m/foobar/) {
>     # ...

Agreed. But that's a lot harder to fix than sticking a local in front of
the offending assignment, as the whole method call has to be checked for
core functions that use $_, and ATM I just want to stop Bioperl messing
around with $_ and giving me strange bugs.

> Of course, this particular brand of pedanticness would indicate that this
> construct:
> 
> while(<FH>) {
>   # ...
> }
> 
> ... should also never be used.

Yep. At least not in a module. The reason being if I do this:

for (qw(different sequence filehandes)) {
    $Bioperl_object->random_method($_); # method assigns to $_
    do_something_else_with_fh($_);
}

then do_something_else() is not getting what I expect it to get. Now I
could put a "my $fh" after the for, but why should I have too, I should
expect the modules to play nicely.

If this is intended to be the default behavour for Bioperl, then fine,
but it needs to stated at the top of all the Bioperl docs in big
letters.

Either way, patches are required.

> But I (grumpily) consent that the various ways Perl can perform actions
> at a distance can in some cases cause frustrating problems for others.
> 
> -Aaron
> 
> P.S. The Conway book is great, but it's not <i>ta biblia</i>.
-- 
Jon


More information about the Bioperl-l mailing list