App-ChangeShebang

 view release on metacpan or  search on metacpan

lib/App/ChangeShebang.pm  view on Meta::CPAN

    my $content = do {
        open my $fh, "<:raw", $file or die "open $file: $!\n";
        local $/; <$fh>;
    };

    $content =~ s/$remove//;

    my $mode = (stat $file)[2];

    my ($tmp_fh, $tmp_name) = File::Temp::tempfile UNLINK => 0, DIR => File::Basename::dirname($file);
    chmod $mode, $tmp_name;
    print {$tmp_fh} <<'...';
#!/bin/sh
exec "$(dirname "$0")"/perl -x "$0" "$@"
#!perl
...
    print {$tmp_fh} $content;
    close $tmp_fh;
    rename $tmp_name, $file or die "rename $tmp_name, $file: $!\n";
}

t/01_basic.t  view on Meta::CPAN

#!/bin/sh
exec "$(dirname "$0")"/perl -x "$0" "$@"
#!perl
...
    is slurp("$tempdir/hoge.rb"), "#!/usr/bin/ruby\n";
};

subtest permission => sub {
    my $tempdir = tempdir;
    spew "$tempdir/hoge$_.pl", "#!/path/to/perl\n" for 1..3;
    chmod 0755, "$tempdir/hoge1.pl";
    chmod 0555, "$tempdir/hoge2.pl";
    chmod 0500, "$tempdir/hoge3.pl";

    App::ChangeShebang->new
        ->parse_options("-f", "-q", map "$tempdir/hoge$_.pl", 1..3)
        ->run;
    is slurp("$tempdir/hoge$_.pl"), <<'...' for 1..3;
#!/bin/sh
exec "$(dirname "$0")"/perl -x "$0" "$@"
#!perl
...
    is( (stat "$tempdir/hoge1.pl")[2] & 07777, 0755 );



( run in 0.269 second using v1.01-cache-2.11-cpan-8d75d55dd25 )