I ran into this, and quickly took the path of least resistance, but had something like this:
use constant { zed => 0, one => 1, repos => (qw(oss non-oss debug)), two => 2, }; #output: Constant name 'non-oss' has invalid characters at -e line 3. BEGIN failed--compilation aborted at -e line 7.
Grumble. So I changed it:
tperl use constant { zed=>0, one=>1, two=>2, }; use constant repos=>(qw(oss non-oss debug)); '
Later when I saw this section again, I forgot why I split it...and tried combining it again with same results. Tried breaking things apart, but not seeing the diffs visualized:
> tperl # (alias tperl='perl -I/home/law/bin/lib -we'\''use strict; us +e P;') our $k; BEGIN{our $k={ zed => 0, one => 1, repos => [qw(oss non-oss debug)], two => 2, };} use constant $k; use constant repos2=>(qw(oss non-oss));'
Had to put assignment to k in BEGIN block for use-constants to see its value. But now no errors, but feeling uneasy about the result, I added some P statement to show me the structure(s):
tperl our $k; BEGIN{our $k={ zed => 0, one => 1, repos => [qw(oss non-oss debug)], two => 2, };} P "k=%s", $k; use constant $k; use constant repos2=>(qw(oss non-oss debug)); P "repos=%s", [repos]; P "repos2=%s", [repos2];' #output: k={one=>1, two=>2, zed=>0, repos=>["oss", "non-oss", "debug"]} repos=[["oss", "non-oss", "debug"]] repos2=["oss", "non-oss"]
Definitely a problem -- in this "solution"sic. Repos isn't a list, but a ref to a list. Grrr. Tried putting an array around the list:
repos => @{[qw(oss non-oss debug)]},
But then, fail:
Constant name 'non-oss' has invalid characters at -e line 10. BEGIN failed--compilation aborted at -e line 10.
I should just give up -- not worth the hassle, but does anyone see an easy way to combine these two constant definitions so that 1 statement will suffice for 2? Don't waste too much time...since I'll have already gone to the separate definition to move on, but sure seems a somewhat quirky situation, where because the definition in 1 def results in repos+defintion being combined with the outer list (because it is a list within a list, ala this mistake:
my ($x,$y) = (qw(1 2 3), qw(4 5 6));
Which we'd more quickly recognize as a problem if it was written:
my ($x,$y) = ( (1,2,3), (4,5,6));
Which most would see as a standard list-flattening gotcha. Which I skimmed over when I tried to combine my constant statements due to thinking that "qw" somehow would group them... Bzzzt. Anyway, Just don't see a safe & easy way to combine the definitions. Counterpoint?

Thanks!


In reply to use constant; RHE of solo confusing in list by perl-diddler

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.