Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

array empty when calling script with getopt::long

by madM (Beadle)
on Apr 29, 2014 at 01:26 UTC ( [id://1084236]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

im starting a script with the command line using getopt::long. I declare the array aminos at the first of the script and a subroutine should use the declared @aminos when i call it with the option sub but when i call the subroutine... the array @aminos is empty in the subroutine.. if i print @aminos in the suroutine it prints 0... do you know why is this happening and how i can prevent this from happening?
GetOptions ( 'sub' => \&a ) @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); sub a { if( "$substr1$substr2" =~ /[^@aminos]/ ) { print "hello\n"; } print "@aminos\n"; }

Replies are listed 'Best First'.
Re: array empty when calling script with getopt::long
by graff (Chancellor) on Apr 29, 2014 at 01:51 UTC
    Would it be too difficult or complicated for you to explain what sort of option processing you are actually hoping to do? Normally, when someone writes a command-line tool that handles optional args, it will print a synopsis whenever the user types a bad arg, or specifically asks for help.

    Usually, the programmer includes a "-h" option for displaying the synopsis, or simply lets Getopt::Long throw an error when "-h" or "-?" is used as an option, and makes sure to display the synopsis whenever Getopt::Long throws an error.

    What would the synopsis be for your script? Why would you need a subroutine to process your options? What do "$substr1" and "$substr2" contain (where is the code that assigns values to those variables)?

Re: array empty when calling script with getopt::long
by Anonymous Monk on Apr 29, 2014 at 03:57 UTC

    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

Re: array empty when calling script with getopt::long
by Laurent_R (Canon) on Apr 29, 2014 at 06:20 UTC
    Declaring and defining your @amino array before calling your GetOptions subroutine should be a remedy to your immediate problem. But, as already pointed out by other monks, there are a number of other issues in your code, including especially the fact that nobody knows what there is in the $substr1 and $substr2 variables, and that I do not understand what you are trying to achieve exactly with your pattern matching expression.
Re: array empty when calling script with getopt::long
by LanX (Saint) on Apr 29, 2014 at 01:34 UTC
      yes .. sorry for that... and thank you very much for your answer :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1084236]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-25 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found