Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 16, 2013
1 parent 9757379 commit 469822c
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -109,41 +109,37 @@ sub _json {
say $json->encode($data);
}

sub _say { say encode('UTF-8', $_[0]) if length $_[0] }
sub _say { length $_ && say encode('UTF-8', $_) for @_ }

sub _select {
my ($buffer, $selector, $charset, @args) = @_;

$buffer = decode($charset, $buffer) // $buffer if $charset;
my $results = Mojo::DOM->new($buffer)->find($selector);

my $finished;
while (defined(my $command = shift @args)) {

# Number
if ($command =~ /^\d+$/) {
return unless ($results = [$results->[$command]])->[0];
next;
}
($results = [$results->[$command]])->[0] ? next : return
if $command =~ /^\d+$/;

# Text
elsif ($command eq 'text') { _say($_->text) for @$results }
return _say(map { $_->text } @$results) if $command eq 'text';

# All text
elsif ($command eq 'all') { _say($_->all_text) for @$results }
return _say(map { $_->all_text } @$results) if $command eq 'all';

# Attribute
elsif ($command eq 'attr') {
next unless my $name = shift @args;
_say($_->attr->{$name}) for @$results;
if ($command eq 'attr') {
return unless my $name = shift @args;
return _say(map { $_->attr->{$name} } @$results);
}

# Unknown
else { die qq{Unknown command "$command".\n} }
$finished++;
}

unless ($finished) { _say($_) for @$results }
_say(@$results);
}

1;
Expand Down

0 comments on commit 469822c

Please sign in to comment.