Here's a proposal for better default value handling in Makefile.PL's.
It's basically an extension to the existing prompt() function. If
$ENV{PERL_MM_DEFAULT_DB} is defined, then it is used as a file
location for an AnyDBM_File. User input to prompt() will be stored
into this database and used next time as the default value. Furthermore,
if prompt() is called with a third parameter, then it is used for
parsing the command line for this argument. In this case, the user will
not be queried, but the specified value will go into the database
(this is debatable).
For the implementation: below is a file which can be saved as
ExtUtils/MakeMaker.pm in a private PERLLIB-accessible directory for
playing with the new feature. Because I do not see a way how to
extract the extension name from the Makefile.PL, a new global variable
$ExtUtils::MakeMaker::EXT_NAME should be set in the Makefile.PL to
the extension name, otherwise the default database mechanism won't
work.
Here call examples:
env PERL_MM_DEFAULT_DB=/tmp/perl-defaults.db PERL5LIB=$HOME/lib/perl perl
Makefile.PL
And here the code (which has much room for improvement):
BEGIN {
do "/usr/local/lib/perl5/5.8.0/ExtUtils/MakeMaker.pm";
}
package ExtUtils::MakeMaker;
sub prompt ($;$$) {
my($mess, $def, $opt) = @_;
Carp::confess("prompt function called without an argument")
unless defined $mess;
my $isa_tty = -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)) ;
my %def_db;
my $key = "$ExtUtils::MakeMaker::EXT_NAME $mess"
if defined $ExtUtils::MakeMaker::EXT_NAME;
my $has_opt;
if (defined $opt) {
require Getopt::Long;
my $val;
Getopt::Long::Configure("default", "pass_through");
Getopt::Long::GetOptions($opt => \$val);
if (defined $val) {
$def = $val;
$has_opt = 1;
}
}
if ($ENV{PERL_MM_DEFAULT_DB}) {
if (!defined $ExtUtils::MakeMaker::EXT_NAME) {
warn "\$ExtUtils::MakeMaker::EXT_NAME is not defined, ignoring
PERL_MM_DEFAULT_DB";
} else {
require AnyDBM_File;
use Fcntl;
tie %def_db, "AnyDBM_File", $ENV{PERL_MM_DEFAULT_DB}, O_RDWR|O_CREAT, 0666
or die "Can't tie $ENV{PERL_MM_DEFAULT_DB}: $!";
if (defined $def_db{$key} && !$has_opt) {
$def = $def_db{$key};
}
}
}
if (!$has_opt) {
my $dispdef = defined $def ? "[$def] " : " ";
$def = defined $def ? $def : "";
local $|=1;
local $\;
print "$mess $dispdef";
my $ans;
if ($ENV{PERL_MM_USE_DEFAULT} || (!$isa_tty && eof STDIN)) {
print "$def\n";
}
else {
$ans = <STDIN>;
if( defined $ans ) {
chomp $ans;
}
else { # user hit ctrl-D
print "\n";
}
}
$def = (!defined $ans || $ans eq '') ? $def : $ans;
}
if (tied %def_db) {
$def_db{$key} = $def;
}
return $def;
}
1;
__END__
Regards and waiting for comments,
Slaven
--
Slaven Rezic - slaven <at> rezic <dot> de