For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2005 > HTTP::Request; content_type









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author HTTP::Request; content_type
robert.waters@gmail.com

2005-10-21, 6:56 pm

Hi,

Why does the method 'content_type' work in an HTTP::Request object?
It is not a method of that class, nor is it a method of HTTP::Message
class, which is HTTP::Request's parent class.
'content_type' *is* a method of the HTTP::Headers class, and the man
page for HTTP::Headers says 'Instances of this class are usually
created as member variables of the HTTP::Request and HTTP::Response
classes' - if this were the case, wouldn't 'content_type' be a method
of a *member* of the HTTP::Request class, rather than a method of the
class itself?? (ex. $request->header->content_type(...))
Is there some sideways-inheritance going on?
I am sorry, am familiar with c++ style OOP, but am a relative newcomer
to perl.

Example code:
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(POST => 'http:some.site');
$req->content_type('application/x-www-form-urlencoded');
$req->content('user=user&password=password');

my $resp = $ua->request($req);

Thanks in advance,
Robert

Paul Lalli

2005-10-21, 9:55 pm

robert.waters@gmail.com wrote:
> Why does the method 'content_type' work in an HTTP::Request object?
> It is not a method of that class, nor is it a method of HTTP::Message
> class, which is HTTP::Request's parent class.
> 'content_type' *is* a method of the HTTP::Headers class,


The docs for HTTP::Message state:
All methods unknown to HTTP::Message itself are delegated to the
HTTP::Headers object that is part of every message. This allows
convenient
access to these methods. Refer to HTTP::Headers for details of these
methods:
(large listing of methods, including content_type() ....)

Viewing the source code for HTTP::Message, we see that this is
implemented as follows:
# delegate all other method calls the the _headers object.
sub AUTOLOAD
{
my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
return if $method eq "DESTROY";

# We create the function here so that it will not need to be
# autoloaded the next time.
no strict 'refs';
*$method = eval "sub { shift->{'_headers'}->$method(\@_) }";
goto &$method;
}

You can read more about AUTOLOAD() in:
perldoc perlobj

Paul Lalli

robert.waters@gmail.com

2005-10-22, 6:55 pm

Thank you *very* much; I don't know how I missed that.

Cheers

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com