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.


Need Your Help

mysql how to know db's name from wich i search

mysql database union

I have a some problems with my self-made php+sql search engine.

Counting same length items in a list

python list

I am trying to port a cgi script using pythonic style of coding.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.