特定
my $t=+"aaa";
Run Code Online (Sandbox Code Playgroud)
在使用$ t之前,是否有可能检查强制是否会成功(我知道它不在这里)?
顺便说一句:我真正想做的是检查一个字符串是否是一个有效的整数.我知道我可以为此目的使用正则表达式,但我认为有一个更简单的解决方案.
我正在尝试创建一个到RPC服务器的Perl 6客户端接口,类层次结构与服务器URL匹配.例如
# for url path: /account/login
# client code:
$client.account.login;
Run Code Online (Sandbox Code Playgroud)
为此,"子"对象(帐户)需要存储对其父对象(客户端)的引用.
这就是我尝试过的:
#!/usr/bin/env perl6
use v6;
class MyApp::Client::Account {
has $!client;
method login() {
# fake login
$!client.session_id = 'abc';
return 'ok';
}
}
class MyApp::Client {
has $.session_id is rw;
method account() {
state $empire = MyApp::Client::Account.new( :client(self) );
return $empire;
}
}
use Test;
plan( 2 );
my $client = MyApp::Client.new;
my $response = $client.account.login;
is( $response, 'ok', 'login successful' );
ok( $client.session_id, 'client has session_id' ); …Run Code Online (Sandbox Code Playgroud) 考虑以下Perl风味?(我想不是?)正则表达式来测试一个字符串是否是回文:
^((.)(?1)\2|.?)$
Run Code Online (Sandbox Code Playgroud)
试试吧.
下列
my regex palindrome {
^((.)(?1)\2|.?)$
}
say "$word is a palindrome"
if $word ~~ /<palindrome>/
&& $word.chars > 1;
Run Code Online (Sandbox Code Playgroud)
给出错误
===SORRY!===
Quantifier quantifies nothing
at /home/cat/projects/perl6/code/misc/words.pl6:6
------> ^((.)(??1)\2|.?)$
Unrecognized backslash sequence (did you mean $1?)
at /home/cat/projects/perl6/code/misc/words.pl6:6
------> ^((.)(?1)\2?|.?)$
Run Code Online (Sandbox Code Playgroud)
我对(Python)正则表达式有一定的了解,当我说"量化器没有量化"时我得到了它的意思,但是当我要求递归而不是量化时,我不明白为什么它会这样说.
我没有足够的Perl知识来知道为什么它不喜欢反斜杠(另一个错误).
我确实试着搞乱语法和搜索,但就我和互联网而言,这个正则表达式工作,并且摆弄它会产生我不会得到的各种其他错误.
我究竟做错了什么?
如何以允许我终止它们的方式从脚本启动进程?
基本上,我可以轻松地终止主脚本,但终止此主脚本启动的外部进程一直是个问题.我用疯狂搜索Perl 6解决方案.我正准备发布我的问题然后认为我会用其他语言解决问题.
使用Perl 6可以轻松启动外部流程:
my $proc = shell("possibly_long_running_command");
Run Code Online (Sandbox Code Playgroud)
shell在流程完成后返回流程对象.所以,我不知道如何以编程方式找出正在运行的进程的PID,因为$proc在外部进程完成之前甚至都没有创建变量.(旁注:完成后,$proc.pid返回一个未定义的Any,所以它不会告诉我它以前有什么PID.)
以下是一些代码,展示了我创建"自毁"脚本的一些尝试:
#!/bin/env perl6
say "PID of the main script: $*PID";
# limit run time of this script
Promise.in(10).then( {
say "Took too long! Killing job with PID of $*PID";
shell "kill $*PID"
} );
my $example = shell('echo "PID of bash command: $$"; sleep 20; echo "PID of bash command after sleeping is still $$"');
say "This line is never printed";
Run Code Online (Sandbox Code Playgroud)
这会导致以下输出终止主脚本,但不会导致外部创建的进程(请参阅单词后面的输出Terminated …
在per5中,我可以使用变量,例如$ foo或@bar而不使用"my".
$foo=1; @bar=(1,2);
Run Code Online (Sandbox Code Playgroud)
在perl6中,为什么我必须一直使用"我的"?否则编译器会说变量unclared.为什么perl6不能只是autovivify?
print "{my @a=1,2,3;}\n"; # have to use "my" to declare variable
print "{@a=1,2,3;}\n"; # this is error 'Variable '@a' is not declared'
Run Code Online (Sandbox Code Playgroud)
我不喜欢必须总是使用"我的"的限制.这个级别太低了,比如C; 非常麻烦.
有没有办法打开始终自动生成?
谢谢.
在Python中,我可以像这样拼接字符串:
solo = A quick brown fox jump over the lazy dog
solo[3:5]
Run Code Online (Sandbox Code Playgroud)
我知道substr并且comb已经足够了,我想知道它是否可行,但是.我应该使用角色来做到这一点吗?
我编写生成"随机"文本文件的程序,其中3个单词替换为存储在$keysfilename文件中的键.Keys文件结构非常简单,就像
ASD123ASD
QWE123QWE
XZC123ZXC
Run Code Online (Sandbox Code Playgroud)
例如,当我使用多个线程时会出现问题
my @threads = (^32).map({
Run Code Online (Sandbox Code Playgroud)
如果在任意文件上失败,则会出错
started
Thread<17>(14) got 1
Thread<18>(15) got 2
Thread<20>(17) got 17
Thread<5>(2) got 3
Thread<16>(13) got 4
Thread<21>(18) got 5
Thread<3>(0) got 6
Thread<8>(5) got 7
Thread<12>(9) got 10
Thread<11>(8) got 8
Thread<9>(6) got 9
Thread<14>(11) got 11
Thread<15>(12) got 12
Unhandled exception: Failed to open file C:\c\perltests\00000017.txt: no such file or directory
Thread<10>(7) got 13
Thread<13>(10) got 14
Thread<7>(4) got 15
Thread<19>(16) got 16
Thread<4>(1) got 0
Thread<6>(3) …Run Code Online (Sandbox Code Playgroud) > so (Any)
False
Run Code Online (Sandbox Code Playgroud)
但
> so [1, Any]
True
Run Code Online (Sandbox Code Playgroud)
如何制作False?
UPD:这似乎有效,但我不确定这是正确的方法.
> so [1, Any].all
False
Run Code Online (Sandbox Code Playgroud) 我的数据结构非常大,需要分配和传递.我还需要进入清单.有时当列表位于标量容器中时,$aList.elems会说1因为列表中只有一个元素.要进入列表,(@$aList).elems将在列表中提供正确数量的元素.
我的问题是:有没有性能劣势使用@$aList频繁,如果有性能问题,将分配@b = @$aList和使用@b,而不是解决问题?也就是说,从列表切换到数组上下文会有加速吗?
谢谢.
这是我的测试程序:
use Readline;
shell 'clear';
my $r = Readline.new;
loop {
my $a = $r.readline("> ");
{say ''; last} if not defined $a;
$r.add-history( $a );
say $a;
}
Run Code Online (Sandbox Code Playgroud)
输入任何字符串后,它将退出并显示以下消息:
> abc
Internal error: unhandled encoding
in method CALL-ME at /opt/rakudo-pkg/share/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 (NativeCall) line 587
in method readline at /home/evb/.perl6/sources/D8BAC826F02BBAA2CCDEFC8B60D90C2AF8713C3F (Readline) line 1391
in block <unit> at abc.p6 line 7
Run Code Online (Sandbox Code Playgroud)
如果我评论该行shell 'clear';,一切都OK.