Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/WWW/Mechanize.pm
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,8 @@ sub content {

my $content = $self->{content};
if ( delete $params{raw} ) {
$content = $self->response()->content();
my $res = $self->response();
$content = $res->content() if $res;
}
elsif ( delete $params{decoded_by_headers} ) {
$content = $self->response()->decoded_content( charset => 'none' );
Copy link

Copilot AI Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the raw content case, this line could also die if response() returns undef. The same null check should be applied here for consistency.

Suggested change
$content = $self->response()->decoded_content( charset => 'none' );
my $res = $self->response();
$content = $res->decoded_content( charset => 'none' ) if $res;

Copilot uses AI. Check for mistakes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot makes a good point.
It seems to me the whole problem follows when user requests for content before any remote http call has been made.
Is there any way to prevent that? Or do we even want to?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to prevent that? Or do we even want to?

I think this is a good start. We can protect the user from themselves without introducing new types of errors. Even with really innocent looking changes, often someone will appear after a subsequent release to let us know that our fix has broken their code. So, it's good to be conservative here. I think this particular case is worth fixing, though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we are done and ready for merging?

Expand Down Expand Up @@ -1714,10 +1715,9 @@ sub form_action {
my ( $self, $action ) = @_;

my $temp;
my @matches = grep {
defined( $temp = $_->action )
and ( $temp =~ m/$action/msx )
} $self->forms;
my @matches
= grep { defined( $temp = $_->action ) and ( $temp =~ m/$action/msx ) }
$self->forms;

my $nmatches = @matches;
if ( $nmatches > 0 ) {
Expand Down
4 changes: 4 additions & 0 deletions t/content.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ my $mech = WWW::Mechanize->new();
$mech->{base} = 'http://example.com/';

is( $mech->content, undef, 'content starts out as undef' );
is(
$mech->content( raw => 1 ),
undef, 'raw content is just as undef as normal'
);

$mech->update_html($html);

Expand Down