Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More methods. Of these, "detect" is probably the most useful; it stop…

…s after the first successful execution of a command.
  • Loading branch information...
commit e865416f5eee7d696286748b017799034e918c73 1 parent 6426e76
@spencertipping authored
Showing with 12 additions and 0 deletions.
  1. +6 −0 perlquery
  2. +6 −0 perlquery.sdoc
View
6 perlquery
@@ -57,11 +57,17 @@ sub sub_for_noun {return sub{1} if $_[0] eq '*';
my %methods = (map => sub {my ($self, @args) = @_; my $f = sub_for join ' ', @args; my $x; [map {($x = &$f($_)) eq 1 || $x eq '' ? $_ : $x} @$self]},
grep => sub {my ($self, @args) = @_; my $f = sub_for join ' ', @args; [grep {&$f($_)} @$self]},
+ log => sub {my ($self, @args) = @_; my $count = @$self; my $name = $args[0] // 'log'; print STDERR "$name: $count entries\n"; $self},
+
each => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args; [map strip(join '', qx/@quoted '$_'/), @$self]},
all => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args, @$self; [split /\n/, join '', qx/@quoted/]},
eachi => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args; system("@quoted '$_'") for @$self; $self},
alli => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args, @$self; system("@quoted"); $self},
test => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args; [grep !system("@quoted '$_'"), @$self]},
+ detect => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args; !system("@quoted '$_'") and return [$_] for @$self},
+
+ at => sub {my ($self, @args) = @_; [@$self[@args]]},
+ first => sub {my ($self, @args) = @_; [@$self[0 .. ($args[0] || 1) - 1]]},
uniq => sub {my ($self) = @_; my %keys; $keys{$_} = 1 for @$self; [sort keys %keys]},
size => sub {my ($self) = @_; [scalar @$self]},
View
6 perlquery.sdoc
@@ -57,11 +57,17 @@ sub sub_for_noun {return sub{1} if $_[0] eq '*';
my %methods = (map => sub {my ($self, @args) = @_; my $f = sub_for join ' ', @args; my $x; [map {($x = &$f($_)) eq 1 || $x eq '' ? $_ : $x} @$self]},
grep => sub {my ($self, @args) = @_; my $f = sub_for join ' ', @args; [grep {&$f($_)} @$self]},
+ log => sub {my ($self, @args) = @_; my $count = @$self; my $name = $args[0] // 'log'; print STDERR "$name: $count entries\n"; $self},
+
each => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args; [map strip(join '', qx/@quoted '$_'/), @$self]},
all => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args, @$self; [split /\n/, join '', qx/@quoted/]},
eachi => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args; system("@quoted '$_'") for @$self; $self},
alli => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args, @$self; system("@quoted"); $self},
test => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args; [grep !system("@quoted '$_'"), @$self]},
+ detect => sub {my ($self, @args) = @_; my @quoted = map "'$_'", @args; !system("@quoted '$_'") and return [$_] for @$self},
+
+ at => sub {my ($self, @args) = @_; [@$self[@args]]},
+ first => sub {my ($self, @args) = @_; [@$self[0 .. ($args[0] || 1) - 1]]},
uniq => sub {my ($self) = @_; my %keys; $keys{$_} = 1 for @$self; [sort keys %keys]},
size => sub {my ($self) = @_; [scalar @$self]},
Please sign in to comment.
Something went wrong with that request. Please try again.