Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

useless use of a constant in a void context?

by princepawn (Parson)
on Nov 20, 2000 at 19:31 UTC ( #42501=perlquestion: print w/ replies, xml ) Need Help??
princepawn has asked for the wisdom of the Perl Monks concerning the following question:

At the end of this hash key definition, I get the error message "useless use of a constant in a void context"... could someone please tell me what this means?
our %spec; $spec{V20} = ( delete_code => 1, ifill_order_number => 8, ifill_order_suffix => 1, sequence_number => 3, client_number => 2, lcc => 13, shipped => 5, ext_price => 7, future_use => 7, ship_year => 2, ship_month => 2, ship_day => 2, xmit_year => 2, xmit_month => 2, xmit_day => 2, xmit_time => 6, cancel_flag => 1, cancel_msg => 30, upc_ean => 13, ship_date_2 => 8, xmit_date_2 => 8, client_title => 30, client_ref => 20, ordered => 5 );

Comment on useless use of a constant in a void context?
Download Code
Re: useless use of a constant in a void context?
by merlyn (Sage) on Nov 20, 2000 at 19:41 UTC
    It means you used a constant where it's not being accessed. Specifically, you just did one of these:
    $a{$b} = (3, 5, 7, 9, $c);
    Which sets $a{$b} to $c, and ignores the 3, 5, 7, and 9.

    Why you would do that, I'm not sure, but you just did. Maybe you meant to use curlies instead of parens around that list to make it into an anon hashref. Perhaps?

    -- Randal L. Schwartz, Perl hacker

(Ovid) Re: useless use of a constant in a void context?
by Ovid (Cardinal) on Nov 20, 2000 at 19:44 UTC
    Try the following:
    $spec{V20} = { delete_code => 1, ... ordered => 5 };
    Note the braces around the hash instead of the parens. You get this error when you use parentheses around a list reference instead of square or curly brackets.

    The square or curly brackets turn list values into scalar values, while parentheses do not. A parenthesized list, when evaluated in scalar context, throws away the left argument(s). If you had printed your value, you would see that it equals '5', instead of being a hash.

    Oddly enough ;), if you picked up Programming Perl, this is explained in the back under

    Useless use of %s in void context.
    My explanation above is almost verbatim.

    Cheers,
    Ovid

    Update: still laughing my head off (ha, ha, thump) at merlyn's response :)

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Oddly enough ;), if you picked up Programming Perl, this is explained in the back{...}
      You've got to give princepawn credit. He's gone from immediately assuming it's a bug in Perl to asking questions that can be answered with RTFM. This is progress!

      Now if he'd just learn to RTFM some more, he could get off the Worst Nodes page more often.

      -- Randal L. Schwartz, Perl hacker

      Actually tracking down diagnostic messages is one thing where people Really Should use the online documentation. They change a lot from release to release, and not knowing how to get the ones for your version of Perl can seriously confuse. (There was a thread once involving our where that came up, there was nothing in 5.003 and hence Camel 2, it exits in 5.6 and hence Camel 3, but in 5.005 the word was reserved and generated a warning that confused some...)

      In this case you first type:

      perldoc perldiag
      then type:
      /Useless use
      then type "n" until you get to the right message. If you go too far type "1G"

      Where I said "Useless use" above I just wanted some part of the fixed error message. Usually it isn't hard to guess, but sometimes you will need to try twice to figure out what it should be.

      That will give you messages that are appropriate for your version of Perl.

        And if you're too lazy to do that, just add
        -Mdiagnostics
        to your command line. It looks up any error message in perldiag for you! No excuse for "not having the manpage easily" now!

        -- Randal L. Schwartz, Perl hacker

      The explanation given in the Camel book isn't satisfying to me. The error message was "useless use of a constant in a void context"

      Where is the constant? Where is the void context?

        The 'constant' is any of the numbers before the last.

        The 'void context' is that you're throwing it away, instead of using it.

        -- Randal L. Schwartz, Perl hacker

        Do you understand context? It freaked me out once or fifty times too. =) When perl is compiling your code, it looks at structures like these and decides on what "context" codehere() is in:
        codehere(); #void context $c=codehere(); #scalar context ($l)=codehere(); #list context @a=codehere(); #list context %h=codehere(); #list context print codehere(); #list context foreach (codehere()) { #list context if ( 1 <= codehere() ) { #scalar context

        When you do something along the lines of this: $a=( codehere(),codehere() ); then the $a= forces the right side into scalar context since perl can see you don't want a list so in scalar context, the right side isn't treated as a list but as a statement group. Thus, "," is an operator that forces void context on it's left-hand-side and passes on the whatever context it is in to it's right-hand-side. Since you strung together a series of values like this: $a = ( 2, 3, 4, 5, 6 ); the first four numbers are in void context and only the last item in the series is in scalar context. Thus your statement could be rewritten: 2; 3; 4; 5; $a = 6;

        And, bam!, from that you can see there are 4 constants in void context. Run either of those with perl -we on the command line and enjoy seeing the 4 warnings pop-up.

        BTW, $a = ( 1, 5 ); pulls no error. =) It seems that perl and in fact Perl treat 1; special.

        HTH Historical note, 4 years ago, or more, merlyn took me to task for snorting at the idea of a comma operator. =) I was so embarassed I re-read the pink camel from cover to cover before I ever posted to the c.l.p.m again. I wish I still had that archive, he explained in about 4 sentences what it took me 3 paragraphs to explain. oh well... =P

        --
        $you = new YOU;
        honk() if $you->love(perl)

Re: useless use of a constant in a void context?
by snax (Friar) on Nov 20, 2000 at 19:44 UTC
    Try curlies {} instead of (). You aren't creating a reference.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://42501]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-09-23 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls