The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Module::CPANfile::Writer - Module for modifying the cpanfile


    use Module::CPANfile::Writer;

    my $writer = Module::CPANfile::Writer->new('cpanfile');
    $writer->add_prereq('Moo', '2.003004');
    $writer->add_prereq('Test2::Suite', undef, relationship => 'recommends');


Module::CPANfile::Writer lets you modify the version of modules in the existing cpanfile.

cpanfile is very flexible because it is written in Perl by using DSL, you can write comments and even code. Therefore, modifying the cpanfile is not easy and you have to understand Perl code.

The idea of modifying the cpanfile was inspired by App::CpanfileSliptop. This module uses PPI to parse and analyze the cpanfile as Perl code. But PPI depends XS modules such as Clone and Params::Util, so these modules are annoying to fatpack in one pure-perl script.

Module::CPANfile::Writer has no XS modules in dependencies because it uses Babble and PPR to parse (recognize) Perl code.


$writer = Module::CPANfile::Writer->new($file)
$writer = Module::CPANfile::Writer->new(\$src)

This will create a new instance of Module::CPANfile::Writer.

It takes the filename or the content of cpanfile as scalarref.


This will returns the content of modified cpanfile.

$writer->add_prereq($module, [$version, relationship => $relationship)

Modify the version of specified $module in cpanfile.

You can also pass $version to 0 or undef, this will remove the version requirement of $module.


Write the content of modified cpanfile to the $file.




Copyright (C) Takumi Akiyama.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Takumi Akiyama <>