OPEN and WRITE a Data File in Perl
The single right arrow means overwrite the contents of the file places.txt
open(DA, ">places.txt");
print DA "I see London\n";
close(DA);
-The word DA is called a Filehandle. Always use ALL CAPS for filehandles. The DA could be pretty much anything you like except STDIN and STDOUT, which are reserved for other things.
-Use full or relative path if places.txt is in different directory.
The two right arrows mean add to the contents of the file places.txt
open(DA, ">>places.txt");
print DA "I see London\n";
close(DA);
NOTES:
-For our Copy and Paste Code, I created a directory named citydata. This directory will likely have to be chmoded 777 to be written to.
-The two vertical bars mean "or". In this instance, open file "or" print "Can't Open blablabla".
-the "\n" adds a UNIX line return to the end of our sentence.
Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
open(DA, ">citydata/places.txt") || print "Can't Open File. Try chmod 777 citydata directory<br><br>";
print DA "I see London\n";
close(DA);
open(DA, "<citydata/places.txt");
@da=<DA>;
close(DA);
print @da;
exit;
Open and Write to a Data File in Perl - See in Action (new window)
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group