rzward has asked for the wisdom of the Perl Monks concerning the following question:
I have a monolithic Perl CGI script that is distributed to a wide variety of web servers. The script has a new feature that requires the use of the function sysopen. The flags I want to pass to sysopen (O_CREAT, O_EXCL and O_WRONLY) in turn require the Fcntl module.
I would like to not require that web servers have the Fcntl module installed to use my script unless the new feature is used.
Here is a small program that uses sysopen and Fcntl:
use strict; print &openit; sub openit1 { use Fcntl qw(O_CREAT O_EXCL O_WRONLY); my $fp = "c:\\temp\\test.csv.lck.tmp"; die "Cannot open $fp" if !sysopen (my $fh,$fp,O_WRONLY|O_CREAT|O_EXC +L); close $fh; unlink $fp; return $fp; }
This program works fine when Fcntl is installed. When Fcntl is not installed, the program fails to compile. So, I have tried the following:
This program does not compile because of use strict; and the barewords O_CREAT, O_EXCL and O_WRONLY. When I remove use strict;, the script compiles but sysopen does not work properly. I don't really understand why but I'm thinking this is because O_CREAT, O_EXCL and O_WRONLY are not set correctly when use Fcntl is evaluated inside a string.use strict; print &openit; sub openit { eval "use Fcntl qw(O_CREAT O_EXCL O_WRONLY);"; my $fp = "c:\\temp\\test.csv.lck.tmp"; die "Cannot open $fp" if !sysopen (my $fh,$fp,O_WRONLY|O_CREAT|O_EXC +L); close $fh; unlink $fp; return $fp; }
I would like those who use the feature to install Fcntl but those who do not to not have to install Fcntl. Is there a way for me to use sysopen in my script, requiring Fcntl only when the code is about to be run?
Thank you in advance for your help.
Richard
|
|---|