我有以下代码:
#!usr/bin/perl
use strict;
use warnings;
use URI qw( );
my @insert_words = qw( HELLO );
my $newURLs;
while ( my $baseURL = <DATA>) {
chomp $baseURL;
my $url = URI->new($baseURL);
my $path = $url->path();
for (@insert_words) {
# Use package vars to communicate with /(?{})/ blocks.
local our $insert_word = $_;
local our @paths;
$path =~ m{
^(.*[/])([^/]*)((?:[/].*)?)\z
(?{
push @paths, "$1$insert_word$2$3";
if (length($2)) {
push @paths, "$1$insert_word$3";
push @paths, "$1$2$insert_word$3";
}
})
(?!)
}x;
for (@paths) {
$url->path($_);
print "$url\n"; #THIS PRINTS THE CORRECT URLS I WANT IN THE ARRAY REF
push( @{ $newURLs->{$baseURL} }, $url ); #TO PUT EACH URL INTO AN ARRAYREF BUT ITS NOT WORKING
}
}
}
print "\n"; #for testing only
print Dumper($newURLs); #for testing only
print "\n"; #for testing only
__DATA__
http://www.stackoverflow.com/dog/cat/rabbit/
http://www.superuser.co.uk/dog/cat/rabbit/hamster/
http://10.15.16.17/dog/cat/rabbit/
我遇到的问题:
当我执行print "$url\n";
时,如上面的代码所示,它会打印出我想要放入数组ref的正确URL,但是当我执行push( @{ $newURLs->{$baseURL} }, $url );
时,我在数据中得到以下内容结构:
$VAR1 = {
'http://www.stackoverflow.com/dog/cat/rabbit/' => [
bless( do{\(my $o = 'http://www.stackoverflow.com/dogHELLO/cat/rabbit/')}, 'URI::http' ),
$VAR1->{'http://www.stackoverflow.com/dog/cat/rabbit/'}[0],
$VAR1->{'http://www.stackoverflow.com/dog/cat/rabbit/'}[0],
$VAR1->{'http://www.stackoverflow.com/dog/cat/rabbit/'}[0],
$VAR1->{'http://www.stackoverflow.com/dog/cat/rabbit/'}[0],
$VAR1->{'http://www.stackoverflow.com/dog/cat/rabbit/'}[0],
$VAR1->{'http://www.stackoverflow.com/dog/cat/rabbit/'}[0],
$VAR1->{'http://www.stackoverflow.com/dog/cat/rabbit/'}[0],
$VAR1->{'http://www.stackoverflow.com/dog/cat/rabbit/'}[0],
$VAR1->{'http://www.stackoverflow.com/dog/cat/rabbit/'}[0]
],
我应该得到的是以下
$VAR1 = {
'http://www.stackoverflow.com/dog/cat/rabbit/' => [
http://www.stackoverflow.com/dog/cat/rabbit/HELLO
http://www.stackoverflow.com/dog/cat/HELLOrabbit/
http://www.stackoverflow.com/dog/cat/HELLO/
http://www.stackoverflow.com/dog/cat/rabbitHELLO/
http://www.stackoverflow.com/dog/HELLOcat/rabbit/
http://www.stackoverflow.com/dog/HELLO/rabbit/
http://www.stackoverflow.com/dog/catHELLO/rabbit/
http://www.stackoverflow.com/HELLOdog/cat/rabbit/
http://www.stackoverflow.com/HELLO/cat/rabbit/
http://www.stackoverflow.com/dogHELLO/cat/rabbit/
],
我忽视或做错了是否显而易见?非常感谢您对此的帮助,非常感谢
答案 0 :(得分:1)
尝试
push( @{ $newURLs->{$baseURL} }, "".$url );
答案 1 :(得分:1)
$ url是一个对象。要获得字符串化,可以让它进行插值:
push @{ $newURLs->{$baseURL} }, "$url";
或调用as_string
方法:
push @{ $newURLs->{$baseURL} }, $url->as_string;