Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do dollar, at-sign and semicolon characters in Perl parameter lists mean?

I have encountered a number of Perl scripts in the codebase at my job. Some of them contain subroutines with the following syntax oddity:

sub sum($$$) {
    my($a,$b,$m)=@_;
    for my $i (0..$m) {
        $$a[$i] += $$b[$i] if $$b[$i] > 0;
    }
}

sub gNode($$;$$) {
    my($n,$l,$s,$d) = @_;
    return (
            "Node name='$n' label='$l' descr='$d'" ,
            $s ? ("Shape type='$s' /") : (),
            '/Node'
        );
}

sub gOut($$@) {
    my $h = shift;
    my $i = shift;
    if ($i > 0) {
        print $h (('')x$i, map '<'.$_.'>', @_);
    } else {
        print $h map '<'.$_.'>', @_;
    }
}

Leaving aside the question of what these subroutines are meant to do (I'm not entirely sure myself...), what do the sequences of characters in the 'parameter list' position mean? Viz. the $$$, $$;$$ and $$@ sequences in these examples.

I have a very limited understanding of Perl, but I believe that the my($a,$b,$m)=@_; line in the first example (sum) unpacks the parameters passed to the subroutine into the $a, $b and $m local variables. This suggests that the $$$ indicates the arity and type signature of sum (it expects three scalars, in this case). This would potentially suggest that gOut expects two scalars and an array. Is this the correct interpretation?

Even if the above interpretation is correct, I'm lost as to the meaning of the semicolon in the second routine (gNode).

like image 812
Peter Avatar asked Aug 02 '13 00:08

Peter


People also ask

What is \S [ \F] in Perl?

Whitespace \s [ \f ]: The character class \s will match a single character i.e. a whitespace. It will also match the 5 characters i.e. -horizontal tab, -the newline, \f-the form feed, -the carriage return, and the space. In Perl v5.18, a new character to be introduced which is matches the \cK – vertical tab .

What are the special variables in Perl?

There are some variables which have a predefined and special meaning in Perl. They are the variables that use punctuation characters after the usual variable indicator ($, @, or %), such as $_ ( explained below ). Most of the special variables have an english like long name, e.g., Operating System Error variable $! can be written as $OS_ERROR.

What are the special character classes in Perl?

The Special Character Classes in Perl are as follows: Digit \d [0-9]: The \d is used to match any digit character and its equivalent to [0-9]. In the regex /\d/ will match a single digit. The \d is standardized to “digit”. The main advantage is that the user can easily write in shorter form and can easily read it.

What is a $a + $b statement in Perl?

The $a + $b is an expression that returns the sum of two variables: $a and $b. A statement is made up of expressions. A statement is executed by Perl at run-time. Each Perl statement must end with a semicolon (; ). The following example shows the statements in Perl: A block is made up of statements wrapped in curly braces {}.


1 Answers

See perldoc perlsub entry on Prototypes.

 # Declared as            Called as
 sub mylink ($$)        mylink $old, $new
 sub myvec ($$$)        myvec $var, $offset, 1
 sub myindex ($$;$)     myindex &getstring, "substr"
 sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off
 sub myreverse (@)      myreverse $a, $b, $c
 sub myjoin ($@)        myjoin ":", $a, $b, $c
 sub mypop (+)          mypop @array
 sub mysplice (+$$@)    mysplice @array, 0, 2, @pushme
 sub mykeys (+)         mykeys %{$hashref}
 sub myopen (*;$)       myopen HANDLE, $name
 sub mypipe (**)        mypipe READHANDLE, WRITEHANDLE
 sub mygrep (&@)        mygrep { /foo/ } $a, $b, $c
 sub myrand (;$)        myrand 42
 sub mytime ()          mytime

Don't forget: This is all very powerful, of course, and should be used only in moderation to make the world a better place.

like image 119
Sinan Ünür Avatar answered Oct 01 '22 01:10

Sinan Ünür