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

Fellow Monasterians,

I feel like this is my first day writing Perl. Doing something I've done a million times, writing a text file, but getting a "No such file or directory" error.

my $path ="http://www.domain.com/WysiwygPro/content.txt"; open (DATA, ">$path") or die "can't write content.txt to $path, error: + $!"; print DATA $content; close (DATA);

Returns error:

can't write content.txt to http://www.domain.com/WysiwygPro/content.tx +t, error: No such file or directory at editortest.pl line 44

The directory is indeed there and chmodded to 755. However, when I change the path to relative:

my $path ="../WysiwygPro/content.txt";

..all is fine.

Why would I be getting the error with the absolute? What incredibly elementary thing am I missing? TIA

UPDATE: Wow! I get it. Unix paths != HTTP paths, except in instances like "../foo". Thank you good monks.


—Brad
"Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton

Replies are listed 'Best First'.
Re: Write file using full path returns error
by Tanktalus (Canon) on Jan 20, 2005 at 20:59 UTC

    The open routine has no concept of transfer protocols such as http, ftp, etc. Only the local filesystem (or what masquerades as the local file system if virtual file systems are in use, such as nfs, afs, dfs, smbfs, etc.). You need to open a local path - starting with / for a full path name. What open is trying to write to is "$CWD/http:/www.domain.com/WysiwygPro/content.txt" where $CWD is the current working directory.

    I recommend using FindBin for creating an absolute (local) path.

Re: Write file using full path returns error
by Anonymous Monk on Jan 20, 2005 at 21:55 UTC
    First of all, you didn't say so, but it sounds like you're writing a CGI program, right?

    Why would I be getting the error with the absolute? What incredibly elementary thing am I missing?

    You sound like you're confusing URLs with UNIX file paths. An "absolute" URL identifies the service and the host, such as "http://www.perlmonks.com". A relative url references another web page, relative to the current web page.

    An absolute UNIX filename specifies the exact location of the file: the list of subdirectories you must go through to find the file, starting from the root directory. Absolute unix pathnames start with a "/", such as "/etc/passwd".

    When writing CGI scripts, you often deal with both: you write to UNIX files, and you print out links to web pages for browers to follow.

    The reason the first example works is because UNIX pathnames, as well as URLs, both share the same way of writing "one level up": by writing a ".." in front of the path to follow.

    The reasons the second doesn't is because you're not asking for an absolute UNIX pathname (which is what perl deals with), you're asking for an odd looking relative pathname. Specificly, you've told Perl something like: "from the current directory (because there's no '/' at the start", go to directory "http:", then to the directory "www.domain.com", then to the directory "WysiwygPro", and then create the file "content.txt".

    The directories "http:", and "www.domain.com" probably don't exist. If you really want to use the full pathname of the UNIX directory your web server is using, you'll have to look it up: but I don't know why you'ld want it.

    Both relative links and relative pathnames are often prefered in both cgi scripts and in HTML, because you can move the cgi script and files without changing all the absolute references.

    Hope this helps clear up some of the confusion.

    --
    AC

Re: Write file using full path returns error
by EverLast (Scribe) on Jan 20, 2005 at 21:17 UTC
    From what I see, it seems you're trying to read data from a URL, and to store this locally.

    LWP::Simple will be your friend here. See the getstore function.

    ---Lars