0

我有以下代码:

#!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/
                         ],

我忽略或做错了什么明显的事情吗?非常感谢您对此的帮助,非常感谢

4

2 回答 2

1

尝试

push( @{ $newURLs->{$baseURL} }, "".$url );
于 2013-03-04T21:08:04.737 回答
1

$url 是一个对象。为了得到它的字符串化,你可以让它插值:

 push @{ $newURLs->{$baseURL} }, "$url";

或调用as_string方法:

push @{ $newURLs->{$baseURL} }, $url->as_string;
于 2013-03-04T21:10:07.670 回答