Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Parameters to make-invoked script

by Sixtease (Friar)
on Jul 23, 2008 at 07:20 UTC ( #699528=perlquestion: print w/replies, xml ) Need Help??
Sixtease has asked for the wisdom of the Perl Monks concerning the following question:

Hello people.

My module runs a script upon make. I'd like to let people pass arguments to this script. So far, I've been doing it through environment variables. Is there a better way?

use strict; use warnings; print "Just Another Perl Hacker\n";

Replies are listed 'Best First'.
Re: Parameters to make-invoked script
by Corion (Pope) on Jul 23, 2008 at 07:33 UTC
Re: Parameters to make-invoked script
by ikegami (Pope) on Jul 23, 2008 at 08:08 UTC

    I can't find any method to pass variables to the Makefile other than by the environment, but

    • It's simple and clean to pass environment variable to the Makefile
    • It's simple and clean to use environment variable in the Makefile (in sh, ksh, bash and zsh at least)
    • The environment variables can be passed to the Perl script as arguments even if they were passed to the Makefile as environment variables.
    $ cat #!/usr/bin/perl print("$ARGV[0]\n"); $ cat Makefile testing: $(TEXT) > $@ $ TEXT=moo make testing moo > testing $ cat testing moo

    I feel this meets your needs. If not, let us know how it doesn't.

      Thanks for the input!

      I use ExtUtils::MakeMaker, so messing with Makefile itself is something I'd like to avoid. If you think that env variables are something I can use to tweak the details of the build process without shame, then I'm very much satisfied. I'll simply take care of the environment variables in the script, that's no problem.

      Thank you once again.

      use strict; use warnings; print "Just Another Perl Hacker\n"; $(TEXT) > $@

      Note that you will have a really hard time making sure that there are no shell special characters in TEXT. Unfortunately, there is no such thing as system() with several arguments in make. I believe some make programs provide implementation-specific means for quoting strings. However, if you expect TEXT to have strange characters, you'd be better off using the environment.

Re: Parameters to make-invoked script
by Perlbotics (Chancellor) on Jul 23, 2008 at 09:07 UTC
    I am only familiar with GNU make, but there you can set/override variables when calling make. Not sure if this work with other make flavours?
    For example (tested with GNU make 3.81):
    pb:/tmp> cat Makefile testing: @echo "extern : myvar='$(myvar)'" @echo "default: CPP='$(CPP)'" pb:/tmp> make extern : myvar='' default: CPP='cc -E' pb:/tmp> make myvar=Tim\ Towdy extern : myvar='Tim Towdy' default: CPP='cc -E' pb:/tmp> make myvar=gcc CPP='$(myvar)' extern : myvar='gcc' default: CPP='gcc'

      Hell yeah, I was looking for exactly this. I'll add this mechanism and keep the environment variables for compatibility with dumber make's. Thanks pal.

      Update: Oh wait, can I add this functionality from Makefile.PL?

      use strict; use warnings; print "Just Another Perl Hacker\n";
Re: Parameters to make-invoked script
by Bloodnok (Vicar) on Jul 23, 2008 at 09:50 UTC

    The 'normal' way to pass run-time arguments to a make(1) script is via make(1) macros declared on the CLI e.g. if using Test::More, a verbose listing can be generated using either of the 2 following equivalent methods...

    Assuming Windoze...

    set TEST_VERBOSE=t dmake test

    Assuming sh (or variant thereof:-)...

    export TEST_VERBOSE=t dmake test

    dmake test TEST_VERBOSE=t

    Note that environment variables are referenced in the same way as macros within the make(1) script, hence CLI macros override any environment variable(s) of the same name.


    At last, a user level that overstates my experience :-))

      Wow, then this means that... I can assume environment variables in the script and then with no change to the makefile use the format

      make VAR=value

      and it will work!

      ... goes and tests it ...

      ... and it works! :-)

      Then my problem is solved and I need not do anything, only document it. :-)) Great, thanks.

      use strict; use warnings; print "Just Another Perl Hacker\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://699528]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2018-06-24 13:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.