xref: /openbsd-src/gnu/usr.bin/perl/cpan/ExtUtils-PL2Bat/lib/ExtUtils/PL2Bat.pm (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1package ExtUtils::PL2Bat;
2$ExtUtils::PL2Bat::VERSION = '0.005';
3use strict;
4use warnings;
5
6use 5.006;
7
8use Config;
9use Carp qw/croak/;
10
11# In core, we can't use any other modules except those that already live in
12# lib/, so Exporter is not available to us.
13sub import {
14	my ($self, @functions) = @_;
15	@functions = 'pl2bat' if not @functions;
16	my $caller = caller;
17	for my $function (@functions) {
18		no strict 'refs';
19		*{"$caller\::$function"} = \&{$function};
20	}
21}
22
23sub pl2bat {
24	my %opts = @_;
25
26	# NOTE: %0 is already enclosed in doublequotes by cmd.exe, as appropriate
27	$opts{ntargs}    = '-x -S %0 %*' unless exists $opts{ntargs};
28	$opts{otherargs} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $opts{otherargs};
29
30	$opts{stripsuffix} = qr/\.plx?/ unless exists $opts{stripsuffix};
31
32	if (not exists $opts{out}) {
33		$opts{out} = $opts{in};
34		$opts{out} =~ s/$opts{stripsuffix}$//i;
35		$opts{out} .= '.bat' unless $opts{in} =~ /\.bat$/i or $opts{in} eq '-';
36	}
37
38	my $head = <<"EOT";
39	\@rem = '--*-Perl-*--
40	\@set "ErrorLevel="
41	\@if "%OS%" == "Windows_NT" \@goto WinNT
42	\@perl $opts{otherargs}
43	\@set ErrorLevel=%ErrorLevel%
44	\@goto endofperl
45	:WinNT
46	\@perl $opts{ntargs}
47	\@set ErrorLevel=%ErrorLevel%
48	\@if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" \@goto endofperl
49	\@if %ErrorLevel% == 9009 \@echo You do not have Perl in your PATH.
50	\@goto endofperl
51	\@rem ';
52EOT
53
54	$head =~ s/^\s+//gm;
55	my $headlines = 2 + ($head =~ tr/\n/\n/);
56	my $tail = <<'EOT';
57	__END__
58	:endofperl
59	@set "ErrorLevel=" & @goto _undefined_label_ 2>NUL || @"%COMSPEC%" /d/c @exit %ErrorLevel%
60EOT
61	$tail =~ s/^\s+//gm;
62
63	my $linedone = 0;
64	my $taildone = 0;
65	my $linenum = 0;
66	my $skiplines = 0;
67
68	my $start = $Config{startperl};
69	$start = '#!perl' unless $start =~ /^#!.*perl/;
70
71	open my $in, '<', $opts{in} or croak "Can't open $opts{in}: $!";
72	my @file = <$in>;
73	close $in;
74
75	foreach my $line ( @file ) {
76		$linenum++;
77		if ( $line =~ /^:endofperl\b/ ) {
78			if (!exists $opts{update}) {
79				warn "$opts{in} has already been converted to a batch file!\n";
80				return;
81			}
82			$taildone++;
83		}
84		if ( not $linedone and $line =~ /^#!.*perl/ ) {
85			if (exists $opts{update}) {
86				$skiplines = $linenum - 1;
87				$line .= '#line '.(1+$headlines)."\n";
88			} else {
89	$line .= '#line '.($linenum+$headlines)."\n";
90			}
91	$linedone++;
92		}
93		if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
94			$line = '';
95		}
96	}
97
98	open my $out, '>', $opts{out} or croak "Can't open $opts{out}: $!";
99	print $out $head;
100	print $out $start, ( $opts{usewarnings} ? ' -w' : '' ),
101						 "\n#line ", ($headlines+1), "\n" unless $linedone;
102	print $out @file[$skiplines..$#file];
103	print $out $tail unless $taildone;
104	close $out;
105
106	return $opts{out};
107}
108
1091;
110
111# ABSTRACT: Batch file creation to run perl scripts on Windows
112
113__END__
114
115=pod
116
117=encoding UTF-8
118
119=head1 NAME
120
121ExtUtils::PL2Bat - Batch file creation to run perl scripts on Windows
122
123=head1 VERSION
124
125version 0.005
126
127=head1 OVERVIEW
128
129This 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.
130
131=head1 FUNCTIONS
132
133=head2 pl2bat(%opts)
134
135This function takes a perl script and write a batch file that contains the script. This is sometimes necessary
136
137=over 8
138
139=item * C<in>
140
141The name of the script that is to be batchified. This argument is mandatory.
142
143=item * C<out>
144
145The name of the output batch file. If not given, it will be generated using C<in> and C<stripsuffix>.
146
147=item * C<ntargs>
148
149Arguments to invoke perl with in generated batch file when run from
150Windows NT.  Defaults to S<'-x -S %0 %*'>.
151
152=item * C<otherargs>
153
154Arguments to invoke perl with in generated batch file except when
155run from Windows NT (ie. when run from DOS, Windows 3.1, or Windows 95).
156Defaults to S<'-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9'>.
157
158=item * C<stripsuffix>
159
160Strip a suffix string from file name before appending a ".bat"
161suffix.  The suffix is not case-sensitive.  It can be a regex or a string and a trailing
162C<$> is always assumed).  Defaults to C<qr/\.plx?/>.
163
164=item * C<usewarnings>
165
166With the C<usewarnings>
167option, C<" -w"> is added after the value of C<$Config{startperl}>.
168If a line matching C</^#!.*perl/> already exists in the script,
169then it is not changed and the B<-w> option is ignored.
170
171=item * C<update>
172
173If the script appears to have already been processed by B<pl2bat>,
174then the script is skipped and not processed unless C<update> was
175specified.  If C<update> is specified, the existing preamble is replaced.
176
177=back
178
179=head1 ACKNOWLEDGEMENTS
180
181This 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.
182
183=head1 AUTHOR
184
185Leon Timmermans <leont@cpan.org>
186
187=head1 COPYRIGHT AND LICENSE
188
189This software is copyright (c) 2015 by Leon Timmermans.
190
191This is free software; you can redistribute it and/or modify it under
192the same terms as the Perl 5 programming language system itself.
193
194=cut
195