I got this perl example that's suppose to demonstrate sysopen and printf, except so far it only demonstrates die.
#! /usr/bin/perl $filepath = 'myhtml.html'; sysopen (HTML, $filepath, O_RDWR|O_EXCL|O_CREAT, 0755) or die "$filepath cannot be opened."; printf HTML "<html>\n"; but when I execute the code it just dies.
myhtml.html cannot be opened. at file_handle.pl line 7. myhtml.html does not exist, but it should have been created by the O_CREAT flag. shouldn't it?
EDIT
I have edited the code to include the suggestions about use strict and $!. Below is the new code and its result.
#! /usr/bin/perl use strict; $filepath = "myhtml.html"; sysopen (HTML, '$filepath', O_RDWR|O_EXCL|O_CREAT, 0755) or die "$filepath cannot be opened. $!"; printf HTML "<html>\n"; output, due to the use strict, gave us a whole bunch of errors:
Global symbol "$filepath" requires explicit package name at file_handle.pl line 3. Global symbol "$filepath" requires explicit package name at file_handle.pl line 5. Bareword "O_RDWR" not allowed while "strict subs" in use at file_handle.pl line 5. Bareword "O_EXCL" not allowed while "strict subs" in use at file_handle.pl line 5. Bareword "O_CREAT" not allowed while "strict subs" in use at file_handle.pl line 5. Execution of file_handle.pl aborted due to compilation errors. EDIT 2
Based on everyone's suggestion and help, here is the final working code:
#! /usr/bin/perl use strict; use Fcntl; my $filepath = "myhtml.html"; sysopen (HTML, $filepath, O_RDWR|O_EXCL|O_CREAT, 0755) or die "$filepath cannot be opened. $!"; printf HTML "<html>\n"; .... 83 Answers
O_RWDR, O_EXCL, and O_CREAT are all constants defined in the Fcntl module. Put the line
use Fcntl; near the top of your script.
1Lots of issues here:
- Always put
use strict;at the top of your program. That would provide a clue. - The reason the
sysopenfailed is in the$!variable. You should generally include it in anydiemessage. - As the
sysopenentry inman perlfuncexplains, the variousO_*constants are exported by theFcntlmodule. You need tousethat module if you want those constants defined. As it is, you're character-wise or-ing together the strings"O_RDWR","O_EXCL", and"O_CREAT", resulting in another string thatsysopendoesn't know what to do with.use strictwould prevent this from happening.
myhtml.html file probably already exists. It is probably because a previous execution of the script created it. The O_EXCL flag will cause sysopen to fail if the file exists. Relevant quote from sysopen documentation:
2In many systems the
O_EXCLflag is available for opening files in exclusive mode. This is not locking: exclusiveness means here that if the file already exists,sysopen()fails.