我应该在我的Perl程序的入门模板中添加什么?

时间:2009-12-03 13:20:28

标签: perl

我基于以下模板/骨架的Perl脚本占主导地位:

#!/usr/bin/perl -w

use strict;
use utf8;

$| = 1;
binmode(STDOUT, ":utf8");

# my code goes here.

此模板实现的目标:

  1. 启用警告(-w
  2. 启用严格模式(use strict
  3. 使用纯UTF-8(use utf8 + binmode(STDOUT, ":utf8")
  4. 禁用缓冲($| = 1
  5. 我的问题是:

    如何改进我的模板以更好地反映Perl最佳实践?

10 个答案:

答案 0 :(得分:18)

-w替换为use warnings。它允许您在需要时以词汇方式禁用警告。请参阅perllexwarn

use utf8编译指示适用于源代码为UTF-8的情况。如果是的话,很棒。如果不是......我不建议添加您实际不使用的东西。同样,除非您实际生成STDOUT,否则不要将STDOUT设置为UTF-8。

禁用缓冲会降低性能。除非您需要,否则不要这样做,然后将范围限制在必要的范围内。

我喜欢包含一个语句来明确说明运行脚本所需的最低Perl版本。如果由于某人使用旧版本的Perl而无法编译错误消息,则会使错误消息更有意义。 e.g。

BEGIN { require 5.00801 }

我使用那个特定的咒语而不是use v5.8.1之类的东西,因为它与Perl的版本向后兼容我试图“支持”一个有意义的错误信息。

答案 1 :(得分:11)

这是我的,虽然我必须承认,有时我只是在不使用模板的情况下开始打字。我将其设置为modulino,以便以后添加测试很容易:

#!perl
package App::XXX;

use utf8;
use 5.010;

use strict;
use warnings;
use vars qw($VERSION);

$VERSION = '0.01_01';

__PACKAGE__->run( @ARGV ) unless caller;

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


    }

1;

如果您想自动将所有文件句柄设置为某些编码,可以添加open编译指示:

 use open IO => ':utf8';

我有另一个文档,我稍后会添加。

此外,有些人将编辑序列添加为脚本顶部或底部的注释。或许:

# -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
# vim: ts=4 sts=4 sw=4:

由于我将脚本放入发行版中,因此安装过程会自动修复shebang行,因此无论我放在那里都无关紧要。

答案 2 :(得分:8)

如何投入一些文档?

=head1 NAME

name here

=head2 SYNOPSIS

short synopsis here

答案 3 :(得分:4)

并非这样可以在模板中实现,但您可以通过强制开发人员在所有源代码上运行perltidyperlcritic来强制实施perl最佳实践。如果你问我,还必须阅读Perl Best Practices

至于你的模板,你唯一应该考虑改变的是内置binmode函数之后的括号,因为它们不是必需的(这是书中许多建议之一)。

答案 4 :(得分:3)

将解释器行更改为

#!/usr/bin/env perl

这会阻止您使用-w,因此您还必须

use warnings;

答案 5 :(得分:2)

您可能想要了解的另一件事是CPAN上的模块工具包。由Damian Conway创建,它允许您组装一组常用模块,然后使用单个“使用”行而不是几个。而不是

use strict;
use warnings;
use foo;
use bar;

你有;

use Toolkit;

您放入工具箱的所有模块都可用。

答案 6 :(得分:2)

我想说你想在你的模板中加入一套标准的POD文档,你可以在CPAN上看到大多数模块的正常标题集。我喜欢在最后添加它,但你可能喜欢它在顶部甚至穿插代码。例如最后:

1;
__END__

=head1 NAME

My::Foo - The great new My::Foo!

=head1 VERSION

Version 0.01

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS 

=head2 new

=head1 AUTHOR

=head1 BUGS

=head1 ACKNOWLEDGEMENTS

=head1 COPYRIGHT & LICENSE

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

See http://dev.perl.org/licenses/ for more information.

=cut

我发现编写模块就好像我要将它发布到CPAN,即使我从来不是一个好主意。如需更多智慧和最佳实践,请查看module-startersudo cpan Module::Starter。这个方便的小工具将为您构建一个完整的模块框架,这对于浏览来说是最有启发性的。 e.g。

$ module-starter --module=My::Foo --author=markp --email=markp@example.com --mb
$ tree My-Foo

答案 7 :(得分:1)

我从不在我的.pl脚本中使用shebang行,也不使用use lib /path/to/special/lib,以便为每次调用自定义这些行:

perl -I/path/to/special/lib myscript.pl

/usr/local/perl5.10 -I/different/path/to/lib myscript.pl

当然,每个文件都以:

开头
use strict;
use warnings;

编辑:我想到了一些我最近开始使用的东西:

# at least while the project is in initial development, so as to
# expose more places where the module might die; maybe turn off
# in production, depending on what other error handling is in place
use autodie;

# ...after all other 'use' lines at the top of a module: remove all
# unwanted imports cluttering up our namespace
use namespace::autoclean;

答案 8 :(得分:1)

虽然不是我所做的所有Perl都适用于网络,但它经常发生,我不得不添加:

use CGI::Carp qw(fatalsToBrowser);

到我的开发模板,一旦它投入生产我就会注释掉。

答案 9 :(得分:1)

默认情况下,您希望关闭缓冲是没有意义的。