| MozRepl documentation | Contained in the MozRepl distribution. |
MozRepl - Perl interface of MozRepl
version 0.06
use strict;
use warnings;
use MozRepl;
my $repl = MozRepl->new;
$repl->setup; ### You must write it.
$repl->execute(q|window.alert("Internet Explorer:<")|);
print $repl->repl_inspect({ source => "window" });
print $repl->repl_search({ pattern => "^getElement", source => "document"});
MozRepl is accessing and control firefox using telnet, provided MozLab extension. This module is perl interface of MozRepl.
Additionaly this is enable to extend by writing plugin module. You want to write plugin, see MozRepl::Plugin::Base or other plugins.
In cygwin, please add binmode param as 1 in client args.
$repl->setup({
client => {
extra_client_args => {
binmode => 1
}
}
});
Create MozRepl instance. One argument, and it must be hash reference.
Module::Pluggable::Fast's arguments. If you want to search modules has not prefix like 'MozRepl::Plugin', then you are set this value like below.
my $repl = MozRepl->new({ search => [qw/MyRepl::Plugin OtherRepl::Plugin/] });
Setup logging, client, plugins. One argument, must be hash reference.
Hash reference or undef. See setup_log($args) in MozRepl, new($args) in MozRepl::Log.
Hash reference or undef. See setup_client($args) in MozRepl, new($ctx, $args) in MozRepl::Client.
Hash reference or undef See setup_plugins($args) in MozRepl.
Create logging instance. default class is MozRepl::Log. If you want to change log class, then set class name using log_class($class) in MozRepl.
This method is only called in setup($args) in MozRepl.
One arguments, array reference. If you want to limit log levels, specify levels like below.
$repl->setup_log([qw/info warn error fatal/]);
See new($args) in MozRepl::Log.
If you want to use another log class, and already instanciate it, then you should call and set the instance before setup() method process.
Example,
my $repl = MozRepl->new;
$repl->log($another_log_instance);
$repl->setup($config);
Create (telnet) client instance. default class is MozRepl::Client. If you want to change client class, then set class name using client_class($class) in MozRepl.
This method is only called in setup($args) in MozRepl.
One arguments, hash reference. See new($ctx, $args) in MozRepl::Client.
Setup plugins. One argument, must be hash reference, it will be passed each plugin's as new method arguments. And load_plugins($args) in MozRepl too.
This method is only called in setup($args) in MozRepl.
Create plugin instance, and mixin method to self. Method name is detect by plugin's package, see plugin_to_method($plugin, $search) in MozRepl::Util.
Load available plugins. One argument, must be hash reference or undef.
Array reference. Specify only plugins you want to use.
$repl->load_plugins({ plugins => [qw/Repl::Print Repl::Inspect/] });
Array reference. Specify except plugins you want to use.
$repl->load_plugins({ except_plugins => [qw/JSON/] });
Return whether the specified plugin is enabled or not.
Execute command and return result string. See execute($command) in MozRepl::Client.
Finalize connection.
Accessor of client object. See MozRepl::Client.
Accessor of log object. See MozRepl::Log.
Accessor of plugin table, key is plugin class name, value is plugin instance.
Accessor of "repl" object name. If two or more connection to MozRepl, this name is added number on postfix like 'repl1'.
Accessor of search pathes. See new($args) in MozRepl.
Logging class name. default value is "MozRepl::Log"
Client class name. default value is "MozRepl::Client"
Toru Yamaguchi, <zigorou@cpan.org>
Please report any bugs or feature requests to
bug-mozrepl@rt.cpan.org, or through the web interface at
http://rt.cpan.org. I will be notified, and then you'll automatically be
notified of progress on your bug as I make changes.
Copyright 2007 Toru Yamaguchi, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| MozRepl documentation | Contained in the MozRepl distribution. |
package MozRepl; use strict; use warnings; use base qw(Class::Accessor::Fast Class::Data::Inheritable); __PACKAGE__->mk_accessors($_) for (qw/client log plugins repl search/); __PACKAGE__->mk_classdata($_) for (qw/log_class client_class/); __PACKAGE__->log_class('MozRepl::Log'); __PACKAGE__->client_class('MozRepl::Client'); use Text::SimpleTable; use UNIVERSAL::require; use MozRepl::Util;
our $VERSION = '0.06';
sub new { my ($class, $args) = @_; if (exists $args->{search} && ref $args->{search} eq 'ARRAY') { unshift(@{$args->{search}}, "MozRepl::Plugin"); my %seen = (); $args->{search} = [grep { ++$seen{$_} } @{$args->{search}}]; } else { $args->{search} = ["MozRepl::Plugin"]; } my $pluggable = "Module::Pluggable::Fast"; my %param = ( "require" => 1, "name" => "__load_plugins", "search" => $args->{search} ); $pluggable->use(%param); my $self = $class->SUPER::new({ client => undef, log => undef, repl => 'repl', plugins => {}, search => $args->{search} }); return $self; }
sub setup { my ($self, $args) = @_; $self->setup_log($args->{log}); $self->setup_client($args->{client}); if ($self->log->is_debug) { my $table = Text::SimpleTable->new([15, 'type'], [60, 'module']); $table->row('logging', $self->log_class); $table->row('client', $self->client_class); $self->log->debug("---- Delegating classes ----\n" . $table->draw); } $self->setup_plugins($args->{plugins}); }
sub setup_log { my ($self, $args) = @_; $args ||= [qw/debug info warn error fatal/]; ### skip already exists log instance unless ($self->log) { $self->log_class->use; $self->log($self->log_class->new(@$args)); } else { $self->log_class(ref $self->log); } return unless ($self->log->is_debug); $self->log->debug('MozRepl logging enabled'); }
sub setup_client { my ($self, $args) = @_; $self->client_class->use; $self->client($self->client_class->new($self, $args)); $self->client->setup($self); }
sub setup_plugins { my ($self, $args) = @_; $self->plugins({}); my @plugins = $self->load_plugins($args); for my $plugin (@plugins) { $self->setup_plugin($plugin, $args); } }
sub setup_plugin { my ($self, $plugin, $args) = @_; return if ($self->enable_plugin($plugin)); my $plugin_obj = $plugin->new($args); $plugin_obj->setup($self, $args); my $method = MozRepl::Util->plugin_to_method($plugin, $self->search); unless ($self->can($method)) { no strict 'refs'; $self->log->debug('define method : ' . $method); *{__PACKAGE__ . "::" . $method} = sub { my ($repl, @args) = @_; $plugin_obj->execute($repl, @args); }; } $self->plugins->{$plugin} = $plugin_obj; }
sub load_plugins { my ($self, $args) = @_; my @available_plugins = grep { $_ ne 'MozRepl::Plugin::Base' } $self->__load_plugins; my %plugins = (); my %except_plugins = (); $self->log->debug(sprintf("Available plugins (%d)", scalar(@available_plugins))); if ($self->log->is_debug && @available_plugins) { my $table = Text::SimpleTable->new([80, 'Available plugin']); $table->row($_) for (@available_plugins); $self->log->debug("---- Available plugin list ----\n" . $table->draw); } return if (@available_plugins == 0); if ($args->{plugins} && ref $args->{plugins} eq 'ARRAY') { $plugins{$_} = 1 for (map { MozRepl::Util->canonical_plugin_name($_) } @{$args->{plugins}}); } else { @plugins{@available_plugins} = map { 1 } @available_plugins; } if ($args->{except_plugins} && ref $args->{except_plugins} eq 'ARRAY') { $except_plugins{$_} = 1 for (map { MozRepl::Util->canonical_plugin_name($_) } @{$args->{except_plugins}}); } my @plugins = grep { $plugins{$_} } grep { !$except_plugins{$_} } grep { $_ ne "MozRepl::Plugin::Base" } @available_plugins; $self->log->debug(sprintf("Load plugins (%d)", scalar(@plugins))); if ($self->log->is_debug && @plugins) { my $table = Text::SimpleTable->new([80, 'Load plugin']); $table->row($_) for (@plugins); $self->log->debug("---- Load plugin list ----\n" . $table->draw); } wantarray ? @plugins : \@plugins; }
sub enable_plugin { my ($self, $plugin) = @_; return ((grep { $_ eq $plugin } keys %{$self->plugins}) == 1) ? 1 : 0; }
sub execute { my ($self, $command) = @_; $self->client->execute($self, $command); }
sub finalize { my ($self, $args) = @_; $self->client->quit; }
1; # End of MozRepl