CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [FishMonger] Need help with AutoComplete ARGV option: Edit Log



FishMonger
Veteran / Moderator

May 9, 2016, 10:02 AM


Views: 62893
Re: [FishMonger] Need help with AutoComplete ARGV option

Here's another example using the dispatch table and the argument handling is closer to what you are wanting.


Code
#!/usr/bin/perl 

use 5.010;
use strict;
use warnings;
use Getopt::Long;

my %dispatch = (
admin => \&admin,
backup => \&backup,
login => \&login,
start => \&start,
steam => \&steam,
default => \&default,
);

my $action = 'default';
my $remote;

GetOptions (
'admin' => sub { $action = shift },
'backup' => sub { $action = shift },
'login' => sub { $action = shift },
'start' => sub { $action = shift },
'steam' => sub { $action = shift },
'remote=s' => \$remote,
)
or die("Error in command line arguments\n");

$dispatch{$action}->($remote);

sub admin {
my $host = shift;
say "doing 'admin' things on $host";
}

sub backup {
my $host = shift;
say "doing 'backup' things on $host";
}

sub login {
my $host = shift;
say "doing 'login' things on $host";
}

sub start {
my $host = shift;
say "doing 'start' things on $host";
}

sub steam {
my $host = shift;
say "doing 'steam' things on $host";
}

sub default {
my $host = shift;
say "doing 'default' things on $host";
}



Quote
c:\test>Perl-1.pl -st
Option st is ambiguous (start, steam)
Error in command line arguments

c:\test>Perl-1.pl -r localhost -b
doing 'backup' things on localhost

c:\test>Perl-1.pl -a -r localhost
doing 'admin' things on localhost



(This post was edited by FishMonger on May 9, 2016, 10:29 AM)


Edit Log:
Post edited by FishMonger (Veteran) on May 9, 2016, 10:06 AM
Post edited by FishMonger (Veteran) on May 9, 2016, 10:10 AM
Post edited by FishMonger (Veteran) on May 9, 2016, 10:25 AM
Post edited by FishMonger (Veteran) on May 9, 2016, 10:26 AM
Post edited by FishMonger (Veteran) on May 9, 2016, 10:29 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives