Freebsd – How to automatically install Perl modules from ports in FreeBSD

freebsdperl

I need to automate installation of a multitude of Perl modules. Most of them are present in ports. But some modules are located in bundles, for example www/p5-HTML-Tree prvides HTML::Element, HTML::TreeBuilder, etc.

Ideally, it should be a command line utility with the synax like:

install_from_ports CGI CGI::FormBuilder HTML::TreeBuilder ...

Do you have any suggestions?

Best Answer

Here's a lightly-tested solution:

#!/bin/sh

# Build a regex to match all the .pm files
_regex=""
for arg in $*; do
    arg=`echo ${arg} | sed -e 's|::|/|g'`
    if [ "X${regex}X" != "XX" ]; then
        regex="${regex}|"
    fi
    regex="${regex}(${arg})"
done
regex="%%SITE_PERL%%/(${regex}).pm"

# Find the .pm files and derive the port names from them, then install using portinstall.
find /usr/ports -type f -name pkg-plist -path '*/p5-*' -exec egrep -l $regex {} + | sort -u | sed -e 's|/usr/ports/||' -e 's|/pkg-plist||' | xargs portinstall

Now, this won't detect modules installed by ports which don't have a pkg-plist but they're very much in the minority. There's only 58 out of 4188 p5-* ports on my system which don't have pkg-plist files.