Shawn Milochik wrote:
> Hello all.

Hello,

> I've been spending too much money lately on Amazon buying 
> Perl books. Recently, I've been playing with scripts downloaded from 
> the Web site of a book I'm going through. For some reason, some of  the
> .pl files have a shebang, and others do not.
> 
> 1. What's the difference, other than whether you have to run them by 
> calling Perl or not?
> 
> 2. I cobbled together a little Perl script to add the shebang where  it
> doesn't exist. I'd appreciate any feedback, especially any  suggestions
> if I'm doing anything weird, or it could be done better  or more
> "Perlishly."
> 
> ------------------------------------------------------------------------
> begin script
> ------------------------------------------------------------------------
> #!/usr/bin/perl
> 
> #add_shebang.pl
> #possible use: ls | ./add_shebang.pl

That is one way to do it.  Another way is "./add_shebang.pl *.pl" and have the
file names available in the @ARGV array, and you could even use the in-place
edit variable ($^I) to modify the files "in-place".  Or you could just use
glob() or opendir()/readdir() to get the file names.  The main problem with
using standard input as in your example is that the shell may modify the file
names.


> use strict;
> use warnings;
> use File::Copy "cp";
> 
> #For each file sent as an argument, check to see if
> #the first line is a shebang. If not, add one.
> foreach my $file (<>){

It is usually more advisable to use a while loop when reading from a filehandle.

>     chomp($file);
>     print "File name $file\n";
>     unless ($file =~ /\.pl$/){
>         print "   Skipping $file -- not a .pl file.\n";
>         next;
>     }
> 
>     my $shebang = "#!/usr/bin/perl\n\n";

You don't really need to set this variable every loop iteration, just once at
the beginning of the program.

>     open(HANDLE, $file);

You should *ALWAYS* verify that the file opened correctly.


>     my $lineNum = 0;

Why not just use perl's built-in $. variable?

>     while (<HANDLE>){
>         $lineNum++;
>         if ($lineNum == 1){
>             if ($_ =~ /^#!/){
>                 print "   Shebang exists.\n";
>                 last;
>             }else{
>                 print "   No shebang; creating.\n";   
>                 open(TEMP, ">$file.tmp");

You should *ALWAYS* verify that the file opened correctly.


>                 print TEMP $shebang;
>             }#check for shebang
>         }else{
>             print TEMP $_;   
>         }#if $lineNum == 1
>     }# while (<HANDLE>)
>     close(TEMP);
>     close(HANDLE);
>     unlink("$file.tmp") if cp("$file.tmp", $file);
> }#foreach $file
> 
> ------------------------------------------------------------------------
> end script
> ------------------------------------------------------------------------

The same thing as a one-liner:

perl -i -pe'$.==1&&!/^#!/&&print"#!/usr/bin/perl\n";close ARGV if eof' *.pl


:-)

John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to