Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

RFC: Simulating Signatures

by LanX (Bishop)
on Apr 22, 2013 at 17:07 UTC ( #1029930=note: print w/replies, xml ) Need Help??

in reply to RFC: Simulating Ruby's "yield" and "blocks" in Perl

(I wanted to put this discussion into a separate post, think it will stir much controversy)

> argument signatures are lengthy in Perl

With the same technique like demonstrated in yield() one could simulate a syntac sugar function sig(), which would assign arguments from the upper caller  sig( my($a,$b) ). sig could easily handle a lot more like:

  • setting default values,
  • check of obligatory parameters,
  • handling named parameters,
  • checking types
  • automatic creation of API documentation.
  • allowing docstrings

But the need to declare each variable to be lexical is cumbersome, who really wants to constantly repeat my ?

sub test { sig my $x='default', my $y, named => my $named, OPT; ... code ... }

so having a new keyword or syntax would facilitate things a lot

something like

sub test { mine $x='default', $y, named => $named, @OPT;
... }

or even better

sub test { | $x='default', $y, named => $named, @OPT |; ... }

while I'm not sure if it's possible to use | w/o dangerous ambiguity.

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re: RFC: Simulating Signatures
by curiousmonk (Sexton) on Apr 23, 2013 at 03:43 UTC

    Just asking a small question here...

    How is

    sub test { sig my $x, my $y , \@opt; }

    Any better than

    sub test { my ( $x , $y ) = @_; }

    Don't get me wrong, I understand you add a few extra features to the first thing. But the user will have to learn/do extra syntax, without getting regular signature syntax like other C languages have. Besides these don't offer signatures in the true sense.

      > Besides these don't offer signatures in the true sense.

      I suppose with true signatures you mean something like test($x, $y , @opt).

      This can only be added by modifying the parser, something far beyond syntactic sugar.

      I heard that p5p recently discussed it but with no real outcome (plz prove me wrong).

      The essential problem with defining signatures is that the variables (normally) need to be lexicals.

      > I understand you add a few extra features to the first thing.

      But that's the point. I recently gave a longer workshop for my client about how to do parameter check in Perl. This really results in much code... and many people where either frustrated or bored.

      Many extra features within an easy syntax do motivate people! And maybe one day these extra features could be reused by a "true" implementation of signatures.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029930]
[shmem]: just one proverb before dozing off: when dealing with a bear, hubris is suicidal
[shmem]: Old_Gray_Bear would nod, and go back to sleep, then - but he's been sleeping for a year now.
[erix]: "AngloZionist Empire"? "Stop the Empire's war on Russia?" WTF?
erix stops reading as recommended by the blog itself
[erix]: my attention being jealously guarded ;)
[shmem]: erix, yes, it's a WTF. Since...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2018-03-19 20:49 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.