我继承了以下形式的函数:
sub func($$) {
}
我更习惯看到:
sub func {
## then extract params using shift for example
}
我查了$$
,这是获取当前进程ID的一种方法。但是看看这个函数看起来并不像这里使用的是进程ID。那么$$
在这种情况下是什么意思呢?
我困惑的功能是下面的parseMessage。为什么($$)
?
use FileHandle;
# The structure of function is pretty much like this - names changed only
sub parseMessage($$)
{
my $string = shift;
my $fileHandle = shift;
my $Message = undef;
# parseAMessage and parseBMessage are functions to extract specific types of messages from file
if ( ($Message = parseAMessage($string, $fileHandle))
|| ($Message = parseBMessage($string, $fileHandle)) )
{
}
return $Message;
}
sub parseAMessage($$)
{
}
sub parseBMessage($$)
{
}
# The function seems to use arguments arg1: string from file, arg2: filehandle of file
# presumably idea behind this is to process current line in file but also have access to file
# handle to move to next line where required. So the way I am calling this is probably not
# great Perl - I am a beginner perler
$fh = FileHandle->new;
if ($fh->open("< myfile.log")) {
# here we evaluate the file handle in a scalar context to get next line
while($line = <$fh>) {
parseMessage($line, $fh);
#print <$fh>;
}
$fh->close;
}
print "DONE\n";
1;
答案 0 :(得分:3)
它们是原型并定义函数作为参数的内容(不安全..)。
它允许您定义内置函数之类的函数,因此您可以像调用sub doSomething
一样调用print
。
doSomething($scalar)
或doSomething $scalar
会产生相同的结果,例如print($scalar)
与print $scalar