小编zos*_*tay的帖子

使用perl按依赖性排序数组

有一系列哈希,

my @arr = get_from_somewhere();
Run Code Online (Sandbox Code Playgroud)

@arr内容(例如)是:

@arr = (
  { id => "id2",    requires => 'someid', text => "another text2" },
  { id => "xid4",   requires => 'id2',    text => "text44" },
  { id => "someid", requires => undef,    text => "some text" },
  { id => "id2",    requires => 'someid', text => "another text2" },
  { id => "aid",    requires => undef,    text => "alone text" },
  { id => "id2",    requires => 'someid', text => "another text2" }, …
Run Code Online (Sandbox Code Playgroud)

sorting algorithm perl cpan graph

11
推荐指数
1
解决办法
454
查看次数

Perl 6 Regex匹配Perl 6分隔注释

任何人都有一个Perl 6正则表达式,它将匹配Perl 6分隔的注释?我更喜欢那些短而不是完整语法的东西,但我不排除任何东西.

作为我正在寻找的一个例子,我想要一些可以在这里解析注释的东西:

#`{ foo {} bar }
#`« woo woo »
say #`(
This is a (
long )
multiliner()) "You rock!"
#`{{ { And don't forget the tricky repeating delimiters }}
Run Code Online (Sandbox Code Playgroud)

我的总体目标是能够获取源文件并删除pod和注释,然后使用剩下的代码执行有趣的操作.剥线注释和pod非常简单,但分隔注释需要额外的技巧.我也希望这个解决方案很小,只使用Perl 6核心,所以我可以将它粘贴在我的dotfiles repo中而不需要外部依赖.

regex perl6

7
推荐指数
1
解决办法
139
查看次数

如何使用$ 1,$ 2,$ 3动态分配哈希值

我想动态创建一个%detail哈希,而不使用eval语句.这段代码可以正常使用eval语句,但有没有更好的方法来执行此操作而不使用eval

my @input=('INFO: Vikram 32 2012','SAL: 12000$','ADDRESS: 54, junk, JUNK');

my %matching_hash= (
                    qr/^INFO:\s*(\S+)\s+(\S+)\s+(\S+)/ =>['name','age','joining'],
                    qr/^SAL:\s*(\S+)/ => ['salary'],
                    qr/ADDRESS:\s*(.*)/ =>['address']
                    );
my %detail;
while(my ($regex, $array) = each(%matching_hash)) {
    foreach (@input){
        if(/$regex/) {
            for(my $i=0;$i<=$#$array; $i++) {
                $j=$i+1;
                eval '$detail{$array->[$i]} = $$j';
            }
        }
    }
}
use Data::Dumper;

print Dumper(\%detail);
++++++++++++++

$VAR1 = {
          'name' => 'Vikram',
          'address' => '54, junk, JUNK',
          'age' => '32',
          'joining' => '2012',
          'salary' => '12000$'
        };
Run Code Online (Sandbox Code Playgroud)

regex perl eval

6
推荐指数
2
解决办法
767
查看次数

如何调试Module :: Starter制作的模块?

我正在练习使用Module :: Starter创建一个新模块.我为包编写了一些测试用例,有时它们运行正常.

但是我注意到有两个问题:

  • 当测试用例失败时,我想在正在测试的函数中放入一些print语句.我跑了make test,它只告诉我我的测试用例失败了,它没有显示我的打印输出,尽管我确实已经达到了打印语句.

  • 假设我有三个测试用例来测试一个函数,我在函数内部放了一个print语句,当测试用例运行时,它会报告三个测试用例中只有一个被运行.如果我删除print语句,则将运行所有三个测试用例.这是为什么?

这是我的代码:

# package declaration and stuff...
sub get_in {
  my ( $hash, @path ) = @_;
  my $ref = $hash;
  print 'lol'; # This is the troublesome print statement. Remove this statement and all three test cases will run and pass
  foreach (@path) {
    if ( ref($ref) eq 'HASH' && exists $ref->{$_} ) {
      $ref = $ref->{$_};
    } else {
      return undef;
    }
  }
  return $ref;
}
Run Code Online (Sandbox Code Playgroud)

这是测试用例:

use Test::More tests …
Run Code Online (Sandbox Code Playgroud)

testing perl

5
推荐指数
1
解决办法
91
查看次数

将JSON字符串转换为Perl/Moose对象

我有一个JSON字符串,例如

use JSON::XS qw(decode_json);
say Dumper( decode_json($json) );
Run Code Online (Sandbox Code Playgroud)

将产生:

$VAR1 = {
    'Fname' => 'SomeFname',
    'Lname' => 'SomeLname',
    'Addr' => {
          'Street => 'Somestreet',
          'Zip' => '00000',
    },
};
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种简单的方法,如何将JSON字符串(或perl结构)"转换"为Perl/Moose对象,如:

 package My;
 use Moose;
 has 'Fname' => (is => 'rw', isa => 'Str');
 has 'Lname' => (is => 'rw', isa => 'Str');
 has 'Addr' =>  (is => 'rw', isa => 'My::Addr');
Run Code Online (Sandbox Code Playgroud)

 package My::Addr;
 use Moose;
 has 'Street' => (is => 'rw', isa => 'Str');
 has 'Zip' => (is => …
Run Code Online (Sandbox Code Playgroud)

perl serialization json code-generation moose

4
推荐指数
1
解决办法
1342
查看次数

在Perl中替换字符

我正在努力将每个单词中的所有元音加倍.例如:

$string="if it rains, cover with umbrella";
Run Code Online (Sandbox Code Playgroud)

这是我写的代码,但我没有得到正确的输出.

$string=~s/a|e|i|o|u/aa|ee|ii|oo|uu/gi; print $string;
Run Code Online (Sandbox Code Playgroud)

预期产出: iif iit raaiins cooveer wiith uumbreelaa

有人可以帮助我吗?

regex perl

3
推荐指数
1
解决办法
177
查看次数

如何在perl变量上保留尾随空格?

脚本(最初从此处复制)将固定宽度的文本文件作为输入,重新排列列的顺序,并应输出固定宽度的文本文件.但是尾随空格正在从变量中截断,这意味着输出不是固定宽度.

open(INPUT, "</home/ecom/tmp/citiBIG/GROUP.txt");
open(OUTPUT, ">/home/ecom/tmp/citiBIG/GROUP2.txt");

my $LINEFORMAT = "A2 A7 A14 A4 A2 A2 A4 A12 A25 A30 A26 A40 A40 A40 A25 A4 A12 A14 A2 A8 A12 A70 A8"; # Adjust to your
 field widths

while(<INPUT>) {
    chomp;
    my($Null0, $EmpNum, $CcNumber, $Null1, $CcExpYy, $CcExpMm, $Null2, $Title, $LastName, $FirstName, $HolderName, $Ad
dress1, $Address2, $Address3, $Suburb, $State, $PostCode, $Null3, $AreaCode, $WorkPhone, $Null4, $Email, $GroupName) =
 unpack($LINEFORMAT, $_);

    print OUTPUT $EmpNum . "               " . "~" . $LastName . …
Run Code Online (Sandbox Code Playgroud)

perl file unpack fixed-width

3
推荐指数
1
解决办法
490
查看次数

如何调试另一个 perl 脚本中使用的 perl 脚本?

如何调试另一个 perl 脚本中使用的 perl 脚本。我们可以分别调试两者。是否可以一步调试?

前任:

!/user/bin/perl

my $param= 8;

my @res=\`perl extract.pl $config`;

print "The results is ....  @res\n";
Run Code Online (Sandbox Code Playgroud)

同样,我们可以调试shell脚本中使用的perl脚本吗?

谢谢。

debugging perl

2
推荐指数
1
解决办法
909
查看次数

如何在给定日期的perl中查找上一个日期

如何在perl中找到给定日期的上一个日期为8月17日.我可以使用下面的代码获取perl中的当前日期.可以帮助我找到给定日期的上一个日期.

Given date as : Aug 17
Required Date as : Aug 16
#Reverting the Date for the respective TimeZone
my $timezone='london';
my ($dt, $Curr_Bus_Date,$Curr_day);
$dt = DateTime->now(time_zone => $timezone);
$Curr_mon=$dt->strftime('%b');
$Curr_date=$dt->strftime('%d');
$Curr_date=~s/^0/ /;
$Curr_dt="$Curr_mon $Curr_date ";
print "The curr date is $Curr_dt \n";
Run Code Online (Sandbox Code Playgroud)

perl datetime date

1
推荐指数
1
解决办法
2272
查看次数

将字符串"20-May-07"转换为日期并进行操作

在我的perl程序中,我计算保修结束日期的保修开始日期差异为3年.以下是我到目前为止编写的代码块:

use Time::ParseDate;
use DateTime;
use POSIX;
use DateTime::Duration;

my $warranty_expiration_string = "20-May-07";
my $epoch = parsedate($warranty_expiration_string);
my $warranty_expiration = strftime "%D", localtime($epoch);   # returns: 05/20/07
Run Code Online (Sandbox Code Playgroud)

如何从$warranty_expiration获得$warranty_start日期中扣除3年?
我试过了,

$warranty_start->subtract(DateTime::Duration->new('years' => 3));
Run Code Online (Sandbox Code Playgroud)

..但它没有用.

perl datetime

0
推荐指数
1
解决办法
1572
查看次数