我在Perl中進行了大量編程,並在想如果人們有一個他們使用並願意共享的「默認」模板Perl腳本。你有一個很好的Perl模板腳本嗎?
我開始複製一個具有Getopt
函數的舊腳本。我在想人們會做類似的事情嗎?
我在Perl中進行了大量編程,並在想如果人們有一個他們使用並願意共享的「默認」模板Perl腳本。你有一個很好的Perl模板腳本嗎?
我開始複製一個具有Getopt
函數的舊腳本。我在想人們會做類似的事情嗎?
正如人們所說,在模塊中有我的方法模板之前:use PMG::PMGBase;
和作爲emacs用戶的初始腳本escafolding,我有我的perl-insert-start和perl-add-get選項模板, :
(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模板腳本(見下文),並與調用它的運行命令的區域:
#!/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
}
礦井很簡單。
#!/usr/bin/perl
use Modern::Perl
當談到喜歡的東西getopt的,沒有我寫的,使其價值,同時具有更詳細的模板腳本中相當的共性。
當我需要一個用於許多類似腳本的基本模板時,我只需將類似的部分變成一個模塊。然後,該腳本簡化爲類似:
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上有這樣的框架,但我認爲這很容易做到這一點,並得到你n不用處理你不需要的部分就可以完成。
在我.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
這些無論如何。
也許是一個無知的問題,但爲什麼'\ r'而不是'\ n'? – Telemachus 2010-09-25 16:03:21
@Telemachus因爲這是什麼工程? Vim有時候可能是一個奇怪的野獸。 – 2010-09-25 17:50:27
這就是我沒有測試的結果。還有一件關於Vim我從來不知道(注意到,碰到過)的Vim。對於其他訪問者,請參閱http://stackoverflow.com/questions/71323/how-to-replace-a-character-for-a-newline-in-vim和http://stackoverflow.com/questions/71417/why -is-ra-newline-for-vim – Telemachus 2010-09-25 18:11:32
:C-u M-| :~/scripts/perl-start-template.pl
從Emacs的一個空白緩衝區選擇一個空格後我有兩個。一老一比一的包裝到Perl的一個班輪和第二,有更多的功能和例子,我經常發現有用的多一點:
#!/usr/bin/perl
# name_of_script ver 0.01 YYYYMMDD [email protected]
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 [email protected]
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__
如果我們將不得不POD代碼 後,如果您將「完成說,」 POD上方,然後__END__更有意義,我__END__通常只使用。
隨意按照你喜歡的方式破解這兩個。我做出良好的作風和做法,在這裏沒有索賠,(我有時短一個開始,從較長的一個塊粘貼,因爲我需要他們兩個代碼樣式的trolols結束了。)
的Bleh,偉大的建議,[但'Modern :: Perl'不夠...](https://rt.cpan.org/Public/Bug/Display.html?id=43061) – 2010-09-24 19:52:36
你可能想讓那個'#!/usr/bin/perl'或甚至'#!/ usr/bin/env perl'。 – 2010-09-24 19:55:58
@Evan - 這是主觀的 – Quentin 2010-09-24 20:11:30