@@ -26,18 +26,28 @@ sub new {
2626
2727 # Handle optional args hash
2828 my %args ;
29- if (@_ && ref ($_ [-1]) eq ' HASH' && exists $_ [-1]-> {useragent }) {
30- %args = %{pop @_ };
31- # Validate useragent parameter
32- if (exists $args {useragent }) {
33- unless (blessed($args {useragent }) && $args {useragent }-> isa(' LWP::UserAgent' )) {
34- Carp::croak(" useragent must be an LWP::UserAgent object" );
29+ if (@_ && ref ($_ [-1]) eq ' HASH' ) {
30+ my $potential_args = $_ [-1];
31+ # Check if this looks like our args hash (has known keys)
32+ my @known_keys = qw( useragent) ;
33+ my @hash_keys = keys %$potential_args ;
34+
35+ # If any key matches our known args, treat it as args hash
36+ if (grep { my $k = $_ ; grep { $k eq $_ } @known_keys } @hash_keys ) {
37+ %args = %{pop @_ };
38+
39+ # Validate useragent parameter if provided
40+ if (exists $args {useragent }) {
41+ unless (blessed($args {useragent }) && $args {useragent }-> isa(' LWP::UserAgent' )) {
42+ Carp::croak(" useragent must be an LWP::UserAgent object" );
43+ }
44+ }
45+
46+ # Only known keys are allowed
47+ my @invalid_keys = grep { my $k = $_ ; !grep { $k eq $_ } @known_keys } keys %args ;
48+ if (@invalid_keys ) {
49+ Carp::croak(" Invalid argument(s): " . join (' , ' , @invalid_keys ));
3550 }
36- }
37- # Only useragent is allowed for now
38- my @invalid_keys = grep { $_ ne ' useragent' } keys %args ;
39- if (@invalid_keys ) {
40- Carp::croak(" Invalid argument(s): " . join (' , ' , @invalid_keys ));
4151 }
4252 }
4353
0 commit comments