Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Opening files

by Anonymous Monk
on Jul 27, 2000 at 21:39 UTC ( [id://24712]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Opening files
by plaid (Chaplain) on Jul 27, 2000 at 21:46 UTC
    You have to include the path to the file, either relative to where you are, or an absolute path, unless the file is in the same current directory as you are. Your current directory is wherever you were when you ran the script, if you're running from the command line. If you're running from a webserver, your current directory is the directory where the script is. You can affect the directory with the chdir function.

    For example, if you are in the directory /home/you and you want to open file.txt in the directory /home/me, you could open the file in 3 different ways:

    open(FILE, "/home/me/file.txt") or die "$!"; #absolute path open(FILE, "../me/file.txt") or die "$!"; #relative path chdir("../me") or die "$!"; open(FILE, "file.txt") or die "$!"; #current dir
Re: Opening files
by maverick (Curate) on Jul 27, 2000 at 21:49 UTC
    # open a file for reading in the current directory open(FILEHANDLE,"< some_file"); # open a file for reading using the full path (always a good idea) open(FILEHANDLE,"< /home/my_user/some_file"); # open a file for writing open(FILEHANDLE,"> /home/my_user/some_file"); # append to the end of a file open(FILEHANDLE,">> /home/my_user/some_file"); # and it's always a good idea to check for failures ($! contains the e +rror message) open(FILEHANDLE,"< /home/my_user/some_file") || die "Can't read file: +$!"; # be polite and close the file when you're done :) close(FILEHANDLE);
    All this stuff is explained in greater detail in the docs that come with perl, or any perl book.
    # by the way # read a line from a file $line = <FILEHANDLE>; # write something to a file print FILEHANDLE "some line of text\n";

    /\/\averick

Re: Opening files
by btrott (Parson) on Jul 27, 2000 at 21:50 UTC
    Read the open documentation.

    The usage of paths, and the paths that you use, differ slightly from OS to OS. MacPerl, for example, has rather interesting logic to determine whether the argument you've provided to open is a relative path or absolute path, etc. If you're just on Unix, though, it should be fairly straightforward.

RE: Opening files
by eLore (Hermit) on Jul 28, 2000 at 02:17 UTC
    Here's a snippet to open a file, and print the contents:
    $input_file="/home/foo/foo_file.txt"; open (INFILE, $input_file) || die "Could not open $input_file\n"; while(<INFILE>){ print $_; } close INFILE;
    Note that there are several operators, which determine how the file is treated. If you wish to just read, no special operator is needed. If you wish to append to a file, specify the name as $somefile=">>/home/foo/foo_file.txt". Research the "open" function for additional modifiers and usage.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://24712]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-16 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found