为什么下面的代码中缺少哈希值

发布于 2025-01-09 06:59:05 字数 1597 浏览 0 评论 0原文

我想向 jira 和 jfrog 发出 POST 请求。当我尝试获取循环内的哈希值时,我无法第二次获取该值。我尝试从 env 读取变量(API 令牌而不是密码),如果未设置,它将传递用户名和密码。

my %urls = (
    'jira' => {
        'url' => 'https://jira.com:123/rest/api/2/issue/', 
        'token' => 'JIRA_TOKEN'
    },
    'jfrog' => {
        'url' => 'https://jfrog.com/artifactory/api/storage/path/to/artifacts',
        'token' => 'JFROG_TOKEN'
    }
);

my $jira_ua = Mojo::UserAgent->new();
for my $outer_elem ( keys %urls ) {
    for my $inner_elem ( keys %{$urls{$outer_elem}} ) {
        # print $inner_elem;
        if ( !$ENV{$urls{$outer_elem}{'token'}} ) {
            print "Enter username : \n";
            my $username = <STDIN>;
            chomp($username);
            my $passwd = read_password("Enter Password: ");
            $url = Mojo::URL->new($urls{$outer_elem}->{'url'})
            ->userinfo($username.':'.$passwd);
        }
        else {
            if ( $inner_elem eq "jira" ) {
                $tx->req->headers->authorization (
          "Bearer $ENV{$urls{$outer_elem}->{'token'}}"
               );
                $url = Mojo::URL->new($urls{$outer_elem}->{'url'})
            }
        }
        print $outer_elem . "\n";
        print "logging in to $urls{$outer_elem}->{'url'}\n";
        $tx = my $ua->get($url); # <--- line 170
        my $res = $tx->res->code;
        print $res;
        last;
    }
}

我得到以下输出

D:\scripts>perl emtf.pl
jira
Can't call method "get" on an undefined value at emtf.pl line 170.

I wanted to do a POST request to jira and jfrog. while I try to get the values of the hash within the loop am not able to get the value the second time. I try to read the variable(API token instead of password) from env, if not set it will pass the username and password.

my %urls = (
    'jira' => {
        'url' => 'https://jira.com:123/rest/api/2/issue/', 
        'token' => 'JIRA_TOKEN'
    },
    'jfrog' => {
        'url' => 'https://jfrog.com/artifactory/api/storage/path/to/artifacts',
        'token' => 'JFROG_TOKEN'
    }
);

my $jira_ua = Mojo::UserAgent->new();
for my $outer_elem ( keys %urls ) {
    for my $inner_elem ( keys %{$urls{$outer_elem}} ) {
        # print $inner_elem;
        if ( !$ENV{$urls{$outer_elem}{'token'}} ) {
            print "Enter username : \n";
            my $username = <STDIN>;
            chomp($username);
            my $passwd = read_password("Enter Password: ");
            $url = Mojo::URL->new($urls{$outer_elem}->{'url'})
            ->userinfo($username.':'.$passwd);
        }
        else {
            if ( $inner_elem eq "jira" ) {
                $tx->req->headers->authorization (
          "Bearer $ENV{$urls{$outer_elem}->{'token'}}"
               );
                $url = Mojo::URL->new($urls{$outer_elem}->{'url'})
            }
        }
        print $outer_elem . "\n";
        print "logging in to $urls{$outer_elem}->{'url'}\n";
        $tx = my $ua->get($url); # <--- line 170
        my $res = $tx->res->code;
        print $res;
        last;
    }
}

I get below output

D:\scripts>perl emtf.pl
jira
Can't call method "get" on an undefined value at emtf.pl line 170.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2025-01-16 06:59:05

您收到的错误是:

无法对未定义的值调用方法“get”

并且生成错误的代码行是:

$tx = 我的 $ua->get($urls{$outer_elem}->{'url'});

该错误意味着当您尝试调用 get() 方法时,$ua 包含 undef 。这是因为您在这行代码上创建了一个名为 $ua 的新变量 - 这就是 my 的作用。由于您没有为这个新变量赋值,因此它将包含 undef

我不确定为什么 my 在那里。而且我不确定我是否真的理解您的代码应该如何工作。还有另一个名为 $ua 的变量,已定义但未在上面几行定义的匿名子例程中使用 (my( $ua, $tx ) = @_;)但该变量不存在于该子例程之外。

您是否可能指的是使用 $jira_ua 代替的行:

$tx = $jira_ua->get($urls{$outer_elem}->{'url'});

The error you're getting is:

Can't call method "get" on an undefined value

And the line of code that generates the error is:

$tx = my $ua->get($urls{$outer_elem}->{'url'});

The error means that $ua contains undef when you try to call the get() method on it. That's because you create a new variable called $ua on this line of code - that's what my does. And because you don't assign a value to this new variable, it will contain undef.

I'm not sure why the my is there. And I'm not sure I really understand how your code is supposed to work. There's another variable called $ua that is defined but not used in the anonymous subroutine that's defined a few lines above (my( $ua, $tx ) = @_;) but that variable doesn't exist outside that subroutine.

Did you, perhaps mean the line to use $jira_ua instead:

$tx = $jira_ua->get($urls{$outer_elem}->{'url'});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文