QM has asked for the wisdom of the Perl Monks concerning the following question:

Just thought I'd share this gotcha I encountered while trying to use Getopt::Declare. [This is not specific to G::D, but occurs with any module taking variable parameters.]
#!/your/perl/here use strict; use warnings; my $options; my $string; BEGIN { $string = <<'HERE'; -one <one:i> First parameter HERE } use Getopt::Declare $string => $options; ### <<< BONK! print "one = $options->{-one}\n";
To use this form of G::D, $string and $options must exist (such as with my), and $string must be defined, such as through a BEGIN (before the compiler sees use Getopt::Declare).

To avoid this, at least with G::D, use the following instead:

use Getopt::Declare; $options = Getopt::Declare->new($string);
You can use a banana to drive a nail, but you'll need to precondition the nail, or the banana, or both.

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

Replies are listed 'Best First'.
Re: use module with parameter variable
by revdiablo (Prior) on Aug 19, 2004 at 16:53 UTC

    Another alternative is to use require instead of use:

    BEGIN { require Getopt::Declare; import Getopt::Declare $string => $options; }
      I must be missing the point:
      BEGIN { require Getopt::Declare; import Getopt::Declare $string => $options; }
      This implies that $string is defined in an earlier BEGIN, doesn't it?

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

        $string could be defined inside the BEGIN block, or the BEGIN block could be left out altogether. I only included it in my example out of sheer reflex. :-) Here's another example that might be more useful:

        my $options; my $string = <<'HERE'; -one <one:i> First parameter HERE require Getopt::Declare; import Getopt::Declare $string => $options;