Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

METAreqWeb - overview of CPAN dist prerequisites

by Intrepid (Deacon)
on Oct 10, 2006 at 16:26 UTC ( #577441=snippet: print w/replies, xml ) Need Help??
Description:

Solution to a chatterbox FAQ: what modules required are shipped with Perl? Which are already on the system?

This cli script sits at the corner of my desk awaiting the next time I am looking up Perl modules at search.cpan.org.

Finding the module I am interested in, I examine its directory (like: http://search.cpan.org/~moconnor/YAML-AppConfig-0.16/ ) ... there are a number of files listed, including Makefile.PL (or Build.PL), MANIFEST, README, and ...META.yml.

I context-click on the META.yml file and click "Copy Link Location" to put the URL pointing to the META.yml file on the system clipboard. Then I run the snippet below to quickly get the overview of this module's prerequisites.

#!/usr/bin/env perl

# Last modified: 10 Oct 2006 at 12:02 PM EDT
# Author: Soren Andersen <intrepid -AT- perlmonk *dot* org>
# $Id$

use strict;
use warnings;
use YAML qw/ Load Dump LoadFile /;
use LWP::Simple;
use POSIX qw(isatty);
sub coremod { "" };
sub hr;
sub dimmer;
sub find_longest_keystring;
sub mtest;

sub testyml {
    my $metayml_uri =
      (shift(@_) || 'http://search.cpan.org/src/RUBYKAT/html2dbk-0.03/
+META.yml');
    my $docu;

    if( $docu = get $metayml_uri ) {
    return $docu
    }
    die "Could not retrieve '$metayml_uri'. Network troubles? Bad URI?
+";
}

## And now a brief interlude of presentational trivia, not very intere
+sting
my $screenW = $ENV{COLUMNS} || 42;
my $unxterm = isatty(*STDOUT) && $^O !~/MSWin32/ ? 1 : 0;
if ($unxterm) {
    require Term::ANSIColor and import Term::ANSIColor ('colored');
   #print colored("Using Term::ANSIColor", 'dark white'), "\n";
} ## done with interlude, back to main show

my $metadoc = testyml (shift(@ARGV) || undef);
my $modmeta = Load( $metadoc );

if( $modmeta->{requires} ) {
    my ($k,$v);
    my $fmtlen;
    my %reqrs = %{
                   my($mkl,@hash)
                    = find_longest_keystring($modmeta->{requires});
                   $fmtlen = $mkl || 28;
                   my $rh = {@hash};
                 };
    if (eval "use Module::CoreList; 1" and not $@)
    {
        no warnings ('redefine','once');
        sub coremod
        {
            my($pm) = @_;
            my $presence_in_core = $Module::CoreList::version{$]}{$pm}
+;
            return "\n" unless defined $presence_in_core;
            my $pmv;
            $pmv = $presence_in_core == 0 ? "without a VERSION number"
               : "version $presence_in_core";
            my $prn  = Module::CoreList->first_release($pm);
            my $note = <<"        ELEPHANT";

  CORE: $pm was first included in the core Perl distribution at Perl r
+elease $prn
        The present Perl system shipped with $pm $pmv
        ELEPHANT
            return $note;
        }
    }

    print hr;
    printf <<"        YODOWN"
 %-${fmtlen}s %s
   %s%s
        YODOWN
 => ($k,($v eq '0'? 'any version':"at version $v") , mtest($k), coremo
+d($k))
           while ($k,$v) = each %reqrs;
    print hr;
} else # there are no prerequisite modules or pragmata listed
    {  print "No requires for $modmeta->{name}\n"; }

sub mtest
{
    my $installed_version;
    my $wherep;
    my $modname = shift();
    my $modlibp = $modname;
    $modlibp =~ s{ :: } {/}xg;
    $modlibp .= '.pm';
    my $can_req = eval "use $modname; 1;";
  if   ($can_req and not $@)
    {
        no strict 'refs';
        $wherep = $INC{ $modlibp };
        $installed_version = (${$modname.'::VERSION'}) || 0;
    }
  else
    {
        return colored(sprintf(
            "%-32s is not installed"=> $modname) , 'cyan')
           if ($unxterm);
        return sprintf(
            "%-32s is not installed"=> $modname)
    }
    
    if ($unxterm) {
    colored(
        sprintf('%-32s v%4s found as %s'
         => ($modname , $installed_version , $wherep))
      , 'magenta' );
    } else {
        sprintf('%-32s v%-4s found as %s'
         => ($modname , $installed_version , $wherep))
    }
}

sub find_longest_keystring
{
    my %rethash = %{ shift() };
    return () unless 1 * @{[ %rethash ]};
    delete $rethash{'perl'};
    my $l_maxed_at = 0;
    for (keys(%rethash)) {
    $l_maxed_at = length() > $l_maxed_at ? length() : $l_maxed_at
    }
    return ( $l_maxed_at, %rethash );
}

sub hr
{
    my $decor = $unxterm
      ? dimmer("-" x $screenW)
      :       ("-" x $screenW);
    $decor . "\n";
}

sub dimmer
{
    colored( shift() , 'dark white' );
}
__END__

=head1 NAME

METAreqWeb

=head1 SYNOPSIS

  ./METAreqWeb [ <http://search.cpan.org/src/MOCONNOR/YAML-AppConfig-0
+.16/META.yml> ]

=cut
Replies are listed 'Best First'.
Re: METAreqWeb - overview of CPAN dist prerequisites
by grinder (Bishop) on Oct 10, 2006 at 17:29 UTC

    Nice code, but, I hate to break it to you, but you're using the wrong web site. If you used Randy Kobes's site, you'd already have it on the page:

    YAML-AppConfig @ kobes

    Either that, or CPANTS lists the information as well

    YAML-AppConfig @ cpants

    Hmmm... you've given me an idea, some bookmarklets would come in handy for this stuff.

    • another intruder with the mooring in the heart of the Perl

      On on Oct 10, 2006, grinder wrote:

      Nice code, but, I hate to break it to you, but you're using the wrong web site. If you used Randy Kobes's site, you'd already have it on the page:

      YAML-AppConfig @ kobes

      Either that, or CPANTS lists the information as well

      YAML-AppConfig @ cpants

      Hmmm... you've given me an idea, some bookmarklets would come in handy for this stuff.

      Glad I could provide some inspiration. I'd be very interested in seeing a Bookmarklet, indeed, for CPAN module information! Very cool. ;-)

      I do have to make a correction, however, for the benefit of those that cannot read the code. Listing the modules required by a chosen CPAN module is only 1/3 of the functionality of the solution shown above. The other 2/3 are:

      1. print information on whether the prerequisite is part of the core Perl distribution ("bundled with Perl"), and if so, when (for what Perl release) that became true; and
      2. print information on the result of detecting the presence of said modules on the user's system.

      Changing websites to Kobe or CPANTS isn't going to accomplish the latter 2/3 of the functionality of the code above (nor answer the FAQ chatterbox question, "How can I tell whether I have that module already?", that prompted this contribution.

      The latter 2 operations are fundamentally local in nature and nothing short of a complex client-side scripting project is going to make that info available in the http user agent (browser).

      Boy, I did a good job of moderating my tone in this response and not displaying my disappointment over receiving such a careless, clueless comment / evaluation / feedback ... didn't I. grinder usually does better.

          Soren A / somian / perlspinr / Intrepid

      -- 
      Words can be slippery, so consider who speaks as well as what is said; know as much as you can about the total context of the speaker's participation in a forum over time, before deciding that you fully comprehend the intention behind those words. If in doubt, ask for clarification before you 'flame'.
Re: METAreqWeb - overview of CPAN dist prerequisites
by chanio (Priest) on Nov 12, 2006 at 20:13 UTC
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://577441]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2023-06-03 17:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (16 votes). Check out past polls.

    Notices?