简体   繁体   English

Concat正则表达式与常量声明中的字符串作为oneliner

[英]Concat regex with a string in a constant declaration as oneliner

I am trying to declare a constant as an one liner like this: 我试图将常量声明为像这样的一个衬里:

use constant HOME_SCRIPT => "/home/george/". $0 =~ /(.*)\\.pl/;
The problem is that this returns: /home/george/1 . 问题是这会返回: /home/george/1 Ie it concats the number of matches of regex. 即它汇总了正则表达式的匹配数。
I tried: 我试过了:
use constant HOME_SCRIPT => ("/home/george/"). $0 =~ /(.*)\\.pl/;
use constant HOME_SCRIPT => "/home/george/". ($0 =~ /(.*)\\.pl/);
but same result. 但结果相同。
Is it possible to create an oneliner for this? 是否可以为此创建一个oneliner?

use constant HOME_SCRIPT => "/home/george/". ($0 =~ /(.*)\.pl/)[0];

要么

use constant HOME_SCRIPT => join "", "/home/george/", $0 =~ /(.*)\.pl/;

If you are running Perl 5 version 14 or later, you can use non-destructive substitution via the /r modifier. 如果您运行的是Perl 5版本14或更高版本,则可以通过/r修饰符使用非破坏性替换。 Like this 像这样

use constant HOME_SCRIPT => ("/home/george/$0") =~ s/\.pl\z//r;

Or if you are forced to use something earlier, or would simply prefer a different method, you can write 或者如果您被迫先前使用某些东西,或者只是喜欢不同的方法,那么您可以写作

use constant HOME_SCRIPT => '/home/george/' . ($0 =~ /(.*)\./, $1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM