in reply to GetOpts not working:

Without using any modules.
use strict; use warnings; use v5.16; my $infile; my $outfile; if ($#ARGV <= 2 || $#ARGV >= 4){ die "Silly you, there are an incorrect amount of args :)\n" } for ( 0 .. $#ARGV / 2 ) { my $switch = shift(@ARGV); my $value = shift(@ARGV); given ($switch) { when ('-f1') { $infile = $value } when ('-f2') { $outfile = $value } } } print "Infile: $infile\n", "Outfile: $outfile";
Update: changed 'if ($#ARGV % 2 == 0)' to 'if ($#ARGV <= 2 || $#ARGV >= 4)'

Replies are listed 'Best First'.
Re^2: GetOpts not working:
by GrandFather (Saint) on Dec 09, 2015 at 17:00 UTC

    From which it can be seen that there is some virtue in using modules, especially when they are core! Code is:

    • generally clearer
    • easier to maintain
    • more likely to work first time
    • shorter
    • easier to write tests for

    And modules generally have decent documentation with examples and save rewriting wheels badly.

    Premature optimization is the root of all job security

      with examples and save rewriting wheels badly.

      Ah, the old subliminal hint ;) haha. I def understand what you mean though, I was just giving an example :)
Re^2: GetOpts not working:
by MidLifeXis (Monsignor) on Dec 09, 2015 at 16:04 UTC

    As long as experimental feature warnings are not a concern.

    --MidLifeXis

Re^2: GetOpts not working:
by stevieb (Canon) on Dec 09, 2015 at 16:44 UTC

    Here's a similar way, but instead of using the experimental switch, I use a dispatch table. Note this destroys @ARGV in the process and definitely is only an example and shouldn't be used in production code without much more work.

    use warnings; use strict; my ($file, $thing); my %arg_table = ( -f => sub { $file = shift; }, -t => sub { $thing = shift; }, default => sub { die "bad param"; }, ); while (my @args = splice @ARGV, 0, 2){ if (defined $arg_table{$args[0]}){ $arg_table{$args[0]}->($args[1]); } else { $arg_table{default}->(); } } print $file if defined $file; print $thing if defined $thing;
Re^2: GetOpts not working:
by james28909 (Deacon) on Dec 10, 2015 at 02:39 UTC
    Heres another way:
    use strict; use warnings; my @ARGV = qw(-infile data_in.bin -outfile data_out.bin); my %args; for ( 0 .. $#ARGV / 2 ) { my $switch = shift(@ARGV); my $value = shift(@ARGV); $args{$switch} = $value; } print "Infile: $args{-infile}\n", "Outfile: $args{-outfile}" if ( exis +ts $args{-infile} && exists $args{-outfile} );
    I am growing to really like hashes, and I think im about ready to start doing like hash of arrays, so i can have multiple values for a single hash key. Isnt that how this works?
    my %hash = { key => [value_1, value_2, value_3, value_4]};
    I just wonder how i would print all the values
    Edit: I figured it out, thanks

      There appears to be no benefit to your use of a for loop in that code. Standard hash population means that it isn't needed.

      use strict; use warnings; my @ARGV = qw(-infile data_in.bin -outfile data_out.bin); my %args = @ARGV; print "Infile: $args{-infile}\n", "Outfile: $args{-outfile}" if ( exis +ts $args{-infile} && exists $args{-outfile} );

      works just the same. HTH.

      PS. I wouldn't use @ARGV for a lexical variable name since it ordinarily has a special meaning.

        I tested it and it does indeed work. How is this possible? How does it know to put the filenames into their respective places? Does it somehow auto assign a key/value pair? Im just as confused lol, because I thought you had to shift the array to get the next value, but somehow it automatically did it :0 This is what i did:
        use strict; use warnings; my %args = @ARGV; print "Infile: $args{-infile}\n", "Outfile: $args{-outfile}" if ( exis +ts $args{-infile} && exists $args{-outfile} );

        I fed it '-infile data_in.bin -outfile data_out.bin'. and it almost seems like it has assigned key/value pairs based off of my thought patterns lol <.<

        I guess what i am asking is, how did it build the hash? and why did my %args = @ARGV; build it correctly like that. How is 'data_in.bin' and 'data_out.bin' not a key? if key -infile contains value 'data_in.bin' then what assigned 'data_in.bin' to be its value? there was no shifting or anything to loop through the input args