in reply to Best Practice: How do I pass option parameters to functions?
Generally speaking I'm in favour of splitting as much logic as possible out into object-oriented modules.
#!/usr/bin/env perl use strict; use warnings; # This could go into a separate ".pm" file. { package DirectoryProcessor; use Class::Tiny { script => sub { $ENV{DIR_PROCESSOR_SCRIPT} || "echo -e" }, verbose => sub { 0 }, }; use Getopt::Long qw( GetOptionsFromArray ); sub process { my $self = shift; my ($dir) = @_; my $cmd = sprintf('%s %s', $self->script, $dir); chomp( my $ret = `$cmd` ); print qq(Output: "$ret"\n) if $self->verbose; } sub process_all { my $self = shift; $self->process($_) for @_; } sub handle_argv { my $class = shift; my ($argv, $opt) = @_; $argv ||= \@ARGV; $opt ||= {}; GetOptionsFromArray($argv, $opt, 'script=s', 'verbose!'); my $self = $class->new($opt); $self->process_all(@$argv); } } # Here's the body of the script. DirectoryProcessor->handle_argv;
|
|---|