On 10/21/06, xavier mas <[EMAIL PROTECTED]> wrote:
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
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