[dancer-users] Useless use of private variable in void context

Maxwell Carey mcarey at ucar.edu
Tue Apr 22 16:18:56 BST 2014


On 04/22/2014 08:21 AM, Hugues wrote:
> $Stock->{$NumP} = ( $Num =>  { Code    => $parc->{Code},
> NumP => $parc->{NumP},
> NumS => $parc->{NumS},
> NumC => $parc->{NumC}
>                                                             } );

Not related to Dancer, just plain Perl. You're assigning a list to a 
scalar, which is probably not what you intend. What you're doing is the 
same as:

     my $foo = ( "foo", "bar" );

which sets the value of $foo to "bar". If you turn on diagnostics with 
`use diagnostics;`, you can get a nice explanation of the warning:

     Another common error is to use ordinary parentheses to construct a list
     reference when you should be using square or curly brackets, for
     example, if you say

         $array = (1,2);

     when you should have said

         $array = [1,2];

     The square brackets explicitly turn a list value into a scalar value,
     while parentheses do not.  So when a parenthesized list is evaluated in
     a scalar context, the comma is treated like C's comma operator, which
     throws away the left argument, which is not what you want.  See
     perlref for more on this.

To fix, change the parentheses to curly braces to create an anonymous hash:

     $Stock->{$NumP} = { $Num =>  { Code    => $parc->{Code},
                                                                NumP => 
$parc->{NumP},
                                                                NumS => 
$parc->{NumS},
                                                                NumC => 
$parc->{NumC}
                                                             } };

Or simply remove the additional level of hash nesting (which is 
essentially what is happening in your current program):

     $Stock->{$NumP} = { Code    => $parc->{Code},
                                           NumP => $parc->{NumP},
                                           NumS => $parc->{NumS},
                                           NumC => $parc->{NumC}
                                        };


More information about the dancer-users mailing list