如果我们像这样设置一个指定的格式。有什么方法可以复制输出并将其放入文件中。
ps:当我使用严格时,它显示“全局符号“$counter”需要在 aggregator.pl 第 19 行明确的包名称”。这是什么原因造成的?我已经使用 local 来定义它的范围,所以我有点困惑。希望有人能给我一个答复。多谢
enter code here
# Setup includes
# use strict;
use XML::RSS;
use LWP::UserAgent;
# Declare variables for URL to be parsed
my $url2parse;
# Get the command-line argument
my $arg = shift;
# Create new instance of XML::RSS
my $rss = new XML::RSS;
# Get the URL, assign it to url2parse, and then parse the RSS content
$url2parse = get($arg);
die "Could not retrieve $arg" unless $url2parse;
$rss->parse($url2parse);
#create arrays to hold data
my @titles;
local $counter = 0;
#open file and write .txt output to it
open my $fh, ">output.txt" or die "File creation failed: $!";
# Print the channel items
foreach my $item (@{$rss->{'items'}}) {
$titles[$counter] = $item->{'title'};
&format_output($item->{'title'});
$counter++;
}
sub get {
my $url = shift;
my $ua = LWP::UserAgent->new();
my $res = $ua->get($url);
die ("Could not retrieve $url: " . $res->status_line) unless($res->is_success);
return $res->content;
}
sub format_output {
local($title) = @_;
$~ = "MYFORMAT";
write;
print $fh @_;
}
format MYFORMAT =
=======================
Title :~ ^<<<<<<<<<
$title
=======================
.