FileDirectory manipulation - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

FileDirectory manipulation

Description:

File/Directory manipulation. Opening a File. To read from a file, ... There Is More Than One Way To Do It Read the file into an array, and join the elements: ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 13
Provided by: PaulL155
Category:

less

Transcript and Presenter's Notes

Title: FileDirectory manipulation


1
File/Directory manipulation
2
Opening a File
  • To read from a file, must use a file handle.
  • by convention, all caps
  • open a file (for reading)
  • open FILE, myfile.txt
  • open file if exists, else return false
  • open file for writing
  • open OUTFILE, gtoutput.txt
  • clobber file if exists, else create new
  • open file for appending
  • open APPFILE, gtgtappend.txt
  • open file, positioned at end if exists, else
    create new

3
Reading from a file
  • open FILE, myfile.txt
  • one_line ltFILEgt
  • _at_all_lines ltFILEgt
  • _at_all_lines get all remaining lines from
    myfile.txt
  • puts each line of file into one member of array
  • remember chomp!
  • Rewind a file by seeking to the beginning
  • seek (FILE, 0, 0)
  • see Camel for explanation

4
printing to a file
  • open OUTFILE, gtoutput.txt
  • print OUTFILE Hello World!\n
  • this can be tedious if all outputs are to same
    output file.
  • select OUTFILE
  • make OUTFILE the default file handle for all
    print statements.

5
Reading a File at Once
  • If you have the need to read the entire contents
    of a file into one large scalar,
  • There Is More Than One Way To Do It
  • Read the file into an array, and join the
    elements
  • _at_lines ltFILEgt
  • oneline join (, _at_lines)
  • Or, set the pre-defined variable / to undefined.
    This variable controls Perls notion of what a
    line is.
  • By default, / is equal to \n, meaning that a
    line is terminated by a newline character. If
    / is undefined, a line is terminated by the
    end-of-file marker
  • undef /
  • oneline ltFILEgt
  • This method is known as slurping a file.

6
Close your files!
  • open FILE, myfile.txt
  • _at_all_lines ltFILEgt
  • close FILE
  • opening another file to the same filehandle will
    implicitly close the first one.
  • dont rely on this. Its not Good Programming
    Practice.

7
File Test Operators
  • Test to see if something is true about a file
  • full list on page 98 of Camel
  • exists, readable, zero size, directory, link,
    text file, etc
  • if (-e myfile.txt)
  • print file exists, now opening\n
  • open FILE, myfile.txt
  • can operate on filename or already existing
    filehandle

8
Directory manipulation
  • directories can be opened, read, created,
    deleted, much like files.
  • take care when doing these operations youre
    affecting your directory structure
  • many of the functions success will depend on
    what permissions you have on the specified
    directories.

9
open, read, close
  • opendir DIR, public_html
  • nextfile readdir DIR
  • _at_remaining_files readdir DIR
  • closedir DIR

10
Rewind
  • opendir DIR, .
  • firstfile readdir DIR
  • secondfile readdir DIR
  • rewinddir DIR
  • _at_allfiles readdir DIR

11
Change, Create, and Delete
  • chdir ? change working directory.
  • mkdir ? create directory (like unix call)
  • rmdir ? remove directory (like unix call)
  • works if and only if directory is empty
  • chdir public_html
  • mkdir images
  • rmdir temp

12
Dont do useless work
  • Theres no reason to change to a directory to
    open a file in that directory
  • You can specify the path of the file in the file
    open statement
  • open FILE, public_html/index.html
  • This opens the file, without going through the
    bother of changing your working directory twice.
Write a Comment
User Comments (0)
About PowerShow.com