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

I can currently open an create a file of source code from a web site using the following code:

use LWP::Simple; $source = get("www.url.com");

open(FILE, ">file.txt");
print FILE $source;
close(FILE);
The problem is that I want to create the files in a sub folder rather than having it print to the parent directory. For instance, if my program is called. program.pl and stored in the "mainfolder" folder, in the above example, it creates a file called file.txt in "mainfolder". However, what I want the program to do is to create a new file in a subfolder of mainfolder called subfolder. So even though the program "program.pl" is housed in "mainfolder", when I run "program.pl", I want it to create "file.txt" in "subfolder" of "mainfolder". How do I do that? Thanks, Jeff

Content restored by GrandFather

  • Comment on Simple/Quick question about opening creating a file using perl

Replies are listed 'Best First'.
Re: Simple/Quick question about opening creating a file using perl
by kyle (Abbot) on Dec 29, 2008 at 19:51 UTC

    Start by making sure the subfolder is there (use mkdir).

    my $subdir = 'subfolder'; if ( ! mkdir( $subdir ) && ! -d $subdir ) { die "Can't create '$subdir': $!"; }

    Then just open the file you want to write into.

    my $outfile = "$subdir/file.txt"; open my $out_fh, '>', $outfile or die "Can't write '$outfile': $!"; print $out_fh $source; close $out_fh or die "close() failed: $!";

    Note that I'm using a lexical filehandle instead of a global one, and I'm using the three-argument form of open. Also, the directory/file that we make is relative to current directory when the program is executed, not to where the program actually lives. For the latter, have a look at $0.

Re: Simple/Quick question about opening creating a file using perl
by jeffa (Bishop) on Dec 29, 2008 at 19:48 UTC

    How about using an absolute path?

    open( FILE, '/full/path/to/file.txt' ) or die $!;

    Also, you could leverage LWP::Simple better by calling:

    perl -MLWP::Simple -e 'getprint "http://www.url.com"' > /full/path/to/ +file.html
    The return is a file that contains HTML and not just plain text, yes? Then name it .html and not .txt

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Simple/Quick question about opening creating a file using perl
by linuxer (Curate) on Dec 29, 2008 at 19:55 UTC

    Another idea is:

    create the directory with mkdir if it not exists.

    then use LWP::Simple's getstore routine to store the content directly in your desired file.

    That saves you the work of opening the file, printing to it and closing it again.

      Thanks for the help! I got it to work!

        I wonder, that your initial question is now untitled and empty.

        Did you remove that stuff? If so, please recover it, so it becomes clear what the answers are related to. Thanks.

        Just to expand on linuxer's reply: Deleting your original post is the on-line equivalent of offering to hold a chair for someone to sit in and then yanking it out from under them when they do.

        Again, please restore what you have deleted.

Re: Simple/Quick question about opening creating a file using perl
by linuxer (Curate) on Dec 29, 2008 at 19:49 UTC

    Create the directory "subfolder" by using mkdir

    Check mkdir() for success; if successful, prepend "subfolder" to the filepath to be written; e.g. using File::Spec's catfile

Re: Simple/Quick question about opening creating a file using perl
by Lawliet (Curate) on Dec 29, 2008 at 19:51 UTC

    If you need to actually make the directory first then look into mkdir.

    And you didn't even know bears could type.