threads / discuss / 14828

[git/perl] unusual syntax?

Subject: [git/perl] unusual syntax?

## tl;dr

9 messages between Aug 4, 2008 and Aug 5, 2008.

replies: 8people: 5as markdown or json

Ray Chuan· Aug 4, 2008, 04:49 UTC · lore
Hi,
i noticed that this doesn't work for me (Perl 5.10):
sub _close_hash_and_insert_object {
	my ($self) = @_;
	return unless defined($self->{hash_object_pid});
	my @vars = map { 'hash_object_' . $_ } qw(pid in out ctx);
	command_close_bidi_pipe($self->{@vars});
	delete $self->{@vars};
}

$self->{@vars} evaluates to undef. i can't find any mention of using arrays to dereference objects in the manual and elsewhere; is this a mistake?

-- 
Cheers,
Ray Chuan
Abhijit Menon-Sen· Aug 4, 2008, 05:02 UTC · re: Ray Chuan · lore

[PATCH] Fix hash slice syntax error

Signed-off-by: Abhijit Menon-Sen <ams@toroid.org>
---
At 2008-08-04 12:49:27 +0800, rctay89@gmail.com wrote:
>
> $self->{@vars} evaluates to undef. i can't find any mention of using
> arrays to dereference objects in the manual and elsewhere; is this a
> mistake?

Yes, @vars would be interpreted in scalar context, which certainly isn't the intended effect.

-- ams
 perl/Git.pm |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index 087d3d0..2ef437f 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -839,8 +839,8 @@ sub _close_hash_and_insert_object {
 
 	my @vars = map { 'hash_object_' . $_ } qw(pid in out ctx);
 
-	command_close_bidi_pipe($self->{@vars});
-	delete $self->{@vars};
+	command_close_bidi_pipe(@$self{@vars});
+	delete @$self{@vars};
 }
 
 =item cat_blob ( SHA1, FILEHANDLE )
@@ -928,8 +928,8 @@ sub _close_cat_blob {
 
 	my @vars = map { 'cat_blob_' . $_ } qw(pid in out ctx);
 
-	command_close_bidi_pipe($self->{@vars});
-	delete $self->{@vars};
+	command_close_bidi_pipe(@$self{@vars});
+	delete @$self{@vars};
 }
 
 =back
-- 
1.6.0.rc0.43.g2aa74
Petr Baudis· Aug 4, 2008, 07:56 UTC · re: Ray Chuan · lore

[PATCH] Git.pm: Fix internal git_command_bidi_pipe() users

The hash_and_insert_object() and cat_blob() helpers were using an incorrect slice-from-ref Perl syntax. This patch fixes that up in the _close_*() helpers and make the _open_*() helpers use the same syntax for consistnecy.

Signed-off-by: Petr Baudis <pasky@suse.cz>
---
  Wow, the command_bidi_pipe API really is dirty. Of course, it is
my fault as anyone's since I didn't get around to review the patches
introducing it.
 perl/Git.pm |   16 ++++++----------
 1 files changed, 6 insertions(+), 10 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index 087d3d0..0624428 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -827,8 +827,7 @@ sub _open_hash_and_insert_object_if_needed {
 
 	return if defined($self->{hash_object_pid});
 
-	($self->{hash_object_pid}, $self->{hash_object_in},
-	 $self->{hash_object_out}, $self->{hash_object_ctx}) =
+	@$self{map { "hash_object_$_" } qw(pid in out ctx)} =
 		command_bidi_pipe(qw(hash-object -w --stdin-paths));
 }
 
@@ -837,9 +836,8 @@ sub _close_hash_and_insert_object {
 
 	return unless defined($self->{hash_object_pid});
 
-	my @vars = map { 'hash_object_' . $_ } qw(pid in out ctx);
-
-	command_close_bidi_pipe($self->{@vars});
+	my @vars = map { "hash_object_$_" } qw(pid in out ctx);
+	command_close_bidi_pipe(@$self{@vars});
 	delete $self->{@vars};
 }
 
@@ -916,8 +914,7 @@ sub _open_cat_blob_if_needed {
 
 	return if defined($self->{cat_blob_pid});
 
-	($self->{cat_blob_pid}, $self->{cat_blob_in},
-	 $self->{cat_blob_out}, $self->{cat_blob_ctx}) =
+	@$self{map { "cat_blob_$_" } qw(pid in out ctx)} =
 		command_bidi_pipe(qw(cat-file --batch));
 }
 
@@ -926,9 +923,8 @@ sub _close_cat_blob {
 
 	return unless defined($self->{cat_blob_pid});
 
-	my @vars = map { 'cat_blob_' . $_ } qw(pid in out ctx);
-
-	command_close_bidi_pipe($self->{@vars});
+	my @vars = map { "cat_blob_$_" } qw(pid in out ctx);
+	command_close_bidi_pipe(@$self{@vars});
 	delete $self->{@vars};
 }
 
Junio C Hamano· Aug 4, 2008, 08:05 UTC · re: Petr Baudis · lore

Re: [PATCH] Git.pm: Fix internal git_command_bidi_pipe() users

Petr Baudis <pasky@suse.cz> writes:
Show 11 quoted lines
> The hash_and_insert_object() and cat_blob() helpers were using
> an incorrect slice-from-ref Perl syntax. This patch fixes that up
> in the _close_*() helpers and make the _open_*() helpers use the
> same syntax for consistnecy.
>
> Signed-off-by: Petr Baudis <pasky@suse.cz>
> ---
>
>   Wow, the command_bidi_pipe API really is dirty. Of course, it is
> my fault as anyone's since I didn't get around to review the patches
> introducing it.
Sorry, delete is still broken with your patch, isn't it?

The earlier patch from Abhijit Menon-Sen does this properly for close_hash_and_insert and close_cat_blob, which I've queued already.

Petr Baudis· Aug 4, 2008, 08:21 UTC · re: Junio C Hamano · lore

Re: [PATCH] Git.pm: Fix internal git_command_bidi_pipe() users

On Mon, Aug 04, 2008 at 01:05:56AM -0700, Junio C Hamano wrote:
Show 15 quoted lines
> Petr Baudis <pasky@suse.cz> writes:
> 
> > The hash_and_insert_object() and cat_blob() helpers were using
> > an incorrect slice-from-ref Perl syntax. This patch fixes that up
> > in the _close_*() helpers and make the _open_*() helpers use the
> > same syntax for consistnecy.
> >
> > Signed-off-by: Petr Baudis <pasky@suse.cz>
> > ---
> >
> >   Wow, the command_bidi_pipe API really is dirty. Of course, it is
> > my fault as anyone's since I didn't get around to review the patches
> > introducing it.
> 
> Sorry, delete is still broken with your patch, isn't it?

Oh, right - I forgot that one and it didn't occur to me to test this part.

> The earlier patch from Abhijit Menon-Sen does this properly for
> close_hash_and_insert and close_cat_blob, which I've queued already.

Abhijit, can you please tag your Git.pm patches so that I actually have a chance to see and review it?

Thanks,
				Petr "Pasky" Baudis
Junio C Hamano· Aug 4, 2008, 08:37 UTC · re: Petr Baudis · lore

Re: [PATCH] Git.pm: Fix internal git_command_bidi_pipe() users

Petr Baudis <pasky@suse.cz> writes:
Show 25 quoted lines
> On Mon, Aug 04, 2008 at 01:05:56AM -0700, Junio C Hamano wrote:
>> Petr Baudis <pasky@suse.cz> writes:
>> 
>> > The hash_and_insert_object() and cat_blob() helpers were using
>> > an incorrect slice-from-ref Perl syntax. This patch fixes that up
>> > in the _close_*() helpers and make the _open_*() helpers use the
>> > same syntax for consistnecy.
>> >
>> > Signed-off-by: Petr Baudis <pasky@suse.cz>
>> > ---
>> >
>> >   Wow, the command_bidi_pipe API really is dirty. Of course, it is
>> > my fault as anyone's since I didn't get around to review the patches
>> > introducing it.
>> 
>> Sorry, delete is still broken with your patch, isn't it?
>
> Oh, right - I forgot that one and it didn't occur to me to test this
> part.
>
>> The earlier patch from Abhijit Menon-Sen does this properly for
>> close_hash_and_insert and close_cat_blob, which I've queued already.
>
> Abhijit, can you please tag your Git.pm patches so that I actually have
> a chance to see and review it?
It is $gmane/91316, Message-ID: <20080804050247.GA13539@toroid.org>

After queueing it, I actually had to revert it, because it seems to break git-svn (t9106-git-svn-commit-diff-clobber.sh, test #8), and I am about to go to bed. If you did not see the same breakage with your patch that does not "fix" delete, it could be that the git-svn uses some of the resources that are released by the delete actually doing what it is asked to do.

---
At 2008-08-04 12:49:27 +0800, rctay89@gmail.com wrote:
>
> $self->{@vars} evaluates to undef. i can't find any mention of using
> arrays to dereference objects in the manual and elsewhere; is this a
> mistake?

Yes, @vars would be interpreted in scalar context, which certainly isn't the intended effect.

-- ams
 perl/Git.pm |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index 087d3d0..2ef437f 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -839,8 +839,8 @@ sub _close_hash_and_insert_object {
 
 	my @vars = map { 'hash_object_' . $_ } qw(pid in out ctx);
 
-	command_close_bidi_pipe($self->{@vars});
-	delete $self->{@vars};
+	command_close_bidi_pipe(@$self{@vars});
+	delete @$self{@vars};
 }
 
 =item cat_blob ( SHA1, FILEHANDLE )
@@ -928,8 +928,8 @@ sub _close_cat_blob {
 
 	my @vars = map { 'cat_blob_' . $_ } qw(pid in out ctx);
 
-	command_close_bidi_pipe($self->{@vars});
-	delete $self->{@vars};
+	command_close_bidi_pipe(@$self{@vars});
+	delete @$self{@vars};
 }
 
 =back
-- 
1.6.0.rc0.43.g2aa74
Abhijit Menon-Sen· Aug 4, 2008, 11:38 UTC · re: Junio C Hamano · lore

[PATCH] Git.pm: localise $? in command_close_bidi_pipe()

Git::DESTROY calls _close_cat_blob and _close_hash_and_insert_object, which in turn call command_close_bidi_pipe, which calls waitpid, which alters $?. If this happens during global destruction, it may alter the program's exit status unexpectedly. Making $? local to the function solves the problem.

(The problem was discovered due to a failure of test #8 in t9106-git-svn-commit-diff-clobber.sh.)

Signed-off-by: Abhijit Menon-Sen <ams@toroid.org>
---
At 2008-08-04 01:37:06 -0700, gitster@pobox.com wrote:
>
> After queueing it, I actually had to revert it, because it seems to
> break git-svn (t9106-git-svn-commit-diff-clobber.sh, test #8), and I
> am about to go to bed.
This patch in addition to my earlier one should solve the problem.

For test #8 to fail, the "git svn dcommit" must succeed, but in both cases (i.e. without my patch applied, or with), the rebase fails:

    rebase refs/remotes/git-svn: command returned error: 1

This results in a call to "fatal $@" on git-svn.perl:254, which calls "exit 1", and test_must_fail is happy.

With my patch, however, Git::DESTROY calls the two _close functions during global destruction, which in turn call command_close_bidi_pipe, which calls waitpid with sensible arguments this time, which alters $?, thus altering the exit status of the dcommit itself to 0. Oops.

All of "make test" passes for me after this change.
-- ams
 perl/Git.pm |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index 2ef437f..3b6707b 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -417,6 +417,7 @@ have more complicated structure.
 =cut
 
 sub command_close_bidi_pipe {
+	local $?;
 	my ($pid, $in, $out, $ctx) = @_;
 	foreach my $fh ($in, $out) {
 		unless (close $fh) {
-- 
1.6.0.rc0.43.g2aa74
Junio C Hamano· Aug 5, 2008, 06:12 UTC · re: Abhijit Menon-Sen · lore

Re: [PATCH] Git.pm: localise $? in command_close_bidi_pipe()

Abhijit Menon-Sen <ams@toroid.org> writes:
> With my patch, however, Git::DESTROY calls the two _close functions
> during global destruction, which in turn call command_close_bidi_pipe,
> which calls waitpid with sensible arguments this time, which alters $?,
> thus altering the exit status of the dcommit itself to 0. Oops.
Thanks.
David Christensen· Aug 4, 2008, 15:01 UTC · re: Ray Chuan · lore

Re: [git/perl] unusual syntax?

Show 13 quoted lines
> sub _close_hash_and_insert_object {
> 	my ($self) = @_;
>
> 	return unless defined($self->{hash_object_pid});
>
> 	my @vars = map { 'hash_object_' . $_ } qw(pid in out ctx);
>
> 	command_close_bidi_pipe($self->{@vars});
> 	delete $self->{@vars};
> }
> $self->{@vars} evaluates to undef. i can't find any mention of using
> arrays to dereference objects in the manual and elsewhere; is this a
> mistake?

This is a hash slice notation, returning an array of hash values matching the corresponding keys. 5.10 removed some syntax warts in the case of hash slices; this is more portably expressed as @{$self} {@vars}; this should work in 5.10 and earlier versions, and so is the preferred syntax.

Regards,

David -- David Christensen End Point Corporation david@endpoint.com

← back to recent threads