chansen / p5-http-tiny

Tiny HTTP Client
https://metacpan.org/dist/HTTP-Tiny
53 stars 52 forks source link

Use of uninitialized value in concatenation when `VERSION` is missing #137

Closed oalders closed 3 years ago

oalders commented 4 years ago

This is from a test in MetaCPAN::Client.

{

    package TrapUA;
    use Moo;
    extends 'HTTP::Tiny::Mech';

    sub mechua {
        require WWW::Mechanize::Cached;
        return WWW::Mechanize::Cached->new();
    }
}

When the test using this class is run:

Use of uninitialized value in concatenation (.) or string at /usr/share/perl5/core_perl/HTTP/Tiny.pm line 602.

The code in HTTP::Tiny:

sub _agent {
    my $class = ref($_[0]) || $_[0];
    (my $default_agent = $class) =~ s{::}{-}g;
    return $default_agent . "/" . $class->VERSION;
}

Obviously the test class can add a version to TrapUA in order to get around this (and this is what I'll be doing). I just wanted to establish if it's reasonable for _agent to use a default version in the case that $class->VERSION is not defined. I'm happy to put a pull request together if this is useful.

tobyink commented 4 years ago

Yeah, this always annoys me too.

Maybe something like:

sub _agent {
    my $class = ref($_[0]) || $_[0];
    my $version = $class->VERSION;
    if (not defined $version) {
        $class = __PACKAGE__;
        $version = $class->VERSION;
    }
    (my $default_agent = $class) =~ s{::}{-}g;
    return $default_agent . "/" . $version;
}