I want to call subroutines in Perl like:
sub temp {
---- some code -----
}
temp(-switchName, value1, --switchName2, value2)
Like I know Getopt::Long is there for command line switches type arguments.
So I want to know for subroutine type arguments.
There’s a number of reasons why one might want to do that, but you didn’t say so I’m going to have to speculate some.
Switches on the command line are useful because command line programs mix up options with a list of arguments and need some way of knowing the difference. Thus the “things which start with – are not regular arguments” convention.
You can certainly do something similar with a subroutine, where it picks through its list of arguments looking for things that start with
--and implying the next in the list is the associated value… but subroutines have better and easier ways to do it.In this case the main list of arguments are passed in first as an array reference, and the options are passed in second in a hash reference. There’s no ambiguity.
You can even take this one step further and pass in everything as an option.
That’s useful if it’s not so clear what’s an option and what’s an argument. Probably overkill for the “list of files with options” example here.