# [PATCH] gitweb: Uniquify usage of subroutine prototypes

3 messages from 2008-03-25 to 2008-03-25. Participants: Jakub Narebski, Junio C Hamano, Rafael Garcia-Suarez.
Thread: https://gitlist.dev/t/12854

## Jakub Narebski, 2008-03-25 12:11

Subject: [PATCH] gitweb: Uniquify usage of subroutine prototypes
Message-ID: <1206447114-29349-1-git-send-email-jnareb@gmail.com>
URL: https://gitlist.dev/e/1206447114-29349-1-git-send-email-jnareb%40gmail.com

```
The idea is NOT to use subroutine prototypes to create new syntax;
prototypes are to be purely informational and optional.  Subroutine
prototypes are meant in gitweb to mark untypical parameters, like
having hash as an argument, or using hash for last parameter to pass
extra options to subroutine.

(Truth to be told this change was caused by the fact that CPerl mode,
Perl mode for GNU Emacs, got confused in presence of "sub name($) {"
prototype...)

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
 gitweb/gitweb.perl |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index ec73cb1..ee5cbd6 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -587,7 +587,7 @@ exit;
 ## ======================================================================
 ## action links
 
-sub href(%) {
+sub href (%) {
 	my %params = @_;
 	# default is to use -absolute url() i.e. $my_uri
 	my $href = $params{-full} ? $my_url : $my_uri;
@@ -740,7 +740,7 @@ sub esc_html ($;%) {
 }
 
 # quote control characters and escape filename to HTML
-sub esc_path {
+sub esc_path ($;%) {
 	my $str = shift;
 	my %opts = @_;
 
@@ -754,7 +754,7 @@ sub esc_path {
 }
 
 # Make control characters "printable", using character escape codes (CEC)
-sub quot_cec {
+sub quot_cec ($;%) {
 	my $cntrl = shift;
 	my %opts = @_;
 	my %es = ( # character escape codes, aka escape sequences
@@ -780,7 +780,7 @@ sub quot_cec {
 
 # Alternatively use unicode control pictures codepoints,
 # Unicode "printable representation" (PR)
-sub quot_upr {
+sub quot_upr ($;%) {
 	my $cntrl = shift;
 	my %opts = @_;
 
@@ -982,7 +982,7 @@ use constant {
 };
 
 # submodule/subproject, a commit object reference
-sub S_ISGITLINK($) {
+sub S_ISGITLINK {
 	my $mode = shift;
 
 	return (($mode & S_IFMT) == S_IFGITLINK)
-- 
1.5.4.4

```

## Junio C Hamano, 2008-03-25 19:47

Subject: Re: [PATCH] gitweb: Uniquify usage of subroutine prototypes
Message-ID: <7vlk46lh61.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vlk46lh61.fsf%40gitster.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.

```

## Rafael Garcia-Suarez, 2008-03-25 22:06

Subject: Re: [PATCH] gitweb: Uniquify usage of subroutine prototypes
Message-ID: <b77c1dce0803251506y443aab5uf0134a47e9cf28de@mail.gmail.com>
URL: https://gitlist.dev/e/b77c1dce0803251506y443aab5uf0134a47e9cf28de%40mail.gmail.com
In-Reply-To: <7vlk46lh61.fsf@gitster.siamese.dyndns.org>

```
On 25/03/2008, Junio C Hamano <junio@pobox.com> wrote:
>  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".

I would agree with what Junio says here. The primary motivation of
prototypes was to emulate the syntax of perl built-ins, and their
extension (with the _ character) in Perl 5.10 has followed this
general principle. As a general rule, I tend to avoid using prototypes
for 'normal' functions. That's mostly an application of the least
surprise principle.

```
