Search code examples
perlurlwhile-looplwp

Perl iterate through list of URLS and extra files


I have a list of json files I need to download. I placed the URLs in a txt file called URL_FILE_LIST.txt the values of that file are:

https://www.fema.gov/api/open/v1/DeclarationDenials.json
https://www.fema.gov/api/open/v2/DisasterDeclarationsSummaries.json

The perl script I am calling looks like:

#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;
use LWP::Simple;


my $file = 'URL_FILE_LIST.txt';
open my $info, $file or die "Could not open $file: $!";

while( my $line = <$info>)  {   
        getstore($line , basename($line));
        last if $. == 2;
}

close $info;

When I print the parts of the getstore, it looks right, I am able to see the URL and the base name. The issue is that nothing happens.

When i get rid of the loop and just pass it the URL as an ARG[0] it works fine. What am I doing wrong?

I have removed the While loop and passed in the URL as ARG[0] and it works fine.


Solution

  • Adding chomp solved the issue.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use File::Basename;
    use LWP::Simple;
    
    
    my $file = 'URL_FILE_LIST.txt';
    open my $info, $file or die "Could not open $file: $!";
    
    while( my $line = <$info>)  {  
           chomp $line;
            getstore( $line , basename( $line));
           last if $. == 2;
           }
    
    close $info;