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

[PATCH 5/7] perl/Git.pm: simplify Git::activestate_pipe

From
Gustavo L. de M. Chaves <gnustavo@cpan.org>
Date
Jan 30, 2013, 17:23 UTC
Message-ID
<1359566583-19654-6-git-send-email-gnustavo@cpan.org>
In-Reply-To
<1359566583-19654-1-git-send-email-gnustavo@cpan.org>
From: "Gustavo L. de M. Chaves" <gnustavo@cpan.org>

Git::activestate_pipe::TIEHANDLE creates an object to keep the external command's output as an array of lines. The object also kept an index into the array to know up to which line had already been read via Git::activestate_pipe::READLINE.

We don't really need that index because lines already read don't need to be kept. So, we simply unshift lines as they're being read and use the array's size to know when we have read all lines.

This implementation uses more idiomatic Perl, which makes it more readable and probably a little bit faster.

Signed-off-by: Gustavo L. de M. Chaves <gnustavo@cpan.org>
---
 perl/Git.pm | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index 42c3971..2d88b89 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -1402,33 +1402,28 @@ sub TIEHANDLE {
 	# should take care of the most common cases.
 	my @escaped_params = map { "\"$_\"" } map { s/"/""/g; $_ } @params;
 	my @data = qx{git @escaped_params};
-	bless { i => 0, data => \@data, exit => $? }, $class;
+	bless { data => \@data, exit => $? }, $class;
 }
 
 sub READLINE {
 	my $self = shift;
-	if ($self->{i} >= scalar @{$self->{data}}) {
-		return undef;
-	}
-	my $i = $self->{i};
+	return unless @{$self->{data}};
 	if (wantarray) {
-		$self->{i} = $#{$self->{'data'}} + 1;
-		return splice(@{$self->{'data'}}, $i);
+		return splice @{$self->{data}};
+	} else {
+		return shift @{$self->{data}};
 	}
-	$self->{i} = $i + 1;
-	return $self->{'data'}->[ $i ];
 }
 
 sub CLOSE {
 	my $self = shift;
 	delete $self->{data};
-	delete $self->{i};
 	return $self->{exit} == 0;
 }
 
 sub EOF {
 	my $self = shift;
-	return ($self->{i} >= scalar @{$self->{data}});
+	return @{$self->{data}} == 0;
 }
 
 
-- 
1.7.12.464.g83379df.dirty
Previous: Gustavo L. de M. ChavesNext: Gustavo L. de M. Chaves
Message 6 of 9 in “perl/Git.pm: a bunch of fixes for Windows”
  1. 0/7 perl/Git.pm: a bunch of fixes for WindowsGustavo L. de M. Chaves, Jan 30, 2013
  2. 1/7 perl/Git.pm: test portably if a path is absoluteGustavo L. de M. Chaves, Jan 30, 2013
  3. 2/7 perl/Git.pm: set up command environment on WindowsGustavo L. de M. Chaves, Jan 30, 2013
  4. 3/7 perl/Git.pm: fix _cmd_close on WindowsGustavo L. de M. Chaves, Jan 30, 2013
  5. 4/7 perl/Git.pm: escape external command's arguments on WindowsGustavo L. de M. Chaves, Jan 30, 2013
  6. 5/7 perl/Git.pm: simplify Git::activestate_pipeGustavo L. de M. Chaves, Jan 30, 2013
  7. 6/7 perl/Git.pm: make command pipe work in slurp-mode on WindowsGustavo L. de M. Chaves, Jan 30, 2013
  8. 7/7 perl/Git.pm: rename 'ActiveState' to 'Windows'Gustavo L. de M. Chaves, Jan 30, 2013
  9. Junio C HamanoFeb 25, 2013

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.