RobertCraven has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I am sorry for the probably stupid question, but I get a warning "Useless use of string in void context" for the elsif statement:

my $dbh; if($A->{genome} eq 'hg18'){ $dbh = $A->{E}->connectDatabase($A->{cfg},'/cfg/db/hg18tg', __LINE +__); } elsif($A->{genome} eq 'hg19'){ $dbh = $A->{E}->connectDatabase($A->{cfg},'/cfg/db/hg19tg', __LINE +__); } else{"'$A->{genome}' not valid"}

Any suggestion greatly appreciated!

Replies are listed 'Best First'.
Re: Useless use of string in void context
by moritz (Cardinal) on Jul 22, 2011 at 09:59 UTC
    The useless string in void context is actually "'$A->{genome}' not valid" (you don't do anything with it, so it has no effect, so it's useless).

    This is one of the cases where line numbers are wrongly reported by perl.

      Thanks, that one hurt. Is there any information out there that gives hints/explains when Perl reports wrong lines?

        This wouldn't have helped with the misleading line number reported, but might have (and might in the future) spark your thinking about just what the root problem is: use diagnostics;

      I have looked through the four or so threads where this warning is discussed and summarise that there is lack of skepticism about it. So from my point of view, all the discussions are equally lacking in skepticism and I might as well post my comment here. Here is the code snippet where I just encountered it today:-
      my $suppress = ($self->{simpos} == 1) and (($typ eq 'BHC') || ($typ eq 'ISC' +));
      So I want to save the truth value of the combined tests, with the expectation of using it more than once in my module. The first is a configuration value: 0 for no simulating, 1 for development simming and 2 for the expected situation in production, followed by one of two additional requirements for the need to set the "suppress" flag (which will later in the code suppress errors from an unenhanced component being delivered by someone else). I don't want to do it differently, e.g. an if or a ternary or something, because it would look more naff - Perl should not be warning me into producing worse code. So after due consideration, I opted to isolate the assignment instead of avoiding it.
      my $suppress; { no warnings; $suppress = ($self->{simpos} == 1) and (($typ eq 'BHC') || ($typ eq 'ISC' +)); }
      Update: my bad, spoke too soon: I needed an extra bracket around the whole assignment and the warning goes away on its own. The lack of an outer bracket indeed renders the last two tests useless, so Perl was right after all.
      my $suppress = (($self->{simpos} == 1) and (($typ eq 'BHC') || ($typ eq 'ISC +' +)));

      One world, one people