In addition to what has been said, you could make the array variable "aminos" global, so that it can be accessed by all. But it's better to make a variable available close to where it's are needed.

Your values to your scalar variables maybe gotten from the CLI and checked if initialized.

Modifying your post a bit like this works, but you might still have to work on it or incorporate it with your code.

use warnings; use strict; use Getopt::Long qw(:config gnu_getopt); help() if $ARGV[0] !~ /^--/; my @aminos = qw (A R N D C Q E G H I L K M F P S T W Y V B Z X); GetOptions( 'substr1=s' => \my $substr1, 'substr2=s' => \my $substr2, 'sub' => \&a, 'help|?' => \&help, ); sub a { if ( "$substr1$substr2" =~ /[^@aminos]/ ) { print "hello\n"; } print "@aminos\n"; } sub help { print "Usage: script --substr1 value --substr2 value --sub" +; exit }
You might have to check the usage of Getopt::Long again.

Note, I didn't check for the initialization of substr1 and 2


In reply to Re: array empty when calling script with getopt::long by Anonymous Monk
in thread array empty when calling script with getopt::long by madM

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.