gctaylor1 has asked for the wisdom of the Perl Monks concerning the following question:
While I was searching for examples related to Getopt::Long I came across this code http://perlbuzz.com/mechanix/2008/05/use-getoptlong-even-if-you-don.html that I just cannot figure out.
use Getopt::Long; GetOptions( 'user=s' => \$user, 'password=s' => \$pass, forms => sub { push( @actions, \&dump_forms ); }, links => sub { push( @actions, \&dump_links ); }, images => sub { push( @actions, \&dump_images ); }, all => sub { push( @actions, \&dump_forms, \&dump_link +s, \&dump_images ); }, absolute => \$absolute, 'agent=s' => \$agent, 'agent-alias=s' => \$agent_alias, help => sub { pod2usage(1); }, ) or pod2usage(2);
This is what I've added and what I'm working with:
use strict; use warnings; use Getopt::Long; my ($user, $pass, $absolute, $agent, $agent_alias, $forms ); my @actions; GetOptions( 'user=s' => \$user, 'password=s' => \$pass, forms => sub { push( @actions, \&dump_forms ); }, links => sub { push( @actions, \&dump_links ); }, images => sub { push( @actions, \&dump_images ); }, all => sub { push( @actions, \&dump_forms, \&dump_link +s, \&dump_images ); }, absolute => \$absolute, 'agent=s' => \$agent, 'agent-alias=s' => \$agent_alias, help => sub { pod2usage(1); }, ) or pod2usage(2); print "$user\n"; print "$pass\n"; $actions[dump_forms()]; sub dump_forms { my @vars = @_; print "@vars\n"; print "we're now in dump_forms\n";}
In particular, where | how | why would I use forms => sub { push( @actions, \&dump_forms ); }, ? I mean why would I want to push a subroutine to an array? Also, I can't seem to get the parameters passed to my sub dump_forms {
Here's example output
Thank-you for your consideration.$ ./mygetopt3.pl --user fred --password password --forms a Useless use of array element in void context at ./mygetopt3.pl line 35 +. fred password we're now in dump_forms
UPDATE
I was asked below about what I'm trying to do, why I didn't look at the examples from mech-dump and why I'm using the command line options that are geared more for mech-dump.
I suppose the questions were asked somewhat in a rhetorical sense but since monks were kind enough to respond to my question, I thought I'd provide context.
The long answer is that I'd asked a couple of questions What's the best way to make my script execute different sections without using goto? and Adding a dispatch table and getting "Variable $x will not stay shared" errors. over the last few weeks about how to have multiple actions in a script. Mostly the responses pointed to Getopt::Long. But I couldn't figure out from Getopt::Long how to make it work. Yes I know that's lame because now that I understand how it works, it's dead simple. So I went on a quest to find examples and http://perlbuzz.com/mechanix/2008/05/use-getoptlong-even-if-you-don.html was one of the only google matches out of the first 30 that didn't point me right back to the docs at Getopt::Long. Being desperate and frustrated I used that as my guide and when I looked at ack or prove I didn't see the examples for using Getopt::Long.
The one place I didn't look was the source for mech-dump, which as it turns out, does explain it so that I could have understood it.
What's crazy is that I was thinking about writing a tutorial and just found that one already exists Parsing your script's command line that I didn't see previously despite searching a few different times.
So I hope that provides a little context and is useful to someone. And I really do appreciate the help and advice.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getopt::Long. forms => sub { push( @actions, \&dump_forms ); },
by ikegami (Patriarch) on Mar 12, 2009 at 04:49 UTC | |
|
Re: Getopt::Long. forms => sub { push( @actions, \&dump_forms ); },
by ELISHEVA (Prior) on Mar 12, 2009 at 05:14 UTC | |
|
Re: Getopt::Long. forms => sub { push( @actions, \&dump_forms ); },
by Marshall (Canon) on Mar 12, 2009 at 10:35 UTC |