2012-12-28 83 views
1
#!/usr/bin/perl 
use strict; 
use warnings; 
use LWP::UserAgent; 
use XML::Twig; 
use HTTP::Request; 

my @joblist = ('Testing','Integrity','TEST','Team_test','test','TEST_1','Update_Outlook'); 
my @score; 

foreach my $job_name (@joblist) { 
    my $url_a = 'http://myhost:8080/job/$job_name/api/xml'; 
    my $ua = LWP::UserAgent->new; 
    $ua->timeout(10); 
    $ua->env_proxy; 
    my $response = $ua->get($url_a); 
    if ($response->is_success) { 
     my $content = $response->decoded_content; # or whatever 
     XML::Twig->new(twig_roots => { 'healthReport/score' => sub { push @score, $_->text; } }) ->parseurl($url_a); 
     foreach my $var (@score) { 
      print "$var \n"; 
     } 
    } 
    else { 
     die $response->status_line; 
    } 
} 

在上面的perl代碼中,我將$ job_name調用到另一個變量$ url_a中。 但我得到以下錯誤。將代碼變量hostname,portnumber調用到另一個變量的Perl代碼

404 Not Found at health.pl line 25. 

請問有人能幫我解答。謝謝。

+1

這部分似乎很爲我好。所以有兩種可能性:.1錯誤位於下一行(您可能會在您的SO問題中打印更多行).2您的perl安裝沒有完成或錯誤。 –

+0

@ F.Hauri是正確的。你的代碼也適用於我。請[編輯]你的問題,並添加後面的行。你使用'strict'和'warnings'嗎? – simbabque

+0

謝謝@ F.Hauri ,.我用完整的代碼更新了這個問題。 – MangeshBiradar

回答

1

試試這個版本:

#!/usr/bin/perl 
use strict; 
use warnings; 
use LWP::UserAgent; 
use XML::Twig; 
use HTTP::Request; 

my @joblist = qw|Testing Integrity TEST Team_test test TEST_1 Update_Outlook|; 
my @score; 

foreach my $job_name (@joblist) { 
    my $url_a = join("/","http://myhost:8080/job",$job_name,"api/xml"); 
    my $ua = LWP::UserAgent->new; 
    $ua->timeout(10); 
    $ua->env_proxy; 
    my $response = $ua->get($url_a); 
    if ($response->is_success) { 
     my $content = $response->decoded_content; # or whatever 
     XML::Twig->new(twig_roots => { 'healthReport/score' => sub { push @score, $_->text; } }) ->parseurl($url_a); 
     foreach my $var (@score) { 
      print "$var \n"; 
     } 
    } 
    else { 
     printf STDERR "ERROR job: %s, result: %s\n", 
     $job_name, $response->status_line; 
    } 
} 
+0

謝謝@ F.Hauri。 – MangeshBiradar