Perl代码将变量hostname,portnumber调用到另一个变量中

时间:2012-12-28 08:47:33

标签: perl perl-module

#!/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中。 但我得到了以下错误。

404 Not Found at health.pl line 25.

有人可以帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分: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;
    }
}