Agent

 view release on metacpan or  search on metacpan

examples/Eval.pa  view on Meta::CPAN

#!/usr/bin/perl

##
# The Eval agent - a sample agent for doing remote eval()'s.
# Steve Purkis <spurkis@engsoc.carleton.ca>
# October 5, 1998.
##

package Agent::Eval;
@ISA = qw( Agent );

sub new {
	my ($class, %args) = @_;
	my $self = {};
	foreach (keys(%args)) { $self->{"$_"} = $args{"$_"}; }
	bless $self, $class;
}

sub agent_main {
	my ($self, @args) = @_;
	my $message;

	# delete so we only do one hop..
	my $to = delete($self->{Host});
	if ($to) {
		@message = ("$self->{Return}\n", $self->store());
	} else {
		unless ($to = delete($self->{Return})) {
			print "I've been abandoned!\n" if $self->{verbose};
			return;
		}
		@message = eval "$self->{Eval}";
		push @message, "ERROR: $@" if $@; # capture errors, if any
	}

	# transfer self | send result to remote host...
	print "Sending message to $to\n" if $self->{verbose};
	my $msg = new Agent::Message(
		Body      => [ @message ],
		Transport => TCP,
		Address   => $to
	);
	if ($msg->send) { print "done.\n" if $self->{verbose}; }
	else { print "couldn't send message!\n" if $self->{verbose}; }
}

1;

__END__

=head1 NAME

Agent::Eval - the Eval sample agent.

=head1 SYNOPSIS

use Agent;

my $agent = new Agent( Name => 'Eval', %args );
$agent->run;

=head1 DESCRIPTION

The Eval agent relocates to a I<static agent> and eval()'s some user-defined
code there.  Return values are packaged in plaintext and delivered back to
the invoking program.

=head1 PARAMETERS

Host     =>  TCP address of static agent
Eval     =>  code to be eval()'d
Return   =>  TCP address to return results to
verbose  =>  self evident

=head1 NOTES



( run in 2.299 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )