threads / patch / 5613

patchgitweb: Fix @git_base_url_list usage

Subject: [PATCH] gitweb: Fix @git_base_url_list usage

## tl;dr

5 messages between Sep 20, 2006 and Sep 22, 2006. Diffs are folded; open one to read it.

replies: 4people: 2as markdown or json

Petr Baudis· Sep 20, 2006, 00:09 UTC · lore

As it is now, that array was never used because the customurl accessor was broken and ''unless @url_list'' never happenned.

Signed-off-by: Petr Baudis <pasky@suse.cz>
---
 gitweb/gitweb.perl |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to gitweb/gitweb.perl +1 −1
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 2d0d56f..1b029ac 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -731,7 +731,7 @@ sub git_get_project_description {
 sub git_get_project_url_list {
 	my $path = shift;
 
-	open my $fd, "$projectroot/$path/cloneurl" or return undef;
+	open my $fd, "$projectroot/$path/cloneurl" or return wantarray ? () : undef;
 	my @git_project_url_list = map { chomp; $_ } <$fd>;
 	close $fd;
 
Junio C Hamano· Sep 20, 2006, 00:28 UTC · re: Petr Baudis · lore

Re: [PATCH] gitweb: Fix @git_base_url_list usage

Petr Baudis <pasky@suse.cz> writes:
Show 13 quoted lines
> As it is now, that array was never used because the customurl accessor was
> broken and ''unless @url_list'' never happenned.
>
> Signed-off-by: Petr Baudis <pasky@suse.cz>
> ---
>...
>  sub git_get_project_url_list {
>  	my $path = shift;
>  
> -	open my $fd, "$projectroot/$path/cloneurl" or return undef;
> +	open my $fd, "$projectroot/$path/cloneurl" or return wantarray ? () : undef;
>  	my @git_project_url_list = map { chomp; $_ } <$fd>;
>  	close $fd;

Why on earth do you want to use wantarray for something like this?

It's not like you are implementinging any fancy DWIM magic.
Isn't
	open my $fd, "foobar" or return;
much easier to read?
#!/usr/bin/perl
sub return_undef {
	open my $fd, "no-such-file"
	    or return wantarray ? () : undef;
}
sub return_broken {
	open my $fd, "no-such-file"
	    or return undef;
}
sub return_empty {
	open my $fd, "no-such-file"
	    or return;
}

my $returned_undef_1 = return_undef(); my ($returned_undef_2) = return_undef(); my @returned_undef = return_undef();

my $returned_broken_1 = return_broken(); my ($returned_broken_2) = return_broken(); my @returned_broken = return_broken();

my $returned_empty_1 = return_empty(); my ($returned_empty_2) = return_empty(); my @returned_empty = return_empty();

printf "U %d B %d E %d\n",
    scalar(@returned_undef),
    scalar(@returned_broken),
    scalar(@returned_empty);
for ($returned_undef_1,
     $returned_undef_2,
     $returned_broken_1,
     $returned_broken_2,
     $returned_empty_1,
     $returned_empty_2) {
	print "What I said was bogus.\n" if (defined $_);
}
Junio C Hamano· Sep 20, 2006, 00:43 UTC · re: Junio C Hamano · lore

Re: [PATCH] gitweb: Fix @git_base_url_list usage

Junio C Hamano <junkio@cox.net> writes:
> Why on earth do you want to use wantarray for something like
> this?
>
> It's not like you are implementinging any fancy DWIM magic.
Side note.  I really hate it when people abuse wantarray.

I can think of only two valid reasons (well, perhaps three but the last one is a logical consequence of the first two) to use wantarray. They are:

 (1) The information you are giving back the caller is list of
     things in nature, but there is a natural representation for
     that list as a single scalar, and that representation is
     not the last element of the list.  You would want to help
     the caller by DWIMmery.
     Perl's built-in localtime() is an excellent example for
     wanting to do something like this.  It returns broken-down
     "struct tm" in list context, but returns string ctime(3) in
     scalar context.
 (2) You want to avoid complex computations when the caller does
     not need full return values from you.  A good example is
     found in perlfunc.pod:
        return unless defined wantarray;	# don't bother doing more
        my @a = complex_calculation();
        return wantarray ? @a : "@a";
 (3) You are emulating Perl's built-in function that DWIMs for
     one of the above reasons, usually (1).
Petr Baudis· Sep 20, 2006, 00:49 UTC · re: Junio C Hamano · lore

Re: [PATCH] gitweb: Fix @git_base_url_list usage

Dear diary, on Wed, Sep 20, 2006 at 02:28:28AM CEST, I got a letter where Junio C Hamano <junkio@cox.net> said that...

Show 26 quoted lines
> Petr Baudis <pasky@suse.cz> writes:
> 
> > As it is now, that array was never used because the customurl accessor was
> > broken and ''unless @url_list'' never happenned.
> >
> > Signed-off-by: Petr Baudis <pasky@suse.cz>
> > ---
> >...
> >  sub git_get_project_url_list {
> >  	my $path = shift;
> >  
> > -	open my $fd, "$projectroot/$path/cloneurl" or return undef;
> > +	open my $fd, "$projectroot/$path/cloneurl" or return wantarray ? () : undef;
> >  	my @git_project_url_list = map { chomp; $_ } <$fd>;
> >  	close $fd;
> 
> Why on earth do you want to use wantarray for something like
> this?
> 
> It's not like you are implementinging any fancy DWIM magic.
> 
> Isn't
> 
> 	open my $fd, "foobar" or return;
> 
> much easier to read?

Sure, that's what I meant, I'm only already a bit tired so I just mimicked the other return in that sub, sorry.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
Snow falling on Perl. White noise covering line noise.
Hides all the bugs too. -- J. Putnam
Petr Baudis· Sep 22, 2006, 23:15 UTC · re: Petr Baudis · lore

As it is now, that array was never used because the customurl accessor was broken and ''unless @url_list'' never happenned. ---

 gitweb/gitweb.perl |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to gitweb/gitweb.perl +1 −1
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index f7e7d10..a357604 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -752,7 +752,7 @@ sub git_get_project_description {
 sub git_get_project_url_list {
 	my $path = shift;
 
-	open my $fd, "$projectroot/$path/cloneurl" or return undef;
+	open my $fd, "$projectroot/$path/cloneurl" or return;
 	my @git_project_url_list = map { chomp; $_ } <$fd>;
 	close $fd;
 

← back to recent threads