|Contents||Perl File open operation|
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.
PERL - Open a File
The following example will open a previously saved HTML document.
#!/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);
Files with special characters or unusual names are best opened by first declaring the URL as a variable. This method removes any confusion that might occur as PERL tries to interpret the code. Tildas in filenames however require a brief character substitution step before they can be placed into your open statements.
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.