Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

james (1129)

james
  (email not shown publicly)
http://whoot.org/

...

Journal of james (1129)

Thursday August 15, 2002
04:41 AM

Pipeline Power

[ #7106 ]

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;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Hmmm, I've been caught out by using accessors like 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?

    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
    • > Hmmm, I've been caught out by using accessors like
      > 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:
      1. Usable - there is a valid LWP::UserAgent object
      2. Unusable - there is no valid LWP::UserAgent object

      Cases can be made for wanting to

    • > what happens if you want to store undef

      I usually write something like

      sub foo {
          my $self = shift;
          return   @_  ? $self->{foo}=$_[0]  : $self->{foo};
      }

      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

      sub foo {
          my $self = shift;
          if (@_) {
              $self->{foo} = shift;
           

      • Put these two subroutines somewhere

        sub non_chaining_accessor {
            my $name = (caller 1)[3];
            $name = substr $name, rindex($name,':')+1;
            $#_  ? $_[0]->{$name}=$_[1]  : $_[0]->{$name}
        }
        sub chaining_accessor {
            my $name = (caller 1)[3];
            $name = substr $name, rindex($name,':')+1;
            if ($#_) {
                $_[0]->{$name} = $_[1];
                return $_[0];
            } else {