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

Hi everyone, I am very new to Perl and having trouble doing the simplest things. I tried running the following lines and it gives me a blank file. Any ideas on what could be wrong? Thanks for your help.

#!/usr/bin/perl use strict; use warnings; use LWP::Simple; getstore('http://www.perlmeme.org', 'some_local_file'); exit 0;

Replies are listed 'Best First'.
Re: LWP::Simple Empty File
by Corion (Patriarch) on Mar 03, 2012 at 18:29 UTC

    Your code works for me (in the sense that it stores something that looks like that page into some_local_file).

    Have you looked at the return code of getstore? Do you have write permissions to some_local_file?

    Consider splitting up getstore into the two components, get (from LWP::Simple) and then writing the file yourself.

Re: LWP::Simple Empty File
by toolic (Bishop) on Mar 03, 2012 at 18:36 UTC
    The LWP::Simple docs indicate that you can look at the response code:
    my $rc = getstore('http://www.perlmeme.org', 'some_local_file'); print status_message($rc), "\n";
    This prints OK for me, and I get a non-empty oputput file.

      Thanks for the quick response. I added the status_message snippet and get "OK". This works fine on my Linux machine but is giving me an empty file on Windows 7 machine. I installed Strawberry Perl 5.10.1.5 and have version LWP 5.837. I am sure it is something stupid that I am missing.

      #!/usr/bin/perl use strict; use warnings; use LWP::Simple; my $rc = getstore('http://www.perlmeme.org', 'some_local_fi +le'); print status_message($rc), "\n"; getstore('http://www.perlmeme.org', 'some_local_file'); exit 0;
        As per suggestion from Corion, what happens when you run this?

        The HTTP return code is not available for get() without using the LWP::UserAgent OO interface, but this 3 lines of code should ensure that your Windows box is actually getting something (runs on my XP Perl 5.10.1). If this prints something, then it comes down to probable file naming or permission errors in your getstore() statement.

        It is hard for me to understand how a new file could be created with no content if the RC is 'OK'. That would imply that you do have the permission to create the file in the first place and therefore have permission to write to it!
        Testing Note: delete your "blank" output file between runs to make sure that it is actually being created and not an artifact of some previous run.

        Anyway run this simple test and report back.

        #!/usr/bin/perl -w use strict; use LWP::Simple; my $content = get("http://perlmonks.org/?node_id=957704"); die "blank content" unless defined $content; print $content;

        Works fine for me fetching http://perlmonks.org/?node_id=957704 on Windows XP using ActiveState Perl 5.10.1. I doubt very much that it is either the build of Perl or the OS.

        True laziness is hard work