質問

I have the following code:

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

The problem I am having:

When I do print "$url\n"; as shown in the code above, it prints the correct urls that I want to put in the array ref, but I when I do push( @{ $newURLs->{$baseURL} }, $url ); I get the following in the data structure:

$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]
                                                        ],

When what I should be getting is the following

$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/
                         ],

Is it something obvious that I am overlooking or doing wrong? Your help with this will be much appreciated, many thanks

役に立ちましたか?

解決

$url is an object. To get its stringification, you can let it interpolate:

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

or call the as_string method:

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

他のヒント

try

push( @{ $newURLs->{$baseURL} }, "".$url );
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top