2

I have inherited functions of the form:

sub func($$) {

}

I am more used to seeing:

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

I looked up $$ and it is a means to get the current Process ID. But looking at the function it doesn't look like the Process ID is used here. so does $$ mean something else in this context?

The function I am puzzled about is parseMessage below. Why the ($$)?

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;
i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
Angus Comber
  • 9,316
  • 14
  • 59
  • 107

1 Answers1

3

They are prototypes and define what the function takes as arguments (not safely..).

It allows you do define functions like the built in functions so you could call your sub doSomething like you call print.

doSomething($scalar) or doSomething $scalar would produce the same result, like print($scalar) vs. print $scalar

As per comments above: http://perldoc.perl.org/perlsub.html#Prototypes

Zach Leighton
  • 1,939
  • 14
  • 24
  • 1
    `print` is probably not a very good example, since it has no special argument parsing, except for the fact that `print` without arguments works like `print $_`. (If you really want that for your own function, you can achieve it with a prototype of `(_@)`.) Something like emulating `push` would be a more interesting example (and, in fact, that's what the perlsub section you linked to uses). – Ilmari Karonen Oct 22 '14 at 15:00
  • @IlmariKaronen you are probably right but it was the first to come to mind, didn't want to overcomplicate with map/grep/array functions – Zach Leighton Oct 22 '14 at 15:26
  • 1
    Actually `print` has pretty special argument parsing because of the `print $fh @list` and `print {$fh} @list` forms, and this cannot be emulated with prototypes. [See also this answer](http://stackoverflow.com/questions/26368555/subroutines-that-take-an-optional-block-parameter). – tobyink Oct 23 '14 at 12:22
  • @tobyink: That's just [indirect object syntax](http://perldoc.perl.org/perlobj.html#Indirect-Object-Syntax), though. – Ilmari Karonen Oct 09 '15 at 12:49