Under the heading of Stupid (Perl) Things I Did Today, is this Forehead Slapper:
# define option names and defaults our $UNIQUE_OPT = q/unique/; our $FORCE_OPT = q/force/; our $VERBOSE_OPT = q/verbose/; our $OUTPUT_EXT_OPT = q/ext/; # create option specs for Getopt my @option_config = qw( $UNIQUE_OPT $FORCE_OPT $VERBOSE_OPT $OUTPUT_EXT_OPT=s ); # create option hash for Getopt my(%option, %option_config); foreach my $option_config ( @option_config ) { # copy $option_config to $option_name, keep only name of option ( my $option_name = $option_config ) =~ s/\W.*$//; # save a reference to the %option value in %option_config hash $option_config{$option_config} = \$option{$option_name}; } # process options GetOptions %option_config or usage(); if ( $option{$UNIQUE_OPT} and $option{$FORCE_OPT} ) { die "-$UNIQUE_OPT and -$FORCE_OPT are mutually exclusive." . " See $SCRIPT_NAME -help for syntax."; }
This broke with:
C:\>perl myscript stuff Error in option spec: "$VERBOSE_OPT" Error in option spec: "$FORCE_OPT" Error in option spec: "$OUTPUT_EXT_OPT=s" Error in option spec: "$UNIQUE_OPT"
Due to the hour of the day, I sat and stared at this for some time, scanned through Getopt::Long, and scratched my head.

Finally it hit me -- qw doesn't interpolate, so things like $UNIQUE_OPT don't DWIM. The fix was:

my @option_config = split ' ', qq( $UNIQUE_OPT $FORCE_OPT $VERBOSE_OPT $OUTPUT_EXT_OPT=s );
which looks very similar to the rough equivalent of qw as found in perlop:
split(' ', q/STRING/);
In fact, it made me wonder if this DWIM:
my @option_config = qw" $UNIQUE_OPT $FORCE_OPT $VERBOSE_OPT $OUTPUT_EXT_OPT=s ";
:( Sadly, it doesn't.

Anyone care to comment on whether this should or shouldn't make it into the language?

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to qw "$string $string" doesn't interpolate by QM

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.