我有重构的代码,它看起来像这样
@month_dirs = <$log_directory/*>;
我更改了$log_directory以加载配置文件(确切地说是AppConfig).现在输出$conf这是一个AppConfig对象而不是导出$log_directory.要访问加载的变量,通常会对变量名称进行方法调用,所以我尝试了…
@month_dirs = <$conf->log_directory()."/*">
这失败了,因为我无法在预期有一个bar字的位置调用$conf-> log_directory.只是在玩,我尝试了这个
$month_directory_command = $conf->log_directory()."/*"; @month_dirs = <$month_directory_command>;
这仍然是失败的,没有任何迹象表明这是一个问题.我尝试直接在钻石中使用一个字符串,但它失败了,显然只有裸字,而不是字符串,被钻石接受我很惊讶,因为我根本不允许使用字符串,我认为大多数地方Barewords可以是否可以使用字符串,这只是因为大多数代码实现单独的逻辑来接受裸字与字符串,但不需要以这种方式实现?
我可以通过完全模仿原始语法来完成这项工作
$month_directory_command = $conf->log_directory(); @month_dirs = <$month_directory_command/*>;
但是,这对我来说很难看.我也很困惑为什么我能做到这一点,但我不能创造一个简单的词:
$bare_word = $conf->log_directory()/*
要么
$month_directory_command = $conf->log_directory(); $bare_word = $month_directory_command/*; @month_dirs = <$bare_word>;
为什么有些变量适用于裸言而非其他?为什么我可以使用缩放器变量,但如果从方法调用返回它?
我尝试在裸字上查找perl语法,但没有太多运气描述它们不是直接编写的情况,而是由变量组成.
我希望有人可以帮助我更好地理解这里的裸字语法.什么时候我可以使用变量作为一个单词的一部分,如果我可以将它保存为变量?
如果可以建议的话,我想弄清楚在我的钻石运算符中使用barword的更简洁的语法,但更多的是我想了解语法,所以我知道将来如何处理裸字.我保证我确实试过提前打算这个,但没有太多运气.
顺便说一下,似乎建议不要在perl中使用barewords?在某种程度上我应该避免钻石运算符的裸字吗?
解决方法
$perl -E'say for <"/*">' /bin /boot /dev ...
(事实上,一个裸字只是一个没有印记的标识符,并且被严格的’subs’禁止;所以你的例子都没有真正的资格.)
这个:
@month_dirs = <$log_directory/*>;
因为在<>内部完成了双引号内插级别,并且内插了诸如$log_directory之类的标量变量.
它相当于:
@month_dirs = glob("$log_directory/*");
这个:
@month_dirs = <$conf->log_directory()."/*">
因为>而失败在$conf-> log_directory()过早地关闭菱形运算符,使解析器混乱.
它被解析为:
<$conf->
(对glob的调用)接着是
log_directory()."/*">
这是一个语法错误.
这个:
$month_directory_command = $conf->log_directory()."/*"; @month_dirs = <$month_directory_command>;
因为失败了
<$month_directory_command>
相当于
readline($month_directory_command)
而不是
glob("$month_directory_command")
从perldoc perlop
开始:
If what the angle brackets contain is a simple scalar variable (for example,
$foo
),then that variable contains the name of the filehandle to input from,or its typeglob,or a reference to the same.[…]
If what’s within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name,typeglob,or typeglob reference,it is interpreted as a filename pattern to be globbed,and either a list of filenames or the next filename in the list is returned,depending on context. This distinction is determined on syntactic grounds alone. That means
<$x>
is always areadline()
from an indirect handle,but<$hash{key}>
is always aglob()
.
所以你试图从尚未打开的文件句柄($month_directory_command)中读取.
使用警告“全部”打开警告;会提醒你这个:
readline() on unopened filehandle at foo line 6.
这个:
$bare_word = $conf->log_directory()/*;
失败是因为您尝试将方法调用的结果与非带引号的字符串连接起来;要连接字符串,您必须将它们插入到双引号字符串中,或使用连接运算符.
你可以这样做:
$bare_word = $conf->log_directory() . "/*"; @month_dirs = <"$bare_word">;
(虽然$bare_word根本不是一个单词,但它是一个标量变量.)
注意:
@month_dirs = <$bare_word>;
(不带引号)将被解释为readline,而不是glob,如上面的perlop所述.
但是,一般来说,直接使用glob运算符可能不那么令人困惑:
@month_dirs = glob( $conf->log_directory() . "/*" );