Hi there, I wondered if there's any value for such a script, if it's silly, or if there's something better that already exists.

Today I tried using scan-prereqs-cpanfile for the first time, and was disappointed that it didn't detect all of the dependencies of a project. So I had to install quite a few dependencies manually. Hence this little hack to never need to do this again - hopefully.

Warning: this code hasn't been tested at all - I'm at a friend's on Windows, but I really want to get feedback on this idea, and I won't be near my computer for two days. Here is the very simple pseudo code:

### THE SCRIPT:
my $cmd_from_user = join ' ', @ARGV;
while( my $dep = return_a_missing_dependency_for_running($cmd_from_user) ) {
    install_a_dependency($dep);
}

### FUNCTIONS:
use Sysadm::Install qw(tap); # tap($cmd): To run a bash command and read the output

sub install_a_dependency {
    my $dep = shift;

    # TODO: Possibly, prompt me (yes/no) to ask if I want to install $dep by running this command:
    my $cmd_to_install_dep = "cpanm $dep"; # Adapt this to your own needs
    my ($stdout, $stderr, $exit_code) = tap($cmd_to_install_dep);
    
    die "Could not install module $dep" if could_not_install_dep($stderr); # Some regex in here
}

sub return_a_missing_dependency_for_running {
    my $cmd_from_user = shift;

    my ($stdout, $stderr, $exit_code) = tap($cmd_from_user);
    
    return extract_missing_dependency_from_output($stderr); # some regex in here. Returns an empty String if no dependencies are missing.
}    

This could easily be adapted to any dependency management system. For example for Carton you would do:

# Add the dependency to the cpanfile, and install it with Carton
my $cmd_to_install_dep = $qq{echo "requires '$dep';" >> cpanfile; carton install}; 

Obviously I'm thinking: if it worked as expected and was useful, it would bloody well exist already. Either I didn't look in the right place, or there is a reason why this is a bad idea.

What do you think?


In reply to A hack to automatically install all dependencies for a given script by mascip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.