Monday, May 24, 2010

PERL: how do i create a new file to output data to??

print("filename :: ");


chomp ($name = %26lt;STDIN%26gt;);





$newfile = "$name.txt";


open(FILE, "$name.txt") or die "Couldnt open file.";





i want $name.txt to be created so i can write information to it from this perl program. Can anyone shed any information??


Thanks.

PERL: how do i create a new file to output data to??
In Perl, open by default opens for READING.





You can explicitly say whether you are reading or writing, by placing a greater-than or less-than character in front of the filename:





open FILE, "%26lt;${name}.txt" (open for read)


open FILE, "%26gt;${name}.txt" (open for write)


open FILE, "%26gt;%26gt;${name}.txt" (open for append)
Reply:Jay here is your link





http://www.google.co.uk/search?hl=en%26amp;q=I...
Reply:To create (over-writing if it exists) the file, use:





open (FILE, "%26gt;", $filename) || die "Could not open file $filename";





To append to the file, use:





open (FILE, "%26gt;%26gt;", $filename) || die "Could not open file $filename";





You should get in the habit of using the three argument form of open. If you use the two argument form and allow users to provide their own filenames, you are potentially opening a security hole.





For example:





open (FILE, $filename);





Imagine if the user provides a filename with a '%26gt;' as the first character. Depending on the unix permissions, they can potentially truncate arbitary files on your system.


No comments:

Post a Comment