简体   繁体   中英

Pass high number of arguments to Perl subroutine or?

I've written a few perl scripts to generate some HTML files. There is one main script which takes in arguments from the user, then this script takes the arguments and passes some of them along to other scripts which contain subroutines to generate the HTML. It's working great, but I keep thinking of new things to add which results in me needing more parameters for the scripts. I'm up to 7 arguments on the main script and 6 for the subroutines and I feel like it's going to be difficult to modify this code in the future as it is beginning to look confusing. Is there a more reasonable/organized way to pass a bunch of arguments to a script?

Thanks for any suggestions!

Pass a hash reference, eg

sub named_parameters { ... }
named_parameters(
    {
        foo => $val1,
        bar => $val2,
        baz => ...
    }
);

Then you can access the arguments by name:

my $arg_ref = shift;
do_stuff_with $arg_ref->{foo};
...

You could also pass a list of key/value pairs (or an ordinary hash), and use that:

my %arg = @_;

This could cause headaches with mixed named and positional parameters, but that's a rather debaucherous way to design a function's interface which I wouldn't recommend anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM