| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | # A little test to see if users' systems can handle the more |
|---|
| 4 | # portable '#!/usr/bin/env perl' shebang line rather than the |
|---|
| 5 | # current '#!/usr/bin/perl'. |
|---|
| 6 | |
|---|
| 7 | use warnings; |
|---|
| 8 | use strict; |
|---|
| 9 | |
|---|
| 10 | use IO::File; |
|---|
| 11 | use Getopt::Long; |
|---|
| 12 | |
|---|
| 13 | my $progname = "usr_bin_env_test"; |
|---|
| 14 | my $version = "0.1"; |
|---|
| 15 | |
|---|
| 16 | printf "%s v%s\n",$progname,$version; |
|---|
| 17 | |
|---|
| 18 | my $opt; |
|---|
| 19 | GetOptions( |
|---|
| 20 | 'version' => \$opt->{version}, |
|---|
| 21 | ); |
|---|
| 22 | |
|---|
| 23 | exit if ($opt->{version}); |
|---|
| 24 | |
|---|
| 25 | print "\nThis is a test component, designed to see whether your\n". |
|---|
| 26 | "system can happily migrate from '#!/usr/bin/perl' to\n". |
|---|
| 27 | "'#!/usr/bin/env perl'.\n\n"; |
|---|
| 28 | |
|---|
| 29 | my $test_file = 'test_file.pl'; |
|---|
| 30 | |
|---|
| 31 | print "Creating test file...\n"; |
|---|
| 32 | |
|---|
| 33 | my $fh = new IO::File(">$test_file") || die "can't open $test_file for writing: $!"; |
|---|
| 34 | |
|---|
| 35 | print $fh <<EOF |
|---|
| 36 | #!/usr/bin/env perl |
|---|
| 37 | |
|---|
| 38 | use strict; |
|---|
| 39 | use warnings; |
|---|
| 40 | |
|---|
| 41 | print "*** Output from test file: Success! ***"; |
|---|
| 42 | |
|---|
| 43 | EOF |
|---|
| 44 | ; |
|---|
| 45 | $fh->close; |
|---|
| 46 | |
|---|
| 47 | print "Setting test file as executable...\n"; |
|---|
| 48 | system "chmod a+x $test_file" || die "Couldn't set test file as executable: $!"; |
|---|
| 49 | |
|---|
| 50 | print "Running test file...\n\n"; |
|---|
| 51 | |
|---|
| 52 | local *TF; |
|---|
| 53 | unless (open(TF, './' . $test_file . ' 2>&1|')) |
|---|
| 54 | { |
|---|
| 55 | print "Couldn't open test file: $!\n"; |
|---|
| 56 | exit 1; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | my $msg = ''; |
|---|
| 60 | while (<TF>) |
|---|
| 61 | { |
|---|
| 62 | print ": $_"; |
|---|
| 63 | $msg .= $_; |
|---|
| 64 | } |
|---|
| 65 | close (TF); |
|---|
| 66 | |
|---|
| 67 | print "\n"; |
|---|
| 68 | |
|---|
| 69 | if ($@) |
|---|
| 70 | { |
|---|
| 71 | print "Some kind of error: $@\n"; |
|---|
| 72 | exit 2; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | if ($msg) |
|---|
| 76 | { |
|---|
| 77 | chomp $msg; |
|---|
| 78 | $msg =~ s/(.*) at .*\/(.*)/$1 at $2/g; |
|---|
| 79 | } |
|---|
| 80 | if ($?) |
|---|
| 81 | { |
|---|
| 82 | printf "Test file exited with %d, output: >>>$msg<<<\n", $? >> 8; |
|---|
| 83 | exit 3; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | print "It worked!\n"; |
|---|
| 87 | |
|---|
| 88 | print "Deleting test file...\n"; |
|---|
| 89 | unlink $test_file; |
|---|
| 90 | |
|---|
| 91 | print "Finished.\n"; |
|---|
| 92 | |
|---|
| 93 | exit 0; |
|---|