This morning coming into work, I knew I needed to hack together a quick proxy server so that I could examine a web protocol stream going back and forth across the network. Whereas previously I'd have sat down and written the whole thing out by hand, this time I decided to use OpenFrame 3 to put together a quick solution.
I had to write just a few lines of code to make it all work -- and some of that code was out of politeness rather than need. The code I wrote was simply a pipeline segment that is added to the OpenFrame pipeline. It simply takes the HTTP::Request out of the store, forwards it to the right place, and then returns the response back to the pipeline:
package Segment::Forwarder;
use strict;
use warnings::register;
use LWP::UserAgent;
use Pipeline::Segment;
use base qw ( Pipeline::Segment );
sub init {
my $self = shift;
my $lwp = LWP::UserAgent->new( env_proxy => 1 );
$self->lwp( $lwp );
}
sub lwp {
my $self = shift;
my $lwp = shift;
if (defined( $lwp )) {
$self->{lwp} = $lwp;
return $self;
} else {
return $self->{lwp};
}
}
sub dispatch {
my $self = shift;
my $pipe = shift;
my $httpr = $pipe->store->get( 'HTTP::Request' );
return $self->lwp->request( $httpr );
}
1;
accessors (Score:1)
This is the reason I am leaning more and more towards Class::Accessor and similiar. I note that you'd have to override set() to return $self that way, however.
-Dom
Re:accessors (Score:1)
> that before. This is a more general code issue,
> and it's probably fine for your small code, but
> what happens if you want to store undef in your
> accessor?
Generally I work very hard to make sure I never need to have an undef in any given state
The object in question will always be in one of two states:
Cases can be made for wanting to
Re:accessors (Score:2)
I usually write something like
which gives you the ability to set something to undef but doesn't let you chain method calls. If you wanted to chain method calls, you could instead write it this way
Now I'm just being silly... (Score:2)