我喜欢这个小代码项目的一些帮助或“第二双眼睛”。 我负责修复一些旧的Perl代码,它将在新的Web服务器上运行。 代码以秒为单位(自1970年以来),连接短划线,连接服务器进程ID号以创建可变长度文件名。然后在许多下游进程中使用该文件和名称。
文件名创建代码是:$ output_file =“$ time - $$”;
我遇到的问题是,在新服务器上,PID最多可以是6(或7?)位,而旧服务器从未看到超过5位数的PID。其余的下游perl脚本都希望该PID文件名部分最多有5位数(程序在文件名的PID部分遇到6位数时会不合理地退出)。
所以我想要完成的是一个Filename,它总是一个16个字符的固定长度。由10位数时间码,短划线和5位数PID组成(如果PID> 5个字符,则最右边5位)。
我尝试创建一个Perl测试脚本,看看我是否可以处理PID数据值。 (a)如果是6或7位字符,则使用substr命令将PID数据修剪回5; (b)或者使用substr命令,如果小于5,则增加到5个前导零的字符。 (a)使它变短的情况似乎工作正常, 但是(b)尝试使其变大的情况在服务器上的行为不一致。
如果可能的话,我想避免使用grep和正则表达式。 对我或其他支持人员稍后破译的任何事情都没有用处。
我的测试脚本的结构是允许我确保所有测试用例都能产生正确的预期结果。 (我可能正在做一些完全错误/愚蠢的事情......)
我正在粘贴: (a)我的测试脚本,(b)服务器结果的屏幕快照。 Server PERL版本:5.10.1
#!/usr/bin/perl -w
use strict;
use warnings;
#
# Name: testsubstr2.pl
#
my $pidnum7 = "1234567";
my $pidnum6 = "123456";
my $pidnum5 = "12345";
my $pidnum4 = "1234";
my $pidnum3 = "123";
my $pidnum2 = "12";
my $pidnum1 = "1";
if ( length( $pidnum7 ) == 7 )
{ print "This is 7 char PIDnum7. Length = ", length( $pidnum7 ), ", Content: ", $pidnum7, "\n";
my $subst7 = substr ($pidnum7, -5, 5);
print " Now shortened as SUBst7 to: ", length($subst7), ", Content: ", $subst7, "\n";
print " Orig PIDnum7 is length: ", length($pidnum7), ", Content: ", $pidnum7, "\n";
}
else { } # do nothing
# this 7 down to 5 seems to work ok.
#elsif ( length( $pidnum6 ) == 6 )
if ( length( $pidnum6 ) == 6 )
# else the string is 6 (or less)
{ print "This is 6 char PIDnum6. Length = ", length( $pidnum6 ), ", Content: ", $pidnum6, "\n";
my $subst6 = substr ($pidnum6, -5, 5);
print " Now shortened as SUBst6 to: ", length($subst6), ", Content: ", $subst6, "\n";
print " Orig PIDnum6 is length: ", length($pidnum6), ", Content: ", $pidnum6, "\n";
}
else { } # do nothing
# this 6 down to 5 seems to work ok.
#elsif ( length( $pidnum5 ) == 5 )
if ( length( $pidnum5 ) == 5 )
# else the string is exactly 5, do nothing
{ print "This PIDnum5 is exactly 5 char PIDnum, Length = ", length( $pidnum5 ), ", Content: ", $pidnum5, "\n";
}
else { } # do nothing. Fine as is.
#elsif ( length( $pidnum4 ) == 4 )
if ( length( $pidnum4 ) == 4 )
# else the string is 4, too short
{ print "This is 4 char PIDnum4. Length = ", length( $pidnum4 ), ", Content: ", $pidnum4, "\n";
my $subst4 = substr ($pidnum4, 0, 0, "0"); #insert character '0' in front
print " After substr command, PIDnum4 = ", length($pidnum4), ", Content: ", $pidnum4, "\n";
print " Now PID4 in variable SUBst4 is length: ", length($subst4), ", Content: ", $subst4, "\n";
# Weird Result in this code block, the var $pidnum4 becomes 5 chars, 01234, though it should not have changed
# but the temp var $subst4 becomes 0 charcters with Blank (no) content. It should I thought become the 5 character receiver.
# something wrong with this block of code.
}
else { } # do nothing
以下是我的测试脚本运行server test results
的Snaggy链接(结果屏幕上的最后7行来自我在测试脚本中包含的一些通用substr命令示例,可能不需要,所以我将它们排除在上面的代码示例之外。)
谢谢。
答案 0 :(得分:5)
我使用sprintf
和modulo,就像这样:
#!/usr/bin/perl
while (<DATA>) {
chomp;
my $pid = $_;
my $time = time;
my $fn = sprintf("%s-%05d", $time, $pid % 100000);
print "$fn\n";
}
__DATA__
1
12
123
12345
123456
7654321
输出:
1485862025-00001
1485862025-00012
1485862025-00123
1485862025-12345
1485862025-23456
1485862025-54321
这是否符合您的需求?