[ABE.pm] Here's a weird one

Ricardo SIGNES rjbs-perl-abe at lists.manxome.org
Thu Feb 22 16:44:47 PST 2007


* "Faber J. Fedor" <faber at linuxnj.com> [2007-02-22T19:15:06]
>     my $table = $q->param('table');
>     
>     $Decile1Tag = '1' if ! defined ($Decile1Tag);
>     $Decile10Tag = '10' if ! defined ($Decile10Tag);
>     $weighting = 'equal' if ! defined($weighting);
>     
>     my $table = 'fm_'.$bm.'_'.$variable.'_'.$weighting.'_weighting'
>         if !defined($table);
>     
> Anyone see the error?  Both instances of $table have a "my" in front of
> it! If I take outthe second my, the CGI program runs on an update as
> well as a submit! 

Each time you write "my $x" you get a new $x, with no defined value.  This is
NEVER EVER what you mean, unless you are trying to fuck with future maintenance
programmers:

  my $x = $value if not defined $x;

You probably mean: 

  my $table = $q->param('table');
     $table = $something_else unless defined $table;

By the way, I couldn't help but notice that this is an eyesore:

  $table = 'fm_'.$bm.'_'.$variable.'_'.$weighting.'_weighting'

Why not:

  $table = "fm\_$bm\_$variable\_$weighting\_weighting"

? :)

> I have been running this code for weeks with this bug.  The compiler
> never saw it.  And I just found four more instances of the same bug! All
> four can be seen above.
> 
> This code compiles and runs from the command line without errors or warnings.

You are getting no warnings because you have not turned on warnings.  You
should ALWAYS start your code with "use strict; use warnings;"

Example:

  ~$ perl -e 'my $x; my $x'
  ~$ 

  ~$ perl -e 'use warnings; my $x; my $x'
  "my" variable $x masks earlier declaration in same scope at -e line 1.

Woop!

-- 
rjbs


More information about the ABE-pm mailing list