From a06cd52b4e4376cb0c4375a8943888ed7c9f37d2 Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Mon, 8 Jul 2013 16:41:30 +0200 Subject: [PATCH] ExtUtils::Miniperl::writemain()'s first argument can also be a filename. Treat a reference to a scalar as the name of a file to open for output. Any other reference is used as an output filehandle. Otherwise the default remains to write to STDOUT. --- ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm | 32 ++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm b/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm index 68a4940..9aaf954 100644 --- a/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm +++ b/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm @@ -10,9 +10,24 @@ use vars qw($VERSION @ISA @EXPORT); @EXPORT = qw(writemain); $VERSION = 1; +# blead will run this with miniperl, hence we can't use autodie or File::Temp +my $temp; + +END { + return if !defined $temp || !-e $temp; + unlink $temp or warn "Can't unlink '$temp': $!"; +} + sub writemain{ - my $fh; - if (ref $_[0]) { + my ($fh, $real); + + if (ref $_[0] eq 'SCALAR') { + $real = ${+shift}; + $temp = $real; + $temp =~ s/(?:.c)?\z/.new/; + open $fh, '>', $temp + or die "Can't open '$temp' for writing: $!"; + } elsif (ref $_[0]) { $fh = shift; } else { $fh = \*STDOUT; @@ -178,6 +193,11 @@ static void xs_init(pTHX) { EOT + + if ($real) { + close $fh or die "Can't close '$temp': $!"; + rename $temp, $real or die "Can't rename '$temp' to '$real': $!"; + } } 1; @@ -193,6 +213,8 @@ ExtUtils::Miniperl - write the C code for perlmain.c writemain(@directories); # or writemain($fh, @directories); + # or + writemain(\$filename, @directories); =head1 DESCRIPTION @@ -200,9 +222,9 @@ C takes an argument list of directories containing archive libraries that relate to perl modules and should be linked into a new perl binary. It writes a corresponding F file that is a plain C file containing all the bootstrap code to make the -modules associated with the libraries available from within perl. -If the first argument to C is a reference, it -is used as the file handle to write to. Otherwise output is to C. +If the first argument to C is a reference to a scalar it is +used as the filename to open for ouput. Any other reference is used as +the filehandle to write to. Otherwise output defaults to C. The typical usage is from within a Makefile generated by L. So under normal circumstances you won't have to -- 2.7.4