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)


In reply to Re: Where is the constant? Where is the void context? by extremely
in thread useless use of a constant in a void context? by princepawn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.