Code never stands in isolation - when a text says it is drawing its code from somewhere and the code doesn't make sense on its lonesome, the first thing to do is to look up the code.

The article you are reading drew its code from mech-dump as it states: "Take a look at prove or ack for examples. mech-dump is pretty decent as an example as well:...". It even gave you a link to the code in question so you could see it in context. So the first help I can give is to suggest you look up the source code (either via the link given in the article or via CPAN).

The list of options is not magic - they reflect the command line syntax for a specific command. Unless you are trying to reproduce the command mech-dump, your command line and list of options should look entirely different. Which brings me to a question of my own: why are you using the command line options for mech-dump? If as a learning exercise I would start with a simpler set of options. When you can get that working, experiment with additional features. If you would update your question to tell what features you are trying to learn about GetOpt::Long, perhaps we might be able to suggest a better sequence of learning problems.

As for your second question, "why push a function onto an array?", that too will be made clearer reading the code for mech-dump. Each of the options --forms, --links, --images is a boolean flag. mech-dump uses these flags to tell what code is needed to carry out the user's wishes.

It could, of course, set a boolean variable to true whenever the flag is set and then use that boolean variable in an if statement. However, the author of mech-dump probably anticipated that the list of optional actions might change. Or maybe he didn't want to maintain a long sequence of if..elsif.... So instead, everytime a flag is set, the code for handling the flag is added to the list of optional bits of code to exectute. The subroutine is the optional code needed to extract images or whatever. The array holds a list of subroutine references to the optional code.

As you read through the mech_dump source code you may find it instructive to look for the place where the array is being used. Using an array to hold optional bits of code is a good pattern to learn and mech_dump is a fairly simple example of good production use of that pattern.

Best, beth


In reply to Re: Getopt::Long. forms => sub { push( @actions, \&dump_forms ); }, by ELISHEVA
in thread 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.