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

Attila Bárdi attila.bardi at gmail.com
Tue Apr 22 17:02:02 BST 2014


Hey Hugues,

both of them are scalar context, but in the first you wanna place a hash to
the scalar variable that is why you have the error, and in the second you
assign a hash reference to the scalar, which is ok, because the reference
is a scalar.

This is a hash:

%hash1 = (
        key1 => 'value1',
        key2 => 'value2',
        key3 => 'value3',
    );

You cannot store a hash in a scalar, but you can store the reference to the
hash in a scalar:

my $hash_ref1 = \%hash;

But you don't wanna deal with a hash name because you would use it only
once to create the hash, so you skip this step and create an anonymous
hash, and place the reference to a scalar in one step:

my $hash_ref2 =   {
        key1 => 'value1',
        key2 => 'value2',
        key3 => 'value3',
    };

You can read more about it in the Intermediate Perl book from O'Reilly.

Best regards,
Attila


On Tue, Apr 22, 2014 at 5:31 PM, Hugues <hugues at max4mail.com> wrote:

> Thanks for your help
> if I understand well
>
> $Stock->{$NumP} = ( $Num =>  { Code    => $parc->{Code} .... )  is scalar
> context, and generate a error
> $Stock->{$NumP} = { $Num =>  { Code    => $parc->{Code} .... } is correct.
>
> I will add "use diagnostics;" in my code
> thanks
> Hugues
>
> Le 22/04/2014 17:18, Maxwell Carey a écrit :
>
>  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}
>>                                        };
>> _______________________________________________
>> dancer-users mailing list
>> dancer-users at dancer.pm
>> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>>
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20140422/0c810b7f/attachment-0001.html>


More information about the dancer-users mailing list