git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] gitweb: Uniquify usage of subroutine prototypes

From
Junio C Hamano <junio@pobox.com>
Date
Mar 25, 2008, 19:47 UTC
Message-ID
<7vlk46lh61.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<1206447114-29349-1-git-send-email-jnareb@gmail.com>
Junio C Hamano <gitster@pobox.com> writes:
> Jakub Narebski <jnareb@gmail.com> writes:
>
>> The idea is NOT to use subroutine prototypes to create new syntax;
>> prototypes are to be purely informational and optional.
That's unfortunately a grave misconception, isn't it?

For example, can you explain (1) how these three calls behave before running them, and (2) why these three behave the way they do?

        sub foo       { my ($s, %f) = @_; print "s = $s\n"; }
        sub bar ($;%) { my ($s, %f) = @_; print "s = $s\n"; }
        my @it = ('This is my string');
        my %hash = (rose => 'blue', violet => 'green');
        foo @it, %hash;		# call 1
        bar @it, %hash;		# call 2
	bar $it[0], %hash;	# call 3

By adding ($;%) to an existing function that did not have prototype, you changed the semantics of the function and:

 (1) it is your responsibility to make sure you did not break existing
     callers when you made such a change, and
 (2) programmers who want to call any existing function in your program
     need to check how the function groks its parameters and make sure
     they do not fall into the same pitfalls as the call sites you had to
     fix in step (1).  They cannot rely on the old fashioned "arguments
     are passed as a flattened list" idiom anymore before checking if you
     have prototypes to the function they want to call.

Prototypes used carelessly tend to force users to do unnecessary things, because the caller cannot rely on the old fashioned "arguments are passed as a flattened list" semantics and check how each and every function is prototyped before making a call.

I am not saying that Perl prototypes is a bad thing. The point of the prototype is to change the syntax and semantics so that you can write a function to which arguments are _not_ passed as a flattend list, and without them you cannot write something that emulates "push @a, $b, $c".

But you need to be aware of what it does to your callers.
Previous: Jakub NarebskiNext: Rafael Garcia-Suarez
Message 2 of 3 in “gitweb: Uniquify usage of subroutine prototypes”
  1. gitweb: Uniquify usage of subroutine prototypesJakub Narebski, Mar 25, 2008
  2. Junio C HamanoMar 25, 2008
  3. Rafael Garcia-SuarezMar 25, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.