-5

I'm trying to create a script that will read URL from another file. My first step is a creation of the file with my URL:

cat > address.txt
https://unix.stackexchange.com

Then I create perl-script:

#!/usr/bin/perl
use LWP::Simple;
$content = get($URL);
die "Couldn't get it!" unless defined $content;

What should I do to set address from address.txt instead of $URL in my script?

fuser
  • 283
  • 2
  • 7
  • 18
  • 4
    Have you tried just reading from file ? Possible duplicate of http://stackoverflow.com/questions/206661/what-is-the-best-way-to-slurp-a-file-into-a-string-in-perl ? – romants Nov 18 '15 at 18:19
  • 1
    The question lists many ways to read from file. For example open F, $filename or die "Can't read $filename: $!"; $file = ; close F; – romants Nov 18 '15 at 18:24
  • http://perldoc.perl.org/functions/open.html – Sobrique Nov 18 '15 at 21:15

1 Answers1

2

I'm going to assume that the file will always only have one line in it...

First, always put use warnings; and use strict; at the top of your scripts. This catches the most common and basic problems before you even get going (like not declaring your variables with my for instance).

You need to open the file (using the three-argument form, and catching any errors with die), then you need to assign the line in the file to a variable, then chomp off any newline character.

use warnings;
use strict;

use LWP::Simple;

my $file = 'address.txt';

open my $fh, '<', $file
  or die "can't open the $file file!: $!";

my $url = <$fh>;
chomp $url;

my $content = get($url);
die "Couldn't get it!" unless defined $content;
stevieb
  • 9,065
  • 3
  • 26
  • 36