Home > Archive > PERL Beginners > October 2006 > To shebang or not to, and script feedback.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
To shebang or not to, and script feedback.
|
|
| Shawn Milochik 2006-10-21, 7:56 am |
| Hello all. 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."
Thanks,
Shawn
------------------------------------------------------------------------
------------------------
begin script
------------------------------------------------------------------------
------------------------
#!/usr/bin/perl
#add_shebang.pl
#possible use: ls | ./add_shebang.pl
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 (<> ){
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";
open(HANDLE, $file);
my $lineNum = 0;
while (<HANDLE> ){
$lineNum++;
if ($lineNum == 1){
if ($_ =~ /^#!/){
print " Shebang exists.\n";
last;
}else{
print " No shebang; creating.\n";
open(TEMP, ">$file.tmp");
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
------------------------------------------------------------------------
------------------------
| |
| Xavier Mas 2006-10-21, 7:56 am |
| A Dissabte 21 Octubre 2006 10:31, Shawn Milochik va escriure:
> Hello all. 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."
>
> Thanks,
> Shawn
>
> ------------------------------------------------------------------------
> ------------------------
> begin script
> ------------------------------------------------------------------------
> ------------------------
> #!/usr/bin/perl
>
> #add_shebang.pl
> #possible use: ls | ./add_shebang.pl
> 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 (<> ){
> 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";
>
> open(HANDLE, $file);
>
> my $lineNum = 0;
> while (<HANDLE> ){
> $lineNum++;
> if ($lineNum == 1){
> if ($_ =~ /^#!/){
> print " Shebang exists.\n";
> last;
> }else{
> print " No shebang; creating.\n";
> open(TEMP, ">$file.tmp");
> 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
> ------------------------------------------------------------------------
> ------------------------
Shebang shows Perl the path to bin program as it can be execute. Is needed if
you run Linux, but not in Windows. Actually, Linux scripts are always headed
on tis first line by one of those to say where is the wanted executable.
Instead, Windows "knows" always where it its, so this line is not needed.
Hope this helps.
--
Xavier Mas
| |
| John W. Krahn 2006-10-21, 7:56 am |
| 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
| |
| Jen Spinney 2006-10-22, 9:56 pm |
| On 10/21/06, xavier mas <xaviermasr@ya.com> wrote:
>
> A Dissabte 21 Octubre 2006 10:31, Shawn Milochik va escriure:
>
> Shebang shows Perl the path to bin program as it can be execute. Is needed
> if
> you run Linux, but not in Windows. Actually, Linux scripts are always
> headed
> on tis first line by one of those to say where is the wanted executable.
> Instead, Windows "knows" always where it its, so this line is not needed.
>
> Hope this helps.
>
> --
> Xavier Mas
>
I'm constantly switching my perl scripts from different operating systems
(usually windows and solaris and sometimes linux), and I've found it easier
to just drop the shebang completely. When I run from the command line, I
just always specify 'perl myperlscript.pl' and I get no complaints. All the
solaris machines I run on have perl installed in a different place, but I
never seem to go wrong if I make sure my path environment variable points to
the most recent installation on each.
Don't know if that helps.
- Jen
|
|
|
|
|