为什么下面的代码中缺少哈希值
我想向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到的错误是:
并且生成错误的代码行是:
该错误意味着当您尝试调用
get()
方法时,$ua
包含undef
。这是因为您在这行代码上创建了一个名为$ua
的新变量 - 这就是my
的作用。由于您没有为这个新变量赋值,因此它将包含undef
。我不确定为什么
my
在那里。而且我不确定我是否真的理解您的代码应该如何工作。还有另一个名为$ua
的变量,已定义但未在上面几行定义的匿名子例程中使用 (my( $ua, $tx ) = @_;
)但该变量不存在于该子例程之外。您是否可能指的是使用
$jira_ua
代替的行:The error you're getting is:
And the line of code that generates the error is:
The error means that
$ua
containsundef
when you try to call theget()
method on it. That's because you create a new variable called$ua
on this line of code - that's whatmy
does. And because you don't assign a value to this new variable, it will containundef
.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: