Hello,

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:

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; }
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.

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


In reply to Can I include O_CREAT, O_EXCEL and O_WRONLY in my script without always requiring Fcntl? by rzward

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.