Perl - File Open
Files are opened using the open and sysopen function. Nothing fancy here at all. Either function may be passed up to 4 arguments, the first is always the file handle discussed earlier, then our file name also known as a URL or filepath, flags, and finally any permissions to be granted to this file.When opening files as a programmer, there will generally be one of three goals in mind, file creation, appending files, or trunicating files.
- Create: Checks to see if the file exists, if not, perl creates a new file.
- Append: Sets the pointer to the end of the file, all output following will be added onto the tail end of the file.
- Truncate: Overwrites your existing file with a new one, this means all data in the old file will be lost.
PERL - Open a File
The following example will open a previously saved HTML document.PERL Code:
#!/usr/bin/perl print "content-type: text/html \n\n"; #The header $FH = "filehandle"; $FilePath = "myhtml.html"; open(FH, $FilePath, permissions); or sysopen(FH, $FileName, permission);
PERL - File Permissions
File permissions are crucial to file security and function. For instance, in order to function, a PERL file (.pl) must have executable file permissions in order to function on your web server. Also, you may not want all of your HTML files to be set to allow others to write to them or over them. Here's a listing of what to pass to the open function when working with file handles.Shorthand Flags:
Entities | Definition |
< or r | Read Only Access |
> or w | Creates, Writes, and Truncates |
>> or a | Writes, Appends, and Creates |
+< or r+ | Reads and Writes |
+> or w+ | Reads, Writes, Creates, and Truncates |
+>> or a+ | Reads, Writes, Appends, and Creates |
O_ Flags:
Value | Definition |
O_RDWR | Read and Write |
O_RDONLY | Read Only |
O_WRONLY | Write Only |
O_CREAT | Create the file |
O_APPEND | Append the file |
O_TRUNC | Truncate the file |
O_EXCL | Stops if file already exists |
O_NONBLOCK | Non-Blocking usability |
PERL Code:
#!/usr/bin/perl print "content-type: text/html \n\n"; #The header use Fcntl; #The Module sysopen (HTML, '/home/html/myhtml.html', O_RDWR|O_EXCL|O_CREAT, 0755); sysopen (HTML, >myhtml.html');
0 comments:
Post a Comment