| Dist-Zilla-Plugin-Prepender documentation | Contained in the Dist-Zilla-Plugin-Prepender distribution. |
Dist::Zilla::Plugin::Prepender - prepend lines at the top of your perl files
version 1.103470
In your dist.ini:
[Prepender]
copyright = 0
line = use strict;
line = use warnings;
This plugin will prepend the specified lines in each Perl module or program within the distribution. For scripts having a shebang line, lines will be inserted just after it.
This is useful to enforce a set of pragmas to your files (since pragmas are lexical, they will be active for the whole file), or to add some copyright comments, as the fsf recommends.
The module accepts the following options in its dist.ini section:
You can look for information on this module at:
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dist-Zilla-Plugin-Prepender
Jerome Quelin
This software is copyright (c) 2009 by Jerome Quelin.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
| Dist-Zilla-Plugin-Prepender documentation | Contained in the Dist-Zilla-Plugin-Prepender distribution. |
# # This file is part of Dist-Zilla-Plugin-Prepender # # This software is copyright (c) 2009 by Jerome Quelin. # # This is free software; you can redistribute it and/or modify it under # the same terms as the Perl 5 programming language system itself. # use 5.008; use strict; use warnings; package Dist::Zilla::Plugin::Prepender; BEGIN { $Dist::Zilla::Plugin::Prepender::VERSION = '1.103470'; } # ABSTRACT: prepend lines at the top of your perl files use Moose; use MooseX::Has::Sugar; with 'Dist::Zilla::Role::FileMunger'; # -- attributes # accept some arguments multiple times. sub mvp_multivalue_args { qw{ line } } has copyright => ( ro, default => 1 ); has _lines => ( ro, lazy, auto_deref, isa => 'ArrayRef[Str]', init_arg => 'line', default => sub { [] }, ); # -- public methods sub munge_file { my ($self, $file) = @_; return $self->_munge_perl($file) if $file->name =~ /\.(?:pm|pl)$/i; return $self->_munge_perl($file) if $file->content =~ /^#!(?:.*)perl(?:$|\s)/; return; } # -- private methods # # $self->_munge_perl($file); # # munge content of perl $file: add stuff at the top of the file # my %re = ( shebang => qr/^#!(?:.*)perl(?:$|\s.*$)/m, vimmode => qr/^#\s*(?:vim?|ex):.*$/m, emacsmode => qr/^#\s*-\*-[^\n]+?-\*-.*$/m, ); sub _munge_perl { my ($self, $file) = @_; my @prepend; # add copyright information if requested if ( $self->copyright ) { my @copyright = ( '', "This file is part of " . $self->zilla->name, '', split(/\n/, $self->zilla->license->notice), '', ); push @prepend, map { length($_) ? "# $_" : '#' } @copyright; } # add hand-written lines to prepend push @prepend, $self->_lines; my $prepend = join "\n", @prepend; # insertion point depends if there's a shebang line my $content = $file->content; if ( $content =~ /\A$re{shebang}\n(?:$re{vimmode}|$re{emacsmode})/ ) { # skip two lines $content =~ s/\A([^\n]+\n[^\n]+\n)/$1$prepend\n/; } elsif ( $content =~ /\A(?:$re{shebang}|$re{vimmode}|$re{emacsmode})/ ) { # skip one line $content =~ s/\n/\n$prepend\n/; } else { $content =~ s/\A/$prepend\n/; } $file->content($content); } __PACKAGE__->meta->make_immutable; no Moose; 1;
__END__