Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Perl从另一个文件读取地址_Perl - Fatal编程技术网

使用Perl从另一个文件读取地址

使用Perl从另一个文件读取地址,perl,Perl,我正在尝试创建一个脚本,该脚本将从另一个文件读取URL。我的第一步是使用我的URL创建文件: cat > address.txt https://unix.stackexchange.com 然后创建perl脚本: #!/usr/bin/perl use LWP::Simple; $content = get($URL); die "Couldn't get it!" unless defined $content; 在脚本中,如何从address.txt而不是$URL设置地址?我假设

我正在尝试创建一个脚本,该脚本将从另一个文件读取URL。我的第一步是使用我的URL创建文件:

cat > address.txt
https://unix.stackexchange.com
然后创建perl脚本:

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

在脚本中,如何从address.txt而不是$URL设置地址?

我假设文件中始终只有一行

首先,始终使用警告;严格使用,;在脚本的顶部。这在你开始之前就抓住了最常见和最基本的问题,比如不使用my声明变量

您需要使用三参数形式创建文件,并使用die捕捉任何错误,然后需要将文件中的行指定给变量,然后关闭任何换行符

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;

你试过从文件中读取吗?可能重复?问题列出了许多读取文件的方法。例如,open F、$filename或die无法读取$filename:$$文件=;关闭F;