CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Reading and writing files



Jan 20, 2000, 7:15 PM

Post #1 of 5 (636 views)
Reading and writing files Can't Post

I have a couple of questions about files.

Firstly, I open an input file and read it OK but the file has to be in the cgi-bin directory for it to be found. I've put the file in other directories and coded the full path (http://...../.../filename.dat) but it is never found. Must the file be in the cgi-bin directory?

Secondly, I cannot write to an output file. I use the statement open(OUTFILE,">newfilename.dat") and I get a permission denied error. I can read and write to the cgi-bin normally (after all, I load my cgi files there). So what is going wrong? Is the cgi-bin treated like the "world" with permissions?

I need to be able to read in a flat file and write out a flat file in the same program.


Administrator / Moderator

Jan 20, 2000, 7:23 PM

Post #2 of 5 (636 views)
Re: Reading and writing files [In reply to] Can't Post

In order for a Perl program to open a file, it needs to be given the server path, not the http path to the file.

For example, your Perl program is in your cgi-bin, and the file you want to open is in a subdirectory outside of your cgi-bin (such as, you'll need to use something similar to:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$filetoopen = "/home/yourdomain/www/somewhere/file.dat";

What permission settings do you have on newfilename.dat? Some servers require file permission settings as high as 777 to be able to write to a file. If in doubt, ask your server administrator.


Jan 20, 2000, 7:27 PM

Post #3 of 5 (636 views)
Re: Reading and writing files [In reply to] Can't Post

You need to use the full path to file from the system not the browser.

$conf = '/path/to/file';

$conf = "http://path/to/file";

Then try it....

BUT if you want to use something that is relavant to a web browser then try:


I have done something simalar to this.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


my $conf ='/home/http/pub/data.txt';

open(CONF, $conf) &#0124; &#0124; die "Failed to open config file $conf : $!";
while(<CONF> ) {
Do something here.....


Let us know..

Brian Hayes


Jan 20, 2000, 7:41 PM

Post #4 of 5 (636 views)
Re: Reading and writing files [In reply to] Can't Post


#!/usr/bin/perl -w


sub get_data{

require LWP::UserAgent;
require LWP::Simple;
require HTTP::Request;
require HTTP::Response;
require HTTP::Status;

## ua is User Agent

local($ua_proc_read_dat) = new LWP::UserAgent;
$ua_proc_read_dat->agent ("Testing with LWP useragent");
$ua_proc_read_dat->from "Your\");
$ua_proc_read_dat->timeout (90);

local($parse_request) = new HTTP::Request("POST", "http://www.yoursite/path/data.txt");
local($parse_response) = $ua_proc_read_dat->request($parse_request);
local($parse_buffer) = $parse_response->content;

if (($parse_responce->code() == 200){

## Here you would do something with $parse_buffer
## It will contain your page.
## 200 is O.K.. from server...


This is something that I tested with to see how it was done without using correct coding..I'm still learning as well. So I would ask someone to clean this up before use.


Brian Hayes


Jan 21, 2000, 4:39 PM

Post #5 of 5 (636 views)
Re: Reading and writing files [In reply to] Can't Post

Once again I receive great help from the forum. One day I might be able to help someone - who knows?

Keep up the good work.

Ray :-)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives