script portability problem

D

develop

Hi,

At the beginning of each perl cgi script is the #! line. I'm assuming that
this is an http directive that executes perl and indicates thedirectory in
which perl is found.

I'm developing scripts for a client. The problem is that he is using a
different web host than I am, and perl is found in different directories,
so I need to use different #! directives at the beginning of the scripts.

I don't want to have to change the scripts when I move them from my host
to the client's host. Is there a '#if' '#else' type of directive pair, and
if there is, what is the syntax and what test could I use to have the
script know whether it's running on my host or the client's host?
(Possibly the environment, $ENV{'???'}.)

Is there another way to accomplish this?

Thanks,
Dan
 
C

Chris

Hi,

At the beginning of each perl cgi script is the #! line. I'm assuming that
this is an http directive that executes perl and indicates thedirectory in
which perl is found.

Negative, Space Ranger... It's called a she-bang path for Unix scripts.
It tells the command shell what executable to use to execute the
script. Has nothing to do with HTTP.
I'm developing scripts for a client. The problem is that he is using a
different web host than I am, and perl is found in different directories,
so I need to use different #! directives at the beginning of the scripts.

I don't want to have to change the scripts when I move them from my host
to the client's host. Is there a '#if' '#else' type of directive pair, and
if there is, what is the syntax and what test could I use to have the
script know whether it's running on my host or the client's host?
(Possibly the environment, $ENV{'???'}.)

Is there another way to accomplish this?

Create a soft link on your development machine that matches the she-bang
you will use on your target machine. For example, if Perl is found in
/usr/local/bin on the target machine but in /usr/bin on your development
machine, place a softlink on your development machine to match it. Then
use that she-bang line in your scripts:

$ ln -sn /usr/bin/perl /usr/local/bin/perl

In file:

#!/usr/local/bin/perl
$|++;

use strict;
use warnings qw( all );
#...etc...

__END__

-ceo
 
C

Chris Mattern

Chris said:
Negative, Space Ranger... It's called a she-bang path for Unix scripts.
It tells the command shell what executable to use to execute the
script. Has nothing to do with HTTP.
Incorrect, my prince. She-bang talks direct to the kernel when the kernel
attempts to load the file for execution. This means, for example, that
she-bang scripts work right when you exec() them, which doesn't involve
the shell at all (as the exec(3) man page explains, as a matter of fact).

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
C

ChrisO

Chris said:
Chris wrote:



Incorrect, my prince. She-bang talks direct to the kernel when the kernel
attempts to load the file for execution. This means, for example, that
she-bang scripts work right when you exec() them, which doesn't involve
the shell at all (as the exec(3) man page explains, as a matter of fact).

I prefer, "Negative, Space Ranger..." :) Thanks for the correction.

-ceo
 
A

Andres Monroy-Hernandez

What about creating a script that changes the shebang line? You would
run this script once you move the scripts to the customer's server.
I've had experienced the same issue and that's what I did.

The script could be something like this:

------ shebang.pl--------------
use File::Find;
my ($dir) = @ARGV;
print "Replacing for files under '$dir'.\n";
find(\&callback, $dir);
exit;

sub callback {
my $namepath = $File::Find::name;
my $name = $_;

# replace shebang only in .pl files
return unless ($name =~ /\.pl$/); {
print "Couldn't open '$name' for reading purposes\n" unless (-w
$name);
replace($name);
}

sub replace{
my ($fn) = @_;
open(F, "+< $fn") or die "can't read $fn: $!";
my $out = '';
$out .= "#!/customer/she/bang/perl\n";
while (<F>) {
next if /^\s*#\/yourshebang$/;
$out .= $_;
}
seek(F, 0, 0) or die "can't seek to start of $fn:
$!";
print F $out or die "can't print to $fn: $!";
truncate(F, tell(F)) or die "can't truncate $fn: $!";
close(F) or die "can't close $fn: $!";
}
-----------------------------------------

Then you will run it like this:
perl shebang.pl /the/dir/where/scripts/are/


Regards,
 
L

Larry Felton Johnson

The best place to start thinking about your options here is the
man page "perlrun". It has a good discussion of approaches to
running perl in different environments. I may be just stating the
obvious, but perlrun is a good starting point.

Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top