<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Peter Martini</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/peter_martini/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/peter_martini/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/peter_martini//1533</id>
    <updated>2012-09-19T01:52:06Z</updated>
    <subtitle>A blog about the Perl programming language</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>Subroutine Signatures - my Plan (v.1)</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/peter_martini/2012/09/subroutine-signatures---the-plan-v1.html" />
    <id>tag:blogs.perl.org,2012:/users/peter_martini//1533.3850</id>

    <published>2012-09-18T23:40:09Z</published>
    <updated>2012-09-19T01:52:06Z</updated>

    <summary>There has been quite a lot of discussion on p5p about subroutine signatures, so I figured I&apos;d lay out my current vision here with as much details as I can. As more of this gets hashed out on p5p, I...</summary>
    <author>
        <name>Peter Martini</name>
        
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/peter_martini/">
        <![CDATA[<p>There has been quite a lot of discussion on p5p about subroutine signatures, so I figured I'd lay out my current vision here with as much details as I can.  As more of this gets hashed out on p5p, I expect I'll add notes at the bottom (can I do that?) pointing to a blog entry for Subroutine Signatures - my Plan (v.2), etc.</p>

<p>All of this is what I'm working on, but I don't have a commit bit, so it's not going anywhere without getting thoroughly vetted and blessed first.</p>

<p>I'm posting my code at http://github.com/PeterMartini/perl, in the peter/signatures branch (meant to be kept in tandem with doy/subroutine-signatures).</p>

<p>GOAL:</p>

<p>Part 1:</p>

<p>In the scope of use feature "signatures" (or whatever)</p>

<p>sub foo($bar,$baz) {}<br />
  equivalent to sub foo { my ($bar, $baz) = @_; }<br />
sub foo($bar,@baz) {}<br />
  equivalent to sub foo { my ($bar, @baz) = @_;}<br />
sub foo($bar,%baz) {}<br />
  equivalent to sub foo { my ($bar, %baz) = @_;}</p>

<p>Part 2:</p>

<p>Add a *new* way to access this information.  This does not replace prototype, and is not meant to.  I'd vote for 'signature', either as a keyword or in some namespace (Scalar::Util?)</p>

<p>I haven't thought this one through yet, but the big concern will be making sure it doesn't block future growth.</p>

<p>Part 3:</p>

<p>For backwards compatability, add a proto attribute to allow for the old behavior.</p>

<p>sub foo($bar,$baz) : proto($$) {}<br />
  equivalent to sub foo($$) { my ($bar,$baz) = @_;}</p>

<p>I don't plan to protect this by a feature.</p>

<p>I don't care either way whether:</p>

<p>sub foo($$);<br />
sub foo($$) : proto($$){}</p>

<p>is legal or not, but will allow it as long as all of the prototypes match.</p>

<p>Note that:</p>

<p>sub foo($$);<br />
sub foo($bar,$baz) : proto($$) {}</p>

<p>MUST have a proto attribute, or it will die due to the prototype mismatch (prototype would be none)</p>

<p>Part 4:</p>

<p>In the check phase of compilation, implicitly convert:</p>

<p>sub foo { my ($bar, $baz) = @_;}</p>

<p>to (internally) sub foo($bar,$baz){}</p>

<p>If and only if the assignment is the first statement of the sub, the named variables are the first entries in the PAD (for technical reasons) and @_ is not modified.</p>

<p>SELECTED POINTS OF CONTENTION:</p>

<p>1. Q: What is the value, if its just saving keystrokes?<br />
    Possible A: It's wanted often enough that several CPAN variants exist<br />
    Possible A: It *may* improve performance<br />
    Possible A: It adds another method of introspection, allowing the code to formally declare its own parameters.</p>

<p>2. Q: Will this prevent custom CPAN modules from implementing their own syntax?<br />
    My A: No. The signature will be controlled by a feature, which means if a 3rd party module is used, it can simple shut off the built in signature.</p>

<p>3. Q: Should a named parameter list *be* the prototype (accessible through prototype(\&sub), though not necessarily affecting parsing)?<br />
    My A: For backwards compatibility reasons, no.  The current prototype is a simple string; I think it may make much more sense to expose the signature information as a string an array of hashes (depending on wantarray / GIMME), to allow room for growth.</p>

<p>4. Q: Do we really want to create lexical variables without an explicit ‘my’?<br />
    My A: I originally allowed for an explicit my, but there seemed to be no other reasonable options so I'd dropped it as redundant.</p>

<p>5. Q: What about @_?<br />
    My A: At this stage, I have no intention of touching it.  It will continue to be available for read-write access, and it is necessary if a sub wants to check the count of arguments it was passed (sub foo($bar) would have no way of seeing the second, third, etc argument)</p>

<p>I'm sure I missed plenty of questions and answers - I'll add them either here or in future posts as I spot them.</p>]]>
        
    </content>
</entry>

</feed>
