Perl函数($$)表单有什么特别之处?

安格斯·康伯

我继承了以下形式的功能:

sub func($$) {

}

我更习惯看到:

sub func {
  ## then extract params using shift for example
}

我抬头看$$,这是获取当前进程ID的一种方法。但是,从功能上看,这里似乎没有使用Process 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;
扎克·莱顿(Zach Leighton)

它们是原型,并定义函数将其用作参数的方式(不安全。)。

它允许您定义诸如内置函数之类的函数,因此您可以sub doSomething像调用那样调用自己的函数print

doSomething($scalar)doSomething $scalar会产生相同的结果,例如print($scalar)vs。print $scalar

按照上面的评论:http : //perldoc.perl.org/perlsub.html#Prototypes

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章