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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |