Hacker News new | ask | show | jobs
by ninkendo 2513 days ago
This post reminded me that Perl doesn’t even have a concept of a method signature, even so much as showing the number of arguments. You have to look at the next few lines to hopefully see all the `... = shift;` lines to see what you’re actually supposed to send.

I mean, a lack of type annotations is normal for a scripting language, but Perl always seemed to go a bit far by not even having a standard way of showing parameter names.

I remember doing `my ($arg1, $arg2) = @_;` or something similar, but even that felt weird, and not everyone adopted that convention.

2 comments

I usually do

  sub mymethod($$)
    my ($arg1, $arg2) = @_;
  }
Apart from on the smallest programs. So it does have a method signature (obv it's an untyped language)

There are shortcuts (just like you could write "doSometing() unless $var;"). Perl does give you options.

Python has different options for passing methods - passing variables, passing a dict,

P.S. while looking at python method declartions, you can send variables, a key-value, multiple keyvalue, and then there's this: https://stackoverflow.com/a/16785702

Which comes out with

  f = lambda **dic: ' '.join(dic.get(key, 'None') for key in 'abc')

Which is no more or less readable than the most confusing perl I've ever encountered.

"Cool Shortcuts" are a problem in any language. There are values in anonymous functions on occasion (especially in sorts), there should be a very compelling reason to do so.

> sub mymethod($$)

That does not work. Prototypes are ignored for method calls. You are expected to not abuse prototypes for signatures as they serve a different purpose.

> it's an untyped language

That's wrong even with the most lenient/charitable interpretation. Using an unexpected type of data is a fatal error.

    perl -e'$x = {}; $x->[0]; print "survived"'
    Not an ARRAY reference at -e line 1.
Type constraints have existed for a very long time, for example see http://p3rl.org/Kavorka::Manual::Signatures#Type-constraints
... but it does, since perl 5.20, which was released May 27, 2014. More than five years ago.

Here's a short example:

    use 5.020_000;
    use warnings;
    use experimental qw<signatures>;
    # this works:
    say add(2, 3);
    # this dies: Too many arguments for subroutine 'main::add' at ...
    say add(2, 3, 4);
    sub add ($x, $y) {
        $x + $y;
    }