Skip to content
Browse files

Allow extra constructor args after HTTP::Message

and simplify the base constructor
  • Loading branch information...
1 parent 7796d4b commit 2271b13fdde6d8155ae0d00ab4318b69abe61d59 @rwstauner committed Apr 21, 2012
Showing with 11 additions and 21 deletions.
  1. +11 −15 lib/AnyEvent/HTTP/Message.pm
  2. +0 −6 t/message.t
View
26 lib/AnyEvent/HTTP/Message.pm
@@ -14,7 +14,7 @@ The constructor accepts either:
=for :list
* a single hashref of named arguments
-* an instance of an appropriate subclass of L<HTTP::Message>
+* an instance of an appropriate subclass of L<HTTP::Message> (with an optional hashref of additional parameters)
* or a specialized list of arguments that will be passed to L</parse_args> (which must be defined by the subclass).
=cut
@@ -23,18 +23,13 @@ sub new {
my $class = shift;
my $self;
- if( @_ == 1 ){
- my $arg = $_[0];
- if( ref($arg) eq 'HASH' ){
- # if passed a single hashref take a shallow copy
- $self = { %$arg };
- }
- elsif( Scalar::Util::blessed($arg) && $arg->isa('HTTP::Message') ){
- $self = $class->from_http_message($arg);
- }
- else {
- $class->_error(sprintf qq[Invalid argument: '%s'], ref($arg) || $arg);
- }
+ if( ref($_[0]) eq 'HASH' ){
+ # if passed a single hashref take a shallow copy
+ $self = { %{ $_[0] } };
+ }
+ elsif( Scalar::Util::blessed($_[0]) && $_[0]->isa('HTTP::Message') ){
+ # allow an optional second hashref for extra params
+ $self = $class->from_http_message(@_);
}
else {
# otherwise it's the argument list for http_request()
@@ -57,7 +52,8 @@ sub _error {
=class_method parse_args
Called by the constructor
-when L</new> is called with a list of arguments.
+when L</new> is called with
+a list of arguments.
Must be customized by subclasses.
@@ -70,7 +66,7 @@ sub parse_args {
=class_method from_http_message
Called by the constructor
-when L</new> is called with a single argument that is
+when L</new> is called with
an instance of a L<HTTP::Message> subclass.
Must be customized by subclasses.
View
6 t/message.t
@@ -67,12 +67,6 @@ foreach my $args (
test_new [ foo => 'bar' ], q/parse_args\(\) is not defined/,
'fails when custom parse_args() not defined';
- test_new [ \'oops' ], q/Invalid argument: 'SCALAR'/,
- 'fails with unknown single argument';
-
- test_new [ bless [], 'Stinky' ], q/Invalid argument: 'Stinky'/,
- 'fails with unknown single argument';
-
# we should be able to test this failure without the real HTTP::Message
{
my $msg = $httpm->new();

0 comments on commit 2271b13

Please sign in to comment.
Something went wrong with that request. Please try again.