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
).
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 .
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.
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.
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 {}.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With