Hi Monks,

What I'm trying to do is take one named option from the command line and run a subroutine based on that option and use the remaining named options in the subroutine. I cannot figure out how to get the options/variables in to the subroutine. For one thing I thought the scope of the variables in Main:: would allow the variable to be seen in side the subroutine. What I'd really rather do is pass them in to the subroutine because it seems better to me. Is that true? Not to mention the fact that it seems that I'm also making this way to hard.

use strict; use warnings; use Getopt::Long; my $user; my $password; GetOptions( 'fetch' => \&fetch, 'user=s' => \$user, 'password=s' => \$password, ); sub fetch { my @vars = @_; print "We're now in fetch\n"; print "The vars are @vars\n"; print "The user is $user\n"; print "The password is $password\n"; routine1($user); } sub routine1 { my @var = @_; print "We're now in routine1\n"; print "The vars for routine1 @var\n"; print "The user for routine1 is $user\n"; }
Which produces these errors:
./mygetopt5.pl -f -user user1 -pass password We're now in fetch The vars are fetch 1 Use of uninitialized value in concatenation (.) or string at ./mygetop +t5.pl line 22. The user is Use of uninitialized value in concatenation (.) or string at ./mygetop +t5.pl line 23. The password is We're now in routine1 Use of uninitialized value in join or string at ./mygetopt5.pl line 32 +. The vars for routine1 Use of uninitialized value in concatenation (.) or string at ./mygetop +t5.pl line 33. The user for routine1 is

Thank-you for your consideration.


In reply to Passing variables to subroutines using Getopt::Long by gctaylor1

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.