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

-- 
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