xref: /openbsd-src/gnu/usr.bin/perl/cpan/ExtUtils-PL2Bat/lib/ExtUtils/PL2Bat.pm (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1256a93a4Safresh1package ExtUtils::PL2Bat;
2*f2a19305Safresh1$ExtUtils::PL2Bat::VERSION = '0.005';
3256a93a4Safresh1use strict;
4256a93a4Safresh1use warnings;
5256a93a4Safresh1
6256a93a4Safresh1use 5.006;
7256a93a4Safresh1
8256a93a4Safresh1use Config;
9256a93a4Safresh1use Carp qw/croak/;
10256a93a4Safresh1
11256a93a4Safresh1# In core, we can't use any other modules except those that already live in
12256a93a4Safresh1# lib/, so Exporter is not available to us.
13256a93a4Safresh1sub import {
14256a93a4Safresh1	my ($self, @functions) = @_;
15256a93a4Safresh1	@functions = 'pl2bat' if not @functions;
16256a93a4Safresh1	my $caller = caller;
17256a93a4Safresh1	for my $function (@functions) {
18256a93a4Safresh1		no strict 'refs';
19256a93a4Safresh1		*{"$caller\::$function"} = \&{$function};
20256a93a4Safresh1	}
21256a93a4Safresh1}
22256a93a4Safresh1
23256a93a4Safresh1sub pl2bat {
24256a93a4Safresh1	my %opts = @_;
25256a93a4Safresh1
26256a93a4Safresh1	# NOTE: %0 is already enclosed in doublequotes by cmd.exe, as appropriate
27256a93a4Safresh1	$opts{ntargs}    = '-x -S %0 %*' unless exists $opts{ntargs};
28256a93a4Safresh1	$opts{otherargs} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $opts{otherargs};
29256a93a4Safresh1
30256a93a4Safresh1	$opts{stripsuffix} = qr/\.plx?/ unless exists $opts{stripsuffix};
31256a93a4Safresh1
32256a93a4Safresh1	if (not exists $opts{out}) {
33256a93a4Safresh1		$opts{out} = $opts{in};
34256a93a4Safresh1		$opts{out} =~ s/$opts{stripsuffix}$//i;
35256a93a4Safresh1		$opts{out} .= '.bat' unless $opts{in} =~ /\.bat$/i or $opts{in} eq '-';
36256a93a4Safresh1	}
37256a93a4Safresh1
38256a93a4Safresh1	my $head = <<"EOT";
39256a93a4Safresh1	\@rem = '--*-Perl-*--
40256a93a4Safresh1	\@set "ErrorLevel="
41256a93a4Safresh1	\@if "%OS%" == "Windows_NT" \@goto WinNT
42256a93a4Safresh1	\@perl $opts{otherargs}
43256a93a4Safresh1	\@set ErrorLevel=%ErrorLevel%
44256a93a4Safresh1	\@goto endofperl
45256a93a4Safresh1	:WinNT
46256a93a4Safresh1	\@perl $opts{ntargs}
47256a93a4Safresh1	\@set ErrorLevel=%ErrorLevel%
48256a93a4Safresh1	\@if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" \@goto endofperl
49256a93a4Safresh1	\@if %ErrorLevel% == 9009 \@echo You do not have Perl in your PATH.
50256a93a4Safresh1	\@goto endofperl
51256a93a4Safresh1	\@rem ';
52256a93a4Safresh1EOT
53256a93a4Safresh1
54256a93a4Safresh1	$head =~ s/^\s+//gm;
55256a93a4Safresh1	my $headlines = 2 + ($head =~ tr/\n/\n/);
56256a93a4Safresh1	my $tail = <<'EOT';
57256a93a4Safresh1	__END__
58256a93a4Safresh1	:endofperl
59256a93a4Safresh1	@set "ErrorLevel=" & @goto _undefined_label_ 2>NUL || @"%COMSPEC%" /d/c @exit %ErrorLevel%
60256a93a4Safresh1EOT
61256a93a4Safresh1	$tail =~ s/^\s+//gm;
62256a93a4Safresh1
63256a93a4Safresh1	my $linedone = 0;
64256a93a4Safresh1	my $taildone = 0;
65256a93a4Safresh1	my $linenum = 0;
66256a93a4Safresh1	my $skiplines = 0;
67256a93a4Safresh1
68256a93a4Safresh1	my $start = $Config{startperl};
69256a93a4Safresh1	$start = '#!perl' unless $start =~ /^#!.*perl/;
70256a93a4Safresh1
71256a93a4Safresh1	open my $in, '<', $opts{in} or croak "Can't open $opts{in}: $!";
72256a93a4Safresh1	my @file = <$in>;
73256a93a4Safresh1	close $in;
74256a93a4Safresh1
75256a93a4Safresh1	foreach my $line ( @file ) {
76256a93a4Safresh1		$linenum++;
77256a93a4Safresh1		if ( $line =~ /^:endofperl\b/ ) {
78256a93a4Safresh1			if (!exists $opts{update}) {
79256a93a4Safresh1				warn "$opts{in} has already been converted to a batch file!\n";
80256a93a4Safresh1				return;
81256a93a4Safresh1			}
82256a93a4Safresh1			$taildone++;
83256a93a4Safresh1		}
84256a93a4Safresh1		if ( not $linedone and $line =~ /^#!.*perl/ ) {
85256a93a4Safresh1			if (exists $opts{update}) {
86256a93a4Safresh1				$skiplines = $linenum - 1;
87256a93a4Safresh1				$line .= '#line '.(1+$headlines)."\n";
88256a93a4Safresh1			} else {
89256a93a4Safresh1	$line .= '#line '.($linenum+$headlines)."\n";
90256a93a4Safresh1			}
91256a93a4Safresh1	$linedone++;
92256a93a4Safresh1		}
93256a93a4Safresh1		if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
94256a93a4Safresh1			$line = '';
95256a93a4Safresh1		}
96256a93a4Safresh1	}
97256a93a4Safresh1
98256a93a4Safresh1	open my $out, '>', $opts{out} or croak "Can't open $opts{out}: $!";
99256a93a4Safresh1	print $out $head;
100256a93a4Safresh1	print $out $start, ( $opts{usewarnings} ? ' -w' : '' ),
101256a93a4Safresh1						 "\n#line ", ($headlines+1), "\n" unless $linedone;
102256a93a4Safresh1	print $out @file[$skiplines..$#file];
103256a93a4Safresh1	print $out $tail unless $taildone;
104256a93a4Safresh1	close $out;
105256a93a4Safresh1
106256a93a4Safresh1	return $opts{out};
107256a93a4Safresh1}
108256a93a4Safresh1
109256a93a4Safresh11;
110256a93a4Safresh1
111256a93a4Safresh1# ABSTRACT: Batch file creation to run perl scripts on Windows
112256a93a4Safresh1
113256a93a4Safresh1__END__
114256a93a4Safresh1
115256a93a4Safresh1=pod
116256a93a4Safresh1
117256a93a4Safresh1=encoding UTF-8
118256a93a4Safresh1
119256a93a4Safresh1=head1 NAME
120256a93a4Safresh1
121256a93a4Safresh1ExtUtils::PL2Bat - Batch file creation to run perl scripts on Windows
122256a93a4Safresh1
123256a93a4Safresh1=head1 VERSION
124256a93a4Safresh1
125*f2a19305Safresh1version 0.005
126256a93a4Safresh1
127256a93a4Safresh1=head1 OVERVIEW
128256a93a4Safresh1
129256a93a4Safresh1This module converts a perl script into a batch file that can be executed on Windows/DOS-like operating systems.  This is intended to allow you to use a Perl script like regular programs and batch files where you just enter the name of the script [probably minus the extension] plus any command-line arguments and the script is found in your B<PATH> and run.
130256a93a4Safresh1
131256a93a4Safresh1=head1 FUNCTIONS
132256a93a4Safresh1
133256a93a4Safresh1=head2 pl2bat(%opts)
134256a93a4Safresh1
135256a93a4Safresh1This function takes a perl script and write a batch file that contains the script. This is sometimes necessary
136256a93a4Safresh1
137256a93a4Safresh1=over 8
138256a93a4Safresh1
139256a93a4Safresh1=item * C<in>
140256a93a4Safresh1
141256a93a4Safresh1The name of the script that is to be batchified. This argument is mandatory.
142256a93a4Safresh1
143256a93a4Safresh1=item * C<out>
144256a93a4Safresh1
145256a93a4Safresh1The name of the output batch file. If not given, it will be generated using C<in> and C<stripsuffix>.
146256a93a4Safresh1
147256a93a4Safresh1=item * C<ntargs>
148256a93a4Safresh1
149256a93a4Safresh1Arguments to invoke perl with in generated batch file when run from
150256a93a4Safresh1Windows NT.  Defaults to S<'-x -S %0 %*'>.
151256a93a4Safresh1
152256a93a4Safresh1=item * C<otherargs>
153256a93a4Safresh1
154256a93a4Safresh1Arguments to invoke perl with in generated batch file except when
155256a93a4Safresh1run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
156256a93a4Safresh1Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
157256a93a4Safresh1
158256a93a4Safresh1=item * C<stripsuffix>
159256a93a4Safresh1
160256a93a4Safresh1Strip a suffix string from file name before appending a ".bat"
161256a93a4Safresh1suffix.  The suffix is not case-sensitive.  It can be a regex or a string and a trailing
162256a93a4Safresh1C<$> is always assumed).  Defaults to C<qr/\.plx?/>.
163256a93a4Safresh1
164256a93a4Safresh1=item * C<usewarnings>
165256a93a4Safresh1
166256a93a4Safresh1With the C<usewarnings>
167256a93a4Safresh1option, C<" -w"> is added after the value of C<$Config{startperl}>.
168256a93a4Safresh1If a line matching C</^#!.*perl/> already exists in the script,
169256a93a4Safresh1then it is not changed and the B<-w> option is ignored.
170256a93a4Safresh1
171256a93a4Safresh1=item * C<update>
172256a93a4Safresh1
173256a93a4Safresh1If the script appears to have already been processed by B<pl2bat>,
174256a93a4Safresh1then the script is skipped and not processed unless C<update> was
175256a93a4Safresh1specified.  If C<update> is specified, the existing preamble is replaced.
176256a93a4Safresh1
177256a93a4Safresh1=back
178256a93a4Safresh1
179256a93a4Safresh1=head1 ACKNOWLEDGEMENTS
180256a93a4Safresh1
181256a93a4Safresh1This code was taken from Module::Build and then modified; which had taken it from perl's pl2bat script. This module is an attempt at unifying all three implementations.
182256a93a4Safresh1
183256a93a4Safresh1=head1 AUTHOR
184256a93a4Safresh1
185256a93a4Safresh1Leon Timmermans <leont@cpan.org>
186256a93a4Safresh1
187256a93a4Safresh1=head1 COPYRIGHT AND LICENSE
188256a93a4Safresh1
189256a93a4Safresh1This software is copyright (c) 2015 by Leon Timmermans.
190256a93a4Safresh1
191256a93a4Safresh1This is free software; you can redistribute it and/or modify it under
192256a93a4Safresh1the same terms as the Perl 5 programming language system itself.
193256a93a4Safresh1
194256a93a4Safresh1=cut
195