Hi Monks,

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

$ ./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
Thank-you for your consideration.

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.


In reply to Getopt::Long. forms => sub { push( @actions, \&dump_forms ); }, by gctaylor1

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.