gpt4 book ai didi

perl - 在 perl 中,为什么当哈希包含长字符串时 sprintf(Dumper\%hash) 会抛出警告?

转载 作者:行者123 更新时间:2023-12-04 14:36:51 25 4
gpt4 key购买 nike

几个月来,我一直在使用类似以下的语法,而没有触发警告:

die join('', sprintf(Dumper [@stack]), sprintf(Dumper {%oprAtnNOW}), 'opt tojudge not specified');
也就是我用过 sprintfDumper , 不指定格式。
在下面的代码中,我们看到这工作正常, 但仅限于一点。 %oprAtnNOW包含一个长字符串,则触发警告。
(在所有情况下,字符串都编译为正则表达式;但在编译之前,它只是一个字符串。)
是什么导致长字符串警告?为什么会出现“缺失的论点”?
授予, sprintf应该给出一个格式,如
https://perldoc.perl.org/functions/sprintf .
但是为什么这仅在较小的字符串被长字符串替换时才强制执行?
#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper qw(Dumper);
$Data::Dumper::Sortkeys = 1;
print "Perl version: $^V\n";

my %oprAtnNOW;
my $string='~~~~~1983-10-21 Fri 13:01:13, today we went to the movie.';

%oprAtnNOW = (
Vv => {
v=>[ '(?<a>a)',],
},
);

tryit();

%oprAtnNOW = (
Vv => {
v=>[
'(?m)^(?<boundjour2009>(?<tilde5>[~]{5})[\\x20\\t]*(?<dateISO1mbeWeekdaymbeTIME>(?<dateISO1mbeWeekday>(?<dateISO1>(?<YYYY>[1-9]\\d\\d\\d)[-](?<nMonth2>0[1-9]|1[0-2])[-](?<nMonthDay2>3[01]|[0-2][0-9]))([\\x20\\t]+(?<wWeekdayAllor3>Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sun|Mon|Tue|Wed|Thu|Fri|Sat))?)([\\x20\\t]+(?<nTIMEdiverse>(at[\\x20\\t]+)?((?<HHcMMcSS>(?<HH>0[0-9]|1[0-9]|2[0-3]):(?<MM>[0-5][0-9]):(?<SS>[0-5][0-9]))|(?<HHMMmbeSS>(?<HHMM>(?<HH>0[0-9]|1[0-9]|2[0-3])(?<MM>[0-5][0-9]))(?<SS>[0-5][0-9])?)|(?<HHcMM_pct_cSS>(?<HH>0[0-9]|1[0-9]|2[0-3]):(?<MM>[0-5][0-9])%:(?<SS>[0-5][0-9]))|(?<HHcMM_stop>(?<HH>0[0-9]|1[0-9]|2[0-3]):(?<MM>[0-5][0-9])(?![:][0-5][0-9])))))?))',
],
},
);

tryit();

sub tryit
{
my $rgx=qr/$oprAtnNOW{Vv}->{v}->[0]/;
if($string=~$rgx)
{
print Dumper \%+;
}
print "with format:\n";
print sprintf('%s', Dumper \%oprAtnNOW);
print "WITHOUT format:\n";
print sprintf(Dumper \%oprAtnNOW);
}

输出:
Perl version: v5.18.4
$VAR1 = {
'a' => 'a'
};
with format:
$VAR1 = {
'Vv' => {
'v' => [
'(?<a>a)'
]
}
};
WITHOUT format:
$VAR1 = {
'Vv' => {
'v' => [
'(?<a>a)'
]
}
};
$VAR1 = {
'HH' => '13',
'HHcMMcSS' => '13:01:13',
'MM' => '01',
'SS' => '13',
'YYYY' => '1983',
'boundjour2009' => '~~~~~1983-10-21 Fri 13:01:13',
'dateISO1' => '1983-10-21',
'dateISO1mbeWeekday' => '1983-10-21 Fri',
'dateISO1mbeWeekdaymbeTIME' => '1983-10-21 Fri 13:01:13',
'nMonth2' => '10',
'nMonthDay2' => '21',
'nTIMEdiverse' => '13:01:13',
'tilde5' => '~~~~~',
'wWeekdayAllor3' => 'Fri'
};
with format:
$VAR1 = {
'Vv' => {
'v' => [
'(?m)^(?<boundjour2009>(?<tilde5>[~]{5})[\\x20\\t]*(?<dateISO1mbeWeekdaymbeTIME>(?<dateISO1mbeWeekday>(?<dateISO1>(?<YYYY>[1-9]\\d\\d\\d)[-](?<nMonth2>0[1-9]|1[0-2])[-](?<nMonthDay2>3[01]|[0-2][0-9]))([\\x20\\t]+(?<wWeekdayAllor3>Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sun|Mon|Tue|Wed|Thu|Fri|Sat))?)([\\x20\\t]+(?<nTIMEdiverse>(at[\\x20\\t]+)?((?<HHcMMcSS>(?<HH>0[0-9]|1[0-9]|2[0-3]):(?<MM>[0-5][0-9]):(?<SS>[0-5][0-9]))|(?<HHMMmbeSS>(?<HHMM>(?<HH>0[0-9]|1[0-9]|2[0-3])(?<MM>[0-5][0-9]))(?<SS>[0-5][0-9])?)|(?<HHcMM_pct_cSS>(?<HH>0[0-9]|1[0-9]|2[0-3]):(?<MM>[0-5][0-9])%:(?<SS>[0-5][0-9]))|(?<HHcMM_stop>(?<HH>0[0-9]|1[0-9]|2[0-3]):(?<MM>[0-5][0-9])(?![:][0-5][0-9])))))?))'
]
}
};
WITHOUT format:
Missing argument in sprintf at /Users/kpr/u/kh/bin/z.pl line 38.
Invalid conversion in sprintf: "%:" at /Users/kpr/u/kh/bin/z.pl line 38.
$VAR1 = {
'Vv' => {
'v' => [
'(?m)^(?<boundjour2009>(?<tilde5>[~]{5})[\\x20\\t]*(?<dateISO1mbeWeekdaymbeTIME>(?<dateISO1mbeWeekday>(?<dateISO1>(?<YYYY>[1-9]\\d\\d\\d)[-](?<nMonth2>0[1-9]|1[0-2])[-](?<nMonthDay2>3[01]|[0-2][0-9]))([\\x20\\t]+(?<wWeekdayAllor3>Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sun|Mon|Tue|Wed|Thu|Fri|Sat))?)([\\x20\\t]+(?<nTIMEdiverse>(at[\\x20\\t]+)?((?<HHcMMcSS>(?<HH>0[0-9]|1[0-9]|2[0-3]):(?<MM>[0-5][0-9]):(?<SS>[0-5][0-9]))|(?<HHMMmbeSS>(?<HHMM>(?<HH>0[0-9]|1[0-9]|2[0-3])(?<MM>[0-5][0-9]))(?<SS>[0-5][0-9])?)|(?<HHcMM_pct_cSS>(?<HH>0[0-9]|1[0-9]|2[0-3]):(?<MM>[0-5][0-9])%:(?<SS>[0-5][0-9]))|(?<HHcMM_stop>(?<HH>0[0-9]|1[0-9]|2[0-3]):(?<MM>[0-5][0-9])(?![:][0-5][0-9])))))?))'
]
}
};

最佳答案

这不是因为长度,而是因为长字符串包含一个百分号。

...(?<MM>[0-5][0-9])%:(?<SS>[0-5][0-9]))...
~
因为它是唯一的参数,所以它被解释为格式。
您可以使用更短的字符串来演示相同的行为,例如
sprintf '%';
如果您不需要格式化,只需使用 print :
print Dumper \%oprAtnNOW; 

关于perl - 在 perl 中,为什么当哈希包含长字符串时 sprintf(Dumper\%hash) 会抛出警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68500372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com