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

Hi,

I have a parameter.ph file that stores only parameters like:

my $i = 2; my $j = 10;

I have included this parameter.ph file in the perl scripts where I am using those parameters.

Is there a way I can change these parameters from the command line using shell?

Thank you.

Replies are listed 'Best First'.
Re: Changing parameters
by davido (Cardinal) on Nov 29, 2004 at 16:22 UTC

    You can't directly type, "myscript $i=200 on the command line and expect it to do what you want. However, it is fairly straightforward to define a default value that can be overridden by some other means.

    For example:

    my $i = 2; if( exists( $ARGV[0] ) ) { $i = $ARGV[0] };

    But if you have anything beyond the most basic of needs in this respect, I suggest you have a look at Getopt::Long, or Getopt::Std. It's best to let one of those modules handle your command-line parsing for you.


    Dave

Re: Changing parameters
by gaal (Parson) on Nov 29, 2004 at 16:27 UTC
    You mean, is there a one-liner to update a value in such a file? Sure.

    # one way to do it. Changes "$i = 2" to "$i = 20". perl -lpi.bak -e 'BEGIN { ($var, $val) = @ARGV; @ARGV = qw/parameter.p +h/ } s/^(my \$$var =).*/$1 $val;/' i 20

    (This is untested.) You also get a free backup file named parameter.ph.bak .

    By the way, any reason you're using the ".ph" extension?

      Hi,

      Thanks for the response.

      Yes, I basically need a one-liner to change one or many parameters in the parameter.ph file from the command-line.

      The reason I am using a .ph extension is because this was strictly a parameter file and I kind of saw something similar online.

      Is there a better way of having a parameter file in perl?

      Thanks.

        My offering only changes one setting at a time.

        Yes, there is something better, but what it is exactly depends on how complex your parameters are, what kind of validations you'll want to be doing on them, and so on. But the baisic thing about configuration is that if they are in a data file, you can give them to someone who isn't familiar with Perl syntax for editing and they have less chance of breaking things if they leave off a semicolon somewhere.

        Very often INI-style config files win over thanks to their simplicity. Take a look over at CPAN -- they have plenty of modules that handle this format. My personal favorite is Config::Tiny, but shop around. (If your config data is flat, for example, you might not like Config::Tiny's "_" level.)

        For more complex data — long text, arrays, maps and so on — YAML is a nice choice. It also has a Perl module to handle it. See if you like it better! (Some people dislike its sensitivity to indentation.)

Re: Changing parameters
by fglock (Vicar) on Nov 29, 2004 at 16:24 UTC

    Do you mean you want to edit parameter.ph from the command line?

    perl -pi.bak -e 's!\$i = 2;!\$i = 4;!' parameter.ph
      Hi,

      Thanks for the reply.

      Is there another way of changing both the values of i and j without knowing what their current values are?

      Your suggestion above does work, but it requires knowing what the current values are of i inorder to change it from 2 to 4.

      Thank you.

        How about:

        perl -pi.bak -e 's!\$i = .*?;!\$i = 4;!' parameter.ph
        You could just append new values to the file.
        echo '$i = 987;' >> parameter.ph
        That will override the old values.

        If you do this often, and do not want the file to grow every time, save the original file and output to a new one.

        cp defaults.ph parameter.ph; echo '$i = 987;' >> parameter.ph
Re: Changing parameters
by exussum0 (Vicar) on Nov 29, 2004 at 16:23 UTC
    You'll need to build in a little logic, and @ARGV. I guess it depends on what your code organization looks like.

    ----
    Then B.I. said, "Hov' remind yourself nobody built like you, you designed yourself"