For some reason I'm unable to get the simplest getopt functionality to work. I'm fine with Getopt::Long, but Getopt::Std for some reason isn't doing what I think it should:

eg:

#!/usr/local/bin/perl -w use strict; use Getopt::Std; my $opt_u = 0; getopt('u') || die "can't do getopt\n"; print "username is $opt_u\n";

This ALWAYS dies. If I don't use die, it just returns an empty variable. It seems to be me misunderstanding how to declare the variable while using strict. Without declaring the variable, the script won't run. But the only way to get it to run is to not use strict and not declare it.

###

Ok, I've played round and found the solution is to declare it within the package. Like so:

#!/usr/bin/perl -w use strict; use Getopt::Std; $Getopt::Std::opt_u = 0; getopt('u'); print "username is $Getopt::Std::opt_u\n";

Thanks for the help, everyone.

toeslikefingers.com - because you've got too much time on your hands

janitored by ybiC: Retitle from less than descriptive "dumbass strict / getopt questions"


In reply to Getopt::Std dies or returns empty var by bonoboy

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.