【问题标题】:Read address from another file using Perl使用 Perl 从另一个文件读取地址
【发布时间】:2016-02-20 14:55:58
【问题描述】:

我正在尝试创建一个从另一个文件读取 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 设置地址?

【问题讨论】:

标签: perl


【解决方案1】:

我将假设该文件将始终只有一行...

首先,始终将use warnings;use strict; 放在脚本的顶部。这可以在您开始之前解决最常见和基本的问题(例如,不使用 my 声明变量)。

您需要open 文件(使用三参数形式,并使用die 捕获任何错误),然后您需要将文件中的行分配给变量,然后chomp 关闭任何换行符字符。

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;

【讨论】:

    猜你喜欢
    • 2023-01-26
    • 2021-02-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多