CWB
view release on metacpan or search on metacpan
lib/CWB/CQP.pm view on Meta::CPAN
@matches = $cqp->dump("Last" [, $from, $to]);
$cqp->undump("Copy", @matches); # produces copy of "Last"
# safely quote regular expressions and literal strings for CQP queries
$query = $cqp->quote('[0-9]+"-[a-z-]+'); # picks single or double quotes
$query = $cqp->quote($cqp->quotemeta($word)); # escape all metacharacters
# activate CQP progress messages during query execution
$cqp->progress_on;
$status = $cqp->progress; # after starting CQP command with run()
($total, $pass, $n_passes, $msg, $percent) = $cqp->progress_info;
$cqp->progress_off;
$cqp->set_progress_handler(\&my_progress_handler); # user-defined handler
# shut down CQP server (exits gracefully)
undef $cqp;
=head1 DESCRIPTION
A B<CWB::CQP> object represents an instance of the corpus query processor CQP
lib/CWB/CQP.pm view on Meta::CPAN
$self->{'query_lock'} = undef;# holds random key while query lock mode is active
## error handling (messages on stderr)
$self->{'error_handler'} = undef; # set to subref for user-defined error handler
$self->{'status'} = 'ok'; # status of last executed command ('ok' or 'error')
$self->{'error_message'} = []; # arrayref to array containing message produced by last command (if any)
## handling of CQP progress messages
$self->{'progress'} = 0; # whether progress messages are activated
$self->{'progress_handler'} = undef; # optional callback for progress messages
$self->{'progress_info'} = []; # contains last available progress information: [$total_percent, $pass, $n_passes, $message, $percent]
## debugging (prints more or less everything on stdout)
$self->{'debug'} = 0;
## select vectors for CQP output (stdout, stderr, stdout|stderr)
$self->{'select_err'} = new IO::Select($err);
$self->{'select_out'} = new IO::Select($out);
$self->{'select_any'} = new IO::Select($err, $out);
## CQP object setup complete
lib/CWB/CQP.pm view on Meta::CPAN
## if there's a complete line in the input buffer, split off all lines
my @new_lines = split /\n/, $self->{'buffer'}, -1; # make sure that last line is handled correctly if buffer ends in \n
$self->{'buffer'} = pop @new_lines; # last entry is incomplete line ("" if buffer ended in \n) => return to input buffer
foreach my $line (@new_lines) {
## skip blank line printed after each CQP command
next if $line eq "";
## handle progress messages if ProgressBar has been activated
if ($self->{'progress'} and $line =~ /^-::-PROGRESS-::-/) {
my ($pass, $n_passes, $message);
(undef, $pass, $n_passes, $message) = split /\t/, $line;
my $percent = ($message =~ /([0-9]+)\%\s*complete/) ? $1+0 : undef; # extract progress percentage, if present
my $total_percent = (100 * ($pass - 1) + ($percent || 0)) / $n_passes; # estimate total progress ($percent assumed to be 0% if not given)
$self->{'progress_info'} = [$total_percent, $pass, $n_passes, $message, $percent];
my $handler = $self->{'progress_handler'};
if (ref($handler) eq 'CODE') {
$handler->($total_percent, $pass, $n_passes, $message, $percent); # call user-defined progress handler
}
}
## regular output lines are collected in object's line buffer
else {
$line = $encoder->decode($line) if defined $encoder;
push @{$self->{'lines'}}, $line;
$lines++;
}
}
}
lib/CWB/CQP.pm view on Meta::CPAN
$self->{'debug'} = $on;
return $prev;
}
=item I<$cqp>->B<progress_on>;
=item I<$cqp>->B<progress_off>;
=item I<$message> = I<$cqp>->B<progress>;
=item (I<$total>, I<$pass>, I<$n_passes>, I<$msg>, I<$percent>) = I<$cqp>->B<progress_info>;
CQP progress messages can be activated and deactivated with the B<progress_on> and B<progress_off> methods (corresponding to C<set ProgressBar on|off;> in CQP).
If active, progress information can be obtained with the method B<progress>, which returns the last progress message received from CQP. The B<progress_info> returns pre-parsed progress information, consisting of estimated total percentage of complet...
It is an error to call B<progress> or B<progress_info> without activating progress messages first.
=cut
## activate / deactivate CQP progress messages
sub progress_on {
my $self = shift;
if ($self->{'progress'}) {
carp 'CWB::CQP: Progress messages have already been activated (ignored)';
lib/CWB/CQP.pm view on Meta::CPAN
my $self = shift;
if ( $self->{'progress'}) {
$self->exec("set ProgressBar off");
$self->{'progress'} = 0;
}
else {
carp 'CWB::CQP: Progress messages have not been turned on yet (ignored)';
}
}
## poll current progress status (estimated total percentage, or detailed information)
sub progress {
my $self = shift;
croak 'CWB::CQP: No progress() information available, please call progress_on() first'
unless $self->{'progress'};
if (not $self->{'command'}) {
carp 'CWB::CQP: No active command, progress() does not make sense';
return undef;
}
else {
## if input is already available, return corresponding progress state; otherwise check for new progress messages
$self->_update
unless @{$self->{'lines'}} > 0;
return $self->{'progress_info'}->[0];
}
}
sub progress_info {
croak 'USAGE: ($total_percent, $pass, $n_passes, $message, $percent) = $cqp->progress_info;'
unless @_ == 1 and wantarray;
my $self = shift;
croak 'CWB::CQP: No progress_info() available, please call progress_on() first'
unless $self->{'progress'};
if (not $self->{'command'}) {
carp 'CWB::CQP: No active command, progress() does not make sense';
return undef;
}
else {
## if input is already available, return corresponding progress state; otherwise check for new progress messages
( run in 0.317 second using v1.01-cache-2.11-cpan-624ce96ca49 )