Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, How to pass a command line argument received in main program back to a module. # main program $conf_file = ARGV[0]; # Module.pm In this module i have a lot of subroutines that uses this onfiguration file and for the subroutines the parameters picked up from the configuration file is same. Currently i am passing the configuration file to each subroutine and in each subroutine i am parsing the configuration file to receive the inputs. I want to know if there is a way to pass this $conf_file name to the module so that i can get all the inputs from the configuration file once, and then all the subroutines can use the same inputs.
  • Comment on How to pass a value from main program back to a module

Replies are listed 'Best First'.
Re: How to pass a value from main program back to a module
by ForgotPasswordAgain (Vicar) on Feb 22, 2008 at 12:50 UTC

    Welcome to object-oriented programming. Your module represents an object. You could, for example, pass this config file name as an argument and have a config_file attribute. How you do OO programming is a good topic for trolling, but the basic idea is something like:

    package Module; sub new { my ($pkg, %args) = @_; my $self = bless {}, $pkg; $self->init(\%args); return $self; } sub init { my ($self, $args) = @_; if (exists $args->{config_file}) { my $file = $args->{config_file}; if (-f $file) { $self->{config_file} = $args->{config_file}; } else { die "config file '$file' not found, blah\n"; } } # other initialization } # this kind of "accessor" thing is so boring and repetitious # that many people have made modules to handle it sub config_file { my ($self, $val) = @_; if (defined $val) { $self->{config_file} = $val; } return $self->{config_file}; } # the rest of the wonderful things your module does sub some_other_method { my ($self, $blah) = @_; .... open (my $fh, $self->{config_file}) || suicide; # you might have a data structure (like a hash) # to store the results of the configuration; # or better, you could look on CPAN to see if # someone already has done a configuration module # (as indeed they have) } 1;

    There are probably at least a dozen things I did there that you can nitpick or do differently, but that's the general idea.

Re: How to pass a value from main program back to a module
by friedo (Prior) on Feb 22, 2008 at 12:52 UTC

    You can set a global variable in the module, for example $Module::conf_file = $ARGV[0]. Within Module.pm, any reference to $conf_file will then have that value.

    Alternatively, you could restructure your module as an object-oriented class, pass the $conf_file as a parameter to the constructor, and save it in the object.

Re: How to pass a value from main program back to a module
by cdarke (Prior) on Feb 22, 2008 at 13:29 UTC
    You can access @ARGV from anywhere in your program, including a module, not just main. But don't forget the '$' prefix:
    my $conf_file = $ARGV[0];
      Thanks all ( ForgotPasswordAgain, friedo and cdarke ) I would use $conf_file = $ARGV[0]. What makes me thinks is that " I have been working in perl from last 2 years " and never realised that there could be a simple solution like this.