У вас есть хороший шаблонный скрипт Perl? - PullRequest
5 голосов
/ 24 сентября 2010

Я много программирую на Perl, и мне было интересно, есть ли у людей шаблонный скрипт Perl "по умолчанию", который они используют и хотят поделиться.

Я начал копировать один из моих старых сценариев с функциями Getopt. Я думаю, что люди сделали бы что-то подобное?

Ответы [ 5 ]

7 голосов
/ 25 сентября 2010

В моем .vimrc файле у меня есть

au BufNewFile *.pl s-^-#!/usr/bin/perl\r\ruse strict;\ruse warnings;\r\r-

который пишет

#!/usr/bin/perl

use strict;
use warnings;

к любому новому скрипту Perl. У меня также есть

au BufNewFile *.pm s-^-package XXX;\r\ruse strict;\ruse warnings;\r\r1;-

для модулей, но я все равно использую Module::Starter для них.

6 голосов
/ 24 сентября 2010

Когда мне нужен базовый шаблон для многих похожих скриптов, я просто превращаю подобные части в модуль. Затем сценарий сводится к чему-то вроде:

 use App::Foo;

 App::Foo->run( @ARGV );

App::Foo наследуется от модуля шаблона и переопределяет все, что отличается:

 package App::Foo;
 use parent qw(App::Template);

 ...

В модуле App::Template вы вводите все, что вам нужно:

 package App::Template;

 sub run {
    my( $class, @args ) = @_;

    my $self = $class->new( ... );
    $self->init;
    $self->process_command_line( ... );

    ...
    }


 sub process_command_line { ... }

 ...

В CPAN есть некоторые платформы для такого рода вещей, но я думаю, что так же легко сделать это самостоятельно и получить именно то, что вам нужно, не имея дело с частями, которые вам не нужны.

5 голосов
/ 27 сентября 2010

Как уже говорили люди, у меня есть шаблоны методов в модуле: use PMG::PMGBase; и для начального эскафолдинга скрипта, как пользователь emacs, у меня есть шаблоны perl-insert-start и perl-add-getoption, но я пишу вещи как:

(defun perl-insert-start ()
  "Places #!..perl at the start of the script"
  (interactive)
  (goto-char (point-min))
  (insert "#!/usr/bin/env perl\n\n")
  (insert "=head1 [progam_name]\n\n")
  (insert " description:\n\n")
  (insert "=cut\n\n")
  (insert "use feature ':5.10';\n")
  (insert "use strict;\n")
  (insert "#use warnings;\n")
  (insert "#use Data::Dumper;\n")
)

немного утомительно. Поэтому в конце мне проще иметь шаблонный скрипт Perl (см. Ниже) и вызывать его с помощью команды run-command-on-region: C-u M-| :~/scripts/perl-start-template.pl из Emacs после выбора одного пробела в пустом буфере:

#!/usr/bin/env perl

=head1 [progam_name]

 description:

=cut

use feature ':5.10';
use strict;
use Getopt::Long;

my $prog = $0;
my $usage = <<EOQ;
Usage for $0:

  >$prog [-test -help -verbose]

EOQ

my $help;
my $test;
my $debug;
my $verbose =1;


my $ok = GetOptions(
                    'test'      => \$test,
                    'debug:i'   => \$debug,
                    'verbose:i' => \$verbose,
                    'help'      => \$help,
                   );

if ($help || !$ok ) {
    print $usage;
    exit;
}


print template();


sub template {
    ##
    ### Here start the template code
    ##
    return <<'EOT';
#!/usr/bin/env perl

=head1 [progam_name]

 description: This script prints a template for new perl scripts

=cut

use feature ':5.10';
use strict;
#use warnings;
#use Data::Dumper;
use Getopt::Long;
# use Template;
# use PMG::PMGBase;  
# use File::Temp qw/ tempfile tempdir /;
# use File::Slurp;
# use File::Copy;
# use File::Path;
# use File::Spec;
# use File::Basename qw(basename dirname);
# use List::Util qw(reduce max min);
# use List::MoreUtils qw(uniq indexes each_arrayref natatime);

# my $PMGbase = PMG::PMGBase->new();
my $prog = $0;
my $usage = <<EOQ;
Usage for $0:

  >$prog [-test -help -verbose]

EOQ

my $date = get_date();

my $help;
my $test;
my $debug;
my $verbose =1;

my $bsub;
my $log;
my $stdout;
my $stdin;
my $run;
my $dry_run;

my $ok = GetOptions(
                    'test'      => \$test,
                    'debug:i'   => \$debug,
                    'verbose:i' => \$verbose,
                    'help'      => \$help,
                    'log'       => \$log,
                    'bsub'      => \$bsub,
                    'stdout'    => \$stdout,
                    'stdin'     => \$stdin,

                    'run'       => \$run,
                    'dry_run'   => \$dry_run,

                   );

if ($help || !$ok ) {
    print $usage;
    exit;
}

sub get_date {

    my ($day, $mon, $year) = (localtime)[3..5] ;

    return my $date= sprintf "%04d-%02d-%02d", $year+1900, $mon+1, $day;
}

sub parse_csv_args {

    my $csv_str =shift;
    return [split ',', $csv_str];
}

EOT


}
1 голос
/ 24 сентября 2010

Мой довольно прост.

#!/usr/bin/perl
use Modern::Perl

Когда речь идет о таких вещах, как getopt, у сценариев, которые я пишу, недостаточно общего, чтобы иметь смысл, имея более подробный шаблон.

0 голосов
/ 03 января 2013

У меня есть два. Старый, который немного больше, чем обертка для однострочного perl, а второй содержит больше функций и примеров, которые я часто нахожу полезными:

#!/usr/bin/perl
# name_of_script ver 0.01 YYYYMMDD authors@email.address
use strict;
no strict "refs";


sub footer
{
    my $this_year=`date +%Y`; chop($this_year);
    print "Copyright 2003-$this_year You or Company\n";
    # This isn't how copyright works -  the dates cove the time when the code
    #  was created.
}

sub help
{
    print "Usage: $0\n";
    &footer;
exit(0);
}

if( ($ARGV[0] =~ /^-+h/i) || (!$ARGV[0]) )
{
    &help;
}
##### code


##### end of code
print "Done that\n";

exit(0);

Я использую вышеупомянутое для быстрого тестирования, но чаще я использую следующее (когда я не взламываю полный модуль).

#!/usr/bin/perl
# name_of_script ver 0.01 YYYYMMDD authors@email.address
use strict;
{
no strict "refs"; # this helps bypass frustration when I'm doing it wrong.
}

=head1 NAME

name_of_script

=head1 VERSION

0.01

=cut

our $VERSION = 0.01;

=head1 ABSTRACT

A synopsis of the new script

=head1 DESCRIPTION

Provide an overview of functionality and purpose of
this script

=head1 OPTIONS

%opt stores the global variables
%ignore overrides %opt

=cut

my (%opt,%ignore);

=head2 ARGS

=over 8

=item B<-h> send for help (just spits out this POD by default, but we can chose something else if we like 

=back

=head3 other arguments and flags that are valid

For when GetOpt is too heavy

-d -v -i[!] (value) 

=cut

for(my $args=0;$args<=(@ARGV -1);$args++){
    if ($ARGV[$args]=~m/^-+h/i){ &help; }
    elsif ($ARGV[$args] eq '-d'){ $opt{D}++; }
    elsif ($ARGV[$args] eq '-v'){ $opt{verbose}++;  print "Verbose output not implemented yet - try debug\n";}
    elsif ($ARGV[$args]=~m/-+i!(.+)/){ delete($ignore{$1}); }
    elsif ($ARGV[$args]=~m/-+record(.+)/){ $opt{record_data}++; }
    elsif ($ARGV[$args]=~m/-+w(ipe_home_dirs)?/){ $opt{wipe_home_dirs}++; }
    elsif ($ARGV[$args]=~m/-+i(.+)/){ $ignore{$1}=1; }
    elsif ($ARGV[$args]=~m/-+path(.+)/){ $opt{BASE_PATH} = $1; }
    elsif ($ARGV[$args]=~m/-+path/){ $args++; $opt{BASE_PATH} = $ARGV[$args]; }
    elsif ($ARGV[$args]=~m/-+dir(.+)/){ $opt{BASE_PATH} = $1; }
    elsif ($ARGV[$args] eq '-no-xml'||$ARGV[$args] eq '-no_xml'){ delete $opt{xml}; }
    elsif ($ARGV[$args] eq '-no-mkdir'||$ARGV[$args] eq '-no_mkdir'){ delete $opt{mkdir}; }
    elsif ($ARGV[$args] !~m/^-/ && -d "$ARGV[$args]"){ push @{ $opt{paths} }, $ARGV[$args] }
    else{ print "what is this $ARGV[$args] you talk of?\n"; &help; }
}


=head1 METHODS

=head3 footer

Adds the Copyright line to any output that needs it

=cut

sub footer { print "perldoc $0 \nCopyright 2011 You or Company\n"; }

=head3 help

Just the help output

=cut

sub help {
    print `perldoc $0`;
    #print "Usage: $0\n";
    #&footer;
    exit(0);
}

##### code


##### end of code

=head1 BUGS AND LIMITATIONS

There are no known problems with this script.
Please report any bugs or feature requests

=head1 SEE ALSO

#L<My::Modules>

=head1 MAINTAINER

is the AUTHOR

=head1 AUTHOR

Some Person, C<<some.person at example.com>>

=head1 LICENSE AND COPYRIGHT

Copyright 2011 Alexx Roche, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the terms of either: Eclipse Public License, Version 1.0 ; 
 the GNU Lesser General Public License as published 
by the Free Software Foundation; or the Artistic License.

See http://www.opensource.org/licenses/ for more information.

=cut

print "Done that\n" if $opt{verbose}>=1;
exit(0);
__END__

__ END__ обычно используется, только если после кода у нас будет POD Если вы переместите «Done that» над POD, тогда __END__ будет иметь для меня больше смысла.

Не стесняйтесь взламывать этих двоих столько, сколько захотите. Здесь я не претендую на хороший стиль или методы (и иногда начинаю с короткого и вставляю блоки из более длинного, так как они мне нужны, заканчивая двумя стилями кода для троллей)

...