<- Back to the HOWTO section

How to create portfiles for CPAN Perl modules

  • Audience: Perl users
  • Requires the following MacPorts perl ports: p5-list-moreutils p5-parse-cpan-meta p5-module-depends p5-cpan-meta-yaml. Note that perl 5.22 is the Macports default version, you can install them as follows:
    port install p5.22-list-moreutils p5.22-parse-cpan-meta p5.22-module-depends p5.22-cpan-meta-yaml


When you find you need a Perl modules from CPAN that does not yet have a MacPorts port, cpan2port can help you create a port file.

Step 1: Set up cpan client

You must have a properly configured cpan client for cpan2port to work. You can either step through the questions, or let it auto-configure.

$ cpan
CPAN is the world-wide archive of perl resources. It consists of about
Would you like me to configure as much as possible automatically? [yes] ''yes''
cpan shell -- CPAN exploration and modules installation (v1.9301)
ReadLine support available (maybe install Bundle::CPAN or Bundle::CPANxxl?)

cpan[1]> ^D

Step 2: Find the CPAN package name

$ perl -MCPAN -e shell
cpan[1]> m Date::Parse
Module id = Date::Parse
   DESCRIPTION  ASCII Date parser using regexp's
   CPAN_USERID  GBARR (Graham Barr <>)
   CPAN_FILE    G/GB/GBARR/TimeDate-1.20.tar.gz
   DSLIP_STATUS Rdpf? (released,developer,perl,functions,)
   MANPAGE      Date::Parse - Parse date strings into time values
   INST_FILE    /opt/local/lib/perl5/vendor_perl/5.8.9/Date/

So you want the TimeDate module.

Step 3: Check that port is not available

Perl modules have names of the form p5-modulename.

$ port search p5-timedate

This particular port actually is available, but we'll pretend it isn't.

Step 4: Create MacPorts directory

$ mkdir ~/macports
$ cd ~/macports
$ curl -O
$ chmod u+x cpan2port

Step 5: Create the port file

$ ./cpan2port -t Date::Parse
trying to find dist in /Users/dtls/.cpan/build/TimeDate-1.20*
md5: 7da7452bce4c684e4238e6d09b390200
rmd160: 513094a2d2fb3678c1ad893abc28c94d344ec7b8
sha1: 6394cd243e05c3c2073fa73348a4ad90d8d5963e
creating perl/p5-timedate/Portfile

Step 6: Build the port

First you can review the portfile:

$ cd perl/p5-timedate
$ cat Portfile
# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id$

PortSystem      1.0
PortGroup       perl5 1.0

perl5.setup     TimeDate 1.20
platforms       darwin
maintainers     cpan2port
description     ASCII Date parser using regexp's
extract.suffix  .tar.gz
checksums sha1 6394cd243e05c3c2073fa73348a4ad90d8d5963e md5 7da7452bce4c684e4238e6d09b390200 rmd160 513094a2d2fb3678c1ad893abc28c94d344ec7b8

The build it:

$ port build
Warning: MacPorts running without privileges. You may be unable to complete certain actions (e.g. install).
--->  Computing dependencies for p5-timedate
--->  Fetching p5-timedate
--->  Attempting to fetch TimeDate-1.20.tar.gz from
--->  Attempting to fetch TimeDate-1.20.tar.gz from
--->  Attempting to fetch TimeDate-1.20.tar.gz from
--->  Attempting to fetch TimeDate-1.20.tar.gz from
--->  Verifying checksum(s) for p5-timedate
--->  Extracting p5-timedate
--->  Configuring p5-timedate
--->  Building p5-timedate

Step 7: Fix the Portfile

The Portfile as generated is in rough form and not ready for submission as a new or updated port. Please at least check and update the following in the Portfile:

  • Check if the port compiles any files or is simply straight perl code. If the latter, then uncomment the following line and delete the comment above it. Otherwise delete the lines entirely.
    # Uncomment this line if you know there will be no arch-specific code:
    #supported_archs     noarch
  • Determine and add the correct license. There is a commented license line that is common to many perl ports.
    #license             {Artistic-1 GPL}
  • Dependencies need to be put into an if block so that they only appear in the sub-ports, not the stub p5 port. See below as an example.
    if {${perl5.major} != ""} {
        depends_lib-append  port:p${perl5.major}-digest-md5 \
  • Check the appropriate perl versions supported by the port. If the dependencies exist for perl 5.18, then add it too.
    perl5.branches      5.8 5.10 5.12 5.14 5.16 5.18

Step 8: Read up on local port repositories

<- Back to the HOWTO section

Last modified 15 months ago Last modified on Jul 13, 2016, 1:17:50 PM