Hacker News new | ask | show | jobs
by duskwuff 1379 days ago
Signatures were added in 5.020, and [edited:] were considered experimental until 5.036.
4 comments

> and are still considered experimental

No, 5.36 stabilised them.

https://perldoc.perl.org/perldelta#Core-Enhancements

> The 5.36 bundle enables the signatures feature. Introduced in Perl version 5.20.0, and modified several times since, the subroutine signatures feature is now no longer considered experimental. It is now considered a stable language feature and no longer prints a warning.

Oh, that's a bummer. I was kind of excited to use those, but since I never know when I'm going to get a call from the Centos 7 guys I have to stay away from features newer than 5.016. Basically just new enough to get semi-sane UTF support.
Ah! Looks like I need to upgrade, then -- I was reading from "perldoc feature" in my install, which was still on 5.34.
Thanks for the correction. I don't use them anymore (instead doing a few lines of sanity-check boilerplate in each sub) so that's why I made that mistake.
are Perl function signatures the PHP/Python equivalent of type hints?
This may shock you, but: historically, Perl functions did not have declared arguments. Like, at all. The function just got arguments implicitly passed to it as an array (@_), and parsing that into individual variables was up to the function -- so most functions would start like this:

    sub myFunction {
        my ($self, $arg1, $arg2) = @_;
        ...
    }
Or, in older code, you might even see this:

    sub myFunction {
        my $self = shift; # implicitly get the first value from @_
        my $arg1 = shift;
        my $arg2 = shift;
        ...
    }
Notably, this didn't even check the number of arguments you passed. If you passed too many or too few arguments to a function which worked this way, the extra arguments would silently be ignored, or missing arguments would silently show up as undef.

Function signatures turn this into something that'll look much more familiar to users of other languages:

    sub myFunction ($self, $arg1, $arg2) {
        ...
    }
which includes checks to make sure that all the expected arguments (and no more) were passed.
> If you passed too many or too few arguments to a function which worked this way, the extra arguments would silently be ignored, or missing arguments would silently show up as undef.

This part probably won't shock anyone given the prevalence of JS today. Which is kinda sad, given that it's 35 years since Perl was first a thing.

Lol there were places, perhaps times, where Perl subroutines would "shift" as they go along, to create a prose effect. Sometimes you'd get a comment about it, but mostly not. Maddening for any longer function.
Anyone remember smart match? Oh dear - a real low point for Perl.