DEV Community

Yuki Kimoto - SPVM Author
Yuki Kimoto - SPVM Author

Posted on

How to parse cpanfile?

How to parse cpanfile? Module::CPANfile makes it possible.

use Module::CPANfile;

my $file = Module::CPANfile->load("cpanfile");
my $prereqs = $file->prereqs; # CPAN::Meta::Prereqs object

my @features = $file->features; # CPAN::Meta::Feature objects
my $merged_prereqs = $file->prereqs_with(@identifiers); # CPAN::Meta::Prereqs

$file->merge_meta('MYMETA.json');
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
ap profile image
Aristotle Pagaltzis • Edited

If all you want to do is process data from your own cpanfile then a useful simple option might be to just do or require it. All you need to do is define a few subs like requires and recommends first, which will be called a bunch of times by the cpanfile and given the dependency data. So the subs should collect it somewhere, and then you can do something with it afterwards.

If you do this you are running the cpanfile as code so you need to be able to trust it, obviously.

And you don’t want to use this as a general parsing approach because the full set of subs you need to define (all the configure_requires, configure_recommends, build_requires, build_recommends, etc variations) is probably excessive.

But if all you are doing is processing your own cpanfile then trust is not an issue and you almost certainly only use a few of the keywords, so this is a nice option.

Collapse
 
yukikimoto profile image
Yuki Kimoto - SPVM Author

If all you want to do is process data from your own cpanfile then a useful simple option might be to just do or require it.

This seems to be good way. I didn't know cpanfile is pure Perl codes until now.

Collapse
 
yukikimoto profile image
Yuki Kimoto - SPVM Author

Thank you for information.

But if all you are doing is processing your own cpanfile then trust is not an issue and you almost certainly only use a few of the keywords, so this is a nice option.

Yes.

Collapse
 
szabgab profile image
Gabor Szabo

You could add perl after the triple back-tick to have color highlighting:

use Module::CPANfile;

my $file = Module::CPANfile->load("cpanfile");
my $prereqs = $file->prereqs; # CPAN::Meta::Prereqs object

my @features = $file->features; # CPAN::Meta::Feature objects
my $merged_prereqs = $file->prereqs_with(@identifiers); # CPAN::Meta::Prereqs

$file->merge_meta('MYMETA.json');
Enter fullscreen mode Exit fullscreen mode
Collapse
 
yukikimoto profile image
Yuki Kimoto - SPVM Author

Gabor

Thanks. I haven't known this.

I will try this syntax.