小编jm6*_*666的帖子

使用unicode文件名的可移植(跨平台)脚本

这让我发疯了.有下一个bash脚本.

testdir="./test.$$"
echo "Creating a testing directory: $testdir"
mkdir "$testdir"
cd "$testdir" || exit 1

echo "Creating a file word.txt with content á.txt"
echo 'á.txt' > word.txt

fname=$(cat word.txt)
echo "The word.txt contains:$fname"

echo "creating a file $fname with a touch"
touch $fname
ls -l

echo "command: bash cycle"
while read -r line
do
    [[ -e "$line" ]] && echo "$line is a file"
done < word.txt

echo "command: find . -name $fname -print"
find . -name $fname -print

echo …
Run Code Online (Sandbox Code Playgroud)

bash

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

对utf8文件进行基准读取 - 解释差异

有这个代码:

#!/usr/bin/env perl

use 5.016;
use warnings;
use autodie;
use Path::Tiny;
use Encode;
use Benchmark qw(:all);

my $cnt = 10_000;
my $utf = 'utf8.txt';

my $res = timethese($cnt, {
    'open-UTF-8' => sub {
        open my $fhu, '<:encoding(UTF-8)', $utf;
        my $stru = do { local $/; <$fhu>};
        close $fhu;
    },
    'open-utf8' => sub {
        open my $fhu, '<:utf8', $utf;
        my $stru = do { local $/; <$fhu>};
        close $fhu;
    },
    'decode-utf8' => sub {
        open my $fhu, '<', $utf;
        my $stru …
Run Code Online (Sandbox Code Playgroud)

io perl encode utf-8

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

为什么正则表达式/ [\ w\W] + x/i运行速度极慢?

尝试:

time perl -E '$x="a" x 100000; $x =~ /[\w\W]+x/i'  
Run Code Online (Sandbox Code Playgroud)

将运行很长时间(在我的笔记本上20秒).没有/i,例如

time perl -E '$x="a" x 100000; $x =~ /[\w\W]+x/' 
Run Code Online (Sandbox Code Playgroud)

完成0.07秒.

无论正则表达式[\w\W]没有多大意义,这种巨大的差异让我感到惊讶.

为何如此大的差异?

编辑

更确切地说:

$ time perl -E '$x="a" x 100000; $x =~ /[\w\W]+x/i'  

real    0m19.479s
user    0m19.419s
sys 0m0.038s
Run Code Online (Sandbox Code Playgroud)

我的 perl

Summary of my perl5 (revision 5 version 20 subversion 3) configuration:

  Platform:
    osname=darwin, osvers=15.0.0, archname=darwin-2level
    uname='darwin nox.local 15.0.0 darwin kernel version 15.0.0: sat sep 19 15:53:46 pdt 2015; root:xnu-3247.10.11~1release_x86_64 x86_64 '
    config_args='-Dprefix=/opt/anyenv/envs/plenv/versions/5.20.3 -de …
Run Code Online (Sandbox Code Playgroud)

regex perl

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

graphviz定义更多"默认值"

需要绘制一个复杂的图形,其中有3种类型的边缘和几种预定义的节点类型.

是否可以在稍后使用中定义更多(例如,不仅一个默认值)"边缘类型"(或节点类型)?

意思是这样的:

edge [colorscheme=paired12, color=8, fontsize=11, fontname="Arial narrow"];
edge2 [colorscheme=paired12, color=3, fontsize=11, fontname="Arial narrow", style=bold];
edge3 [colorscheme=paired12, color=5, fontsize=14, fontname="Arial narrow"];

node1 -> node2; /* will use the default edge definition from the above */
node2 -> node3 [edgetype=edge2]; /* will use the second edge definition */
node2 -> node4 [edgetype=edge3]; /* and so on... */
Run Code Online (Sandbox Code Playgroud)

以上,ofc,不正确 - 只是为了解释......

graphviz

8
推荐指数
2
解决办法
2810
查看次数

Perl和MongoDB二进制数据

MongoDB手册:

默认情况下,所有数据库字符串都是UTF8.要保存图像,二进制文件和其他非UTF8数据,可以将字符串作为对数据库的引用传递.

我正在抓取页面并希望存储内容以供以后处理.

  • 我不能依赖meta-charset,因为很多页面都有utf8内容但错误地声明iso-8859-1或类似
  • 所以不能使用Encode(不知道原来的charset)
  • 因此,我希望简单地存储内容as flow of bytes(二进制数据)以供以后处理

我的代码片段:

sub save {
    my ($self, $ok, $url, $fetchtime, $request ) = @_;

    my $rawhead = $request->headers_as_string;
    my $rawbody = $request->content;

    $self->db->content->insert(
        { "url" => $url, "rhead" => \$rawhead, "rbody" => \$rawbody } ) #using references here
      if $ok;

    $self->db->links->update(
        { "url" => $url },
        {
            '$set' => {
                'status'       => $request->code,
                'valid'        => $ok,
                'last_checked' => time(),
                'fetchtime'    => $fetchtime,
            }
        }
    );
} …
Run Code Online (Sandbox Code Playgroud)

perl mongodb

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

YAPE :: Regex ::解释不使用5.014;

这段代码:

use strict;
use warnings;
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new( qr/d+/ )->explain();
Run Code Online (Sandbox Code Playgroud)

版画

The regular expression:

(?-imsx:d+)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  d+                       'd' (1 or more times (matching the most
                           amount possible))
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

但这段代码

use 5.014;  #added this
use strict;
use warnings;
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new( qr/d+/ )->explain();
Run Code Online (Sandbox Code Playgroud)

仅打印:

The …
Run Code Online (Sandbox Code Playgroud)

regex perl

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

找到重复字符的单词

想要在字典中搜索在第二个和最后一个位置具有相同字符的每个单词,并在某个中间位置搜索一次.

例子:

statement - has the "t" at the second, fourth and last place
severe = has "e" at 2,4,last
abbxb = "b" at 2,3,last
Run Code Online (Sandbox Code Playgroud)

错误

abab = "b" only 2 times not 3
abxxxbyyybzzzzb - "b" 4 times, not 3
Run Code Online (Sandbox Code Playgroud)

我的grep不起作用

my @ok = grep { /^(.)(.)[^\2]+(\2)[^\2]+(\2)$/ } @wordlist;
Run Code Online (Sandbox Code Playgroud)

例如

perl -nle 'print if /^(.)(.)[^\2]+(\2)[^\2]+(\2)$/' < /usr/share/dict/words
Run Code Online (Sandbox Code Playgroud)

打印例如

zarabanda
Run Code Online (Sandbox Code Playgroud)

怎么了.

什么应该是正确的正则表达式?

编辑:

如何捕捉封闭的群体?例如为了

statement - want cantupre: st(a)t(emen)t - for the later use

my $w1 = $1; my w2 …
Run Code Online (Sandbox Code Playgroud)

regex perl

8
推荐指数
2
解决办法
1095
查看次数

Perlb的perlbrew

在这里作为东西perlbrewperl6,或推荐的安装方法,因为它在描述

ps:使用OS X.

perl6 raku

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

在Perl正则表达式中不推荐使用左大括号 - 恰好在何时?

perldoc perlre 这样说:

(如果大括号出现在任何其他上下文中并且不构成反斜杠序列的一部分\x{...},则将其视为常规字符.但是,对于所有此类事件都会引发弃用警告,而在Perl v5.26中,文字使用一个花括号的大概需要被转义,比如在它们前面用反斜杠("\{")或将它们括在方括号("[{]")中.这个改变将允许将来的语法扩展(比如使量词的下限可选),以及更好的量词错误检查.)

好的,所以下面打印了弃用消息.

perl -lE 'm/x{x}/'
Run Code Online (Sandbox Code Playgroud)

为什么不以下?

perl -lE 'm/x({x})/'
Run Code Online (Sandbox Code Playgroud)

例如在捕获组中是否{允许未转义?可能不是因为

perl -lE 'm/x(x{x})/'
Run Code Online (Sandbox Code Playgroud)

还打印警告.

那么,什么是确切的"逻辑"?

PS:我会逃避每一个文字{,但我想知道上面背后的理由.

regex perl

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

在perl中按需加载所需的包

重新提出的问题 - 抱歉,这有点长.

例如,有一个简单的包

package My;
use Moose;
use namespace::sweep;
sub cmd1 {1}
sub smd2 {2}
__PACKAGE__->meta->make_immutable;
1;
Run Code Online (Sandbox Code Playgroud)

我希望允许My其他方法扩展其他方法,例如

package My::Cmd3;
use Moose;
extends 'My';
sub cmd3 {3}
1;
Run Code Online (Sandbox Code Playgroud)

这允许使用"基础" MyMy::Cmd3下一个方法:

use My::Cmd3;
my $obj = My::Cmd3->new();
say $obj->cmd1(); #from the base My
say $obj->cmd3(); #from the My::Cmd3;
Run Code Online (Sandbox Code Playgroud)

但这不是我想要的.我不想use My::Cmd3;,(这里会有更多扩展包),我想要use My;.

使用角色是NICER,如:

package My;
use Moose;
with 'My::Cmd3';
sub cmd1 {1}
sub cmd2 {2}
__PACKAGE__->meta->make_immutable;
1;

package …
Run Code Online (Sandbox Code Playgroud)

perl moose

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

标签 统计

perl ×7

regex ×4

bash ×1

encode ×1

graphviz ×1

io ×1

mongodb ×1

moose ×1

perl6 ×1

raku ×1

utf-8 ×1