in reply to Re: Post query to Metacpan API
in thread Post query to Metacpan API

That's cool but too complicated I just need to get version numbers back from module names like:
#!/usr/bin/perl # THIS SCRIPT DOES WORK # (if the module exists and has a normal version number) # Get the latest version numbers of your favorite modules # Usage: $0 Module::Name Other::Module Etc use strict; use warnings; use HTTP::Tiny; my @arg = @ARGV ? @ARGV : ('MetaCPAN::Client'); for (@arg) { print "$_\t", HTTP::Tiny->new->get("https://metacpan.org/pod/$_")->{content} =~ /<span itemprop="softwareVersion">([^<]+)<\/span>/s, "\n" }

Replies are listed 'Best First'.
Re^3: Post query to Metacpan API
by haukex (Archbishop) on Sep 19, 2018 at 19:52 UTC
    That's cool but too complicated

    The original question was "How does one query the metacpan api from perl?", to which the IMO best answer is indeed MetaCPAN::Client. Why reject it so quickly? I'd recommend having a look at the documentation. You'll see it's actually pretty easy to use.

    Here's what fetching the version of a distro looks like without the module:

    use warnings; use strict; use HTTP::Tiny; use URI; use Cpanel::JSON::XS qw/decode_json/; my $http = HTTP::Tiny->new; my @modules = @ARGV ? @ARGV : ('MetaCPAN::Client'); for my $mod (@modules) { my $uri = URI->new('http://fastapi.metacpan.org/v1/module'); $uri->path_segments( $uri->path_segments, $mod ); my $resp = $http->get($uri); die "$uri: $resp->{status} $resp->{reason}\n" unless $resp->{s +uccess}; my $api = decode_json($resp->{content}); print "$mod: $api->{version}\n"; }

    Update: In comparison, stevieb showed just how easy using the module makes it.

Re^3: Post query to Metacpan API
by stevieb (Canon) on Sep 19, 2018 at 20:24 UTC

    I don't believe that this is very difficult:

    use warnings; use strict; use MetaCPAN::Client; my $mc = MetaCPAN::Client->new; my @modules = qw( Test::BrewBuild RPi::WiringPi Devel::Examine::Subs ); for (@modules){ printf "%s: %s\n", $_, $mc->module($_)->version; }

    Output:

    Test::BrewBuild: 2.20 RPi::WiringPi: 2.3628 Devel::Examine::Subs: 1.69

    Here's the code broken up a bit to possibly better describe what's actually happening. The output is the same:

    use warnings; use strict; use MetaCPAN::Client; my $mc = MetaCPAN::Client->new; my @modules = qw( Test::BrewBuild RPi::WiringPi Devel::Examine::Subs ); for my $module_name (@modules){ my $module_object = $mc->module($module_name); my $module_version = $module_object->version; print "$module_name: $module_version\n"; }

    As I said, it may seem daunting at first, but the code will be more reliable going forward. Just takes a bit of time to read documentation, and do some basic testing :)