Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
File input and output is not much of a stretch from normal I/O. Basically you have to use the open command to open a filestream and then read to and write from it. Then once you're done with it you use close to close the file. The syntax for opening a file is:

open(FILEHANDLE,"filename");

This opens a new filehandle with the name of FILEHANDLE, and associates it with filename which is the location of the file on your disk. This works for reading a file. If you want to write to it you need to put a > in front of the filename as seen below:

open(FILEHANDLE,">filename");

To append to a file you use >> in front of the filename as you can see here:

open(FILEHANDLE,">>filename");

Now for some quick examples:
open(FILE, "data.txt"); #opens data.txt in read-mode while(<FILE>){ #reads line by line from FILE which i +s the filehandle for data.txt chomp; print "Saw $_ in data.txt\n"; #shows you what we have read } close FILE; #close the file.

To print stuff to a file you merely through a line into your program like:

print FILEHANDLE "your text here\n";

FILEHANDLE needs to be a FILEHANDLE you currently have open. Notice there is no comma between the FILEHANDLE and the stuff your printing out. That is the way it needs to be. Now for a more extended example:
open FILE, ">keylogger.dat"; #opens file to be written to while(<>){ #while we're getting input from the keyb +oard print FILE $_; #write it to our file } close FILE; #then close our file.


Another thing you might want to look into are the filetest operators these allow you to do things like test whether a file exists, is readable, is writable among other things. complete information look at the documentation

In reply to File Input and Output by root

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (8)
    As of 2015-08-01 01:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (285 votes), past polls