develooper Front page | perl.perl5.porters | Postings from March 2020

Subroutine signatures validation.

Thread Next
From:
Todd Rinaldo
Date:
March 17, 2020 17:31
Subject:
Subroutine signatures validation.
Message ID:
E3A9131C-AD54-493F-8D56-A1A1895AA0F7@cpanel.net
We have started drinking the subroutine signatures kool-aid at cPanel. It has been a very enjoyable experience to change out our boiler plate code to signatures. Thank you so much to everyone who worked hard to implement it!

What has become obvious as we convert existing code is this sort of example:

sub foo {
    my $var = shift or die;
    ...;
}

Which then has to be converted to:

sub foo ($var) {
    $var or die;
    ...;
}

We think it would be VERY useful if signatures were enhanced to do this additionally.

sub verify_defined ($var)  { 
    return defined $var;
}

sub foo( $x :verify_defined) {
    ...;
}

foo(undef); # line 8
exit;

$>perl foo.pl
die: foo doesn't pass validation for $var argument at line 8

This could be further enhanced to pass variables on the end of the verifier and you would get things like isa for free:

sub verify_defined ($var)  { 
    return defined $var;
}

sub foo( $x :verify_defined, $y :isa("MyClass") ) {
    ...;
}

Has this idea been proposed? Is this idea worth pursuing?

Thanks,
Todd

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About