What does this Perl conditional do?

I'm trying to understand a particular Perl code from vcake. Usually I find my way around in Perl but the following statement baffles me. I suspect that this is simply an error but I'm not completely sure. The statement is:

foreach my $seq (keys %$set) {
    if( (defined $set->{$seq}) and (my $numReads >= ($coverage)) ) {
        do something;
    }
    ...
}

$coverage has been defined at the beginning of the file as a scalar integer (e.g. 10) and is never again written to. $numReads is only used in the line above, nowhere else!

$set, on the other hand, is modified inside the loop so the first part of the condition makes perfect sense. What I don't understand is the second part because as I see it, this will always evaluate to the same value and I don't understand the significance of $numReads or >= here. Can someone please enlighten me? Are there perhaps invisible automatic variables involved?

Answers


my $numReads means: Create a new local variable within the context of the foreach loop. Its initial value is undef, which in numerical context is treated as 0. So the code reads:

if ((...) and (0 >= ($coverage)) ) {
}

which means "do something" is never executed unless $coverage is set to 0 or less.

If this was debug code, I'd assume that $coverage is used to enable/disable this statement.

My guess is: You've found a bug.


One might also note that unless $set is a reference to %set, or some other hash that mimics the structure of %set, then the line

foreach my $seq (keys %set)

Does not match the condition

defined $set->{$seq}

Since $set->{$seq} dereferences a hash ref, and %set is a hash. It is more likely that this is a second bug than it is that someone has instantiated both the hash %set and the scalar $set with a hashref.


Need Your Help

Solution for e-Forms engine

forms validation webforms

My client is asking for a solution that handles e-forms generation to be done by a technology-savvy user (not a developer). In other words, An e-form engine for dynamic creation and implementation...

History tables in SQL Server

sql sql-server sql-server-2008-r2

I have been working now on a project for a period of time. The project has been released and is in production mode.