[Bioperl-l] $_ assignment question

Jonathan Barber jon at compbio.dundee.ac.uk
Thu Jul 31 08:46:44 EDT 2003


Before I go around fixing what I think are bugs, I just want to make
sure that the practice of assigning to $_ is a Bad Idea (TM), and there
isn't any magic going on that I'm unaware of.

For those that don't know, $_ is a magic global variable, so if you
monkey with it then it's altered everywhere, e.g.

    sub foo {
        $_ = shift
    }

    $_ = "moose";
    print "$_\n";   # prints "moose"
    foo("bar");
    print "$_\n";   # prints "bar"

This might be usually harmless, but when a caller is using $_ then
trouble can occur.

I ask as there seems to be a lot of use of the idioms:

    return unless $_ = shift;

and:

    while ($_=$self->_readline()) {
    }

Is there some valid Bioperl reason that local can't be called on $_
before the assignment.

Usually it's easy to see why it's been done, but the problem comes with
some of the while() loops that use the 2nd idiom and have huge amounts
of code in them, including many method calls which could depend on $_
being set before being called (even though this would be a violation of
data hiding) and I'm too Lazy to check them without first asking.

Also, is there any reason why indirect invocation of methods is being
used in some places, according to Damian Conway [1], this is a bad idea.

So are there any Bioperl specific reasons why its being used?

Cheers

[1] Object Oriented Perl, Damian Conway - p98
-- 
Jon


More information about the Bioperl-l mailing list