【问题标题】:standard perl program representation标准 perl 程序表示
【发布时间】:2011-10-10 13:08:51
【问题描述】:

我想要标准的 perl 编程方式。我正在编写 perl 脚本,但我需要在程序启动之前编写一些信息。这个脚本和组织的目的是什么以及时间和日期以及文件位置...e.t.c.例如在 c 语言中我们是这样写的

  /*!
 *****************************************************************************
 *
 * @file code.c
 * @brief
 * @b Description: temperature measurement
 * @ university of east london, All rights reserved.
 *
 * $Header: //data/name/source/code.c#10 $
 * $DateTime: 2011/01/18 16:06:25 $
 *
 ****************************************************************************/
 /* Include files:                                                           */
 #include "stdio.h"

和上面一样,cmets 用在 c 语言中。我需要在 perl 脚本之前写下相同的描述。有没有什么格式。我是 perl 的初学者。

【问题讨论】:

    标签: perl


    【解决方案1】:

    我使用 perl 的内置文档。吊舱

    http://perldoc.perl.org/perlpod.html

    我要写的一个简单的类是

    #!/bin/perl
    #---------------------------------------------------------------------------------
    =head1 Temperature.pm
    
    This class records temperatures ant converts between celcius and ferenheit
    
    university of east london, All rights reserved.
    
        $Id$
        $Url$
    
    =head2 Constants
    
    =over
    
    =item Temperature units
    
    These constants are used to indicate the units temperature is recorded in.
    
    =cut
    #---------------------------------------------------------------------------------
    use constant UNIT_CELSUIS   => 1;
    use constant UNIT_FARENHEIT => 2;
    
    #---------------------------------------------------------------------------------
    =item Attribute indexes
    
    Our object is an array ref, so these private constants are the indexes
    of the attributes of our class
    
    =cut
    #---------------------------------------------------------------------------------
    my $IDX_DEGREES = 0;
    my $IDX_UNITS   = 1;
    
    #---------------------------------------------------------------------------------
    =back
    
    =head2 Methods
    
    =over
    
    =item new
    
    This is the constructor creates the object. Default is 0 degrees celsius
    
    =cut
    #---------------------------------------------------------------------------------
    sub new
    {
        my( $class, $degrees, $units ) = @_;
        my $self = bless( [], ref($class) || $class );
        $self->[$IDX_DEGREES] = $degrees || 0;
        $self->[$IDX_UNITS]   = $units   || UNIT_CELSIUS;
        return $self;
    } # END new
    
    #---------------------------------------------------------------------------------
    =item asCelsius
    
    Returns the temperature in degrees celsius
    
    =cut
    #---------------------------------------------------------------------------------
    sub asCelsius
    {
        my( $self ) = @_;
        if( $self->[$IDX_UNITS] == UNIT_CELSIUS )
        {
            return $self->[$IDX_DEGREES];
        }
        else
        {
            return ( $self->[$IDX_DEGREES]  − 32 ) * (5⁄9);
        }
    } # END as Celsius
    
    #---------------------------------------------------------------------------------
    =back
    
    End of module
    
    =cut
    #---------------------------------------------------------------------------------
    1;
    

    【讨论】:

      【解决方案2】:

      实际上 Perl 中有一个标准可以在脚本中嵌入文档,但这并不是您想的那样。看看Perl's POD format。这是将文档嵌入 Perl 程序的标准方式。您可以使用perldoc 程序查看此文档:

      $ perldoc myscript.pl
      

      而且,您可以使用各种 pod2xxx 命令来格式化此信息:

      $ pod2html myscript.pl > myscript.html  #HTML format
      $ pod2text myscript.pl > myscript.txt   #Text format
      $ pod2wiki myscript.pl > wikitext.txt   #For embedding into various Wikis (not part of std Perl dist)
      

      POD 格式非常简单易学。最难理解的是,每个命令和节之间必须有一个空行。

      这是错误的:

       =pod
       =head1 PROGRAM NAME
       myscript.pl
       =head1 DESCRIPTION
       My Program is nice.
       =head1 SYNOPSIS
       My program does things
      

      改为:

       =pod
      
       =head1 PROGRAM NAME
      
       myscript.pl
      
       =head1 DESCRIPTION
      
       My Program is nice.
      
       =head1 SYNOPSIS
      
       My program does things
      

      另请参阅 Pod StylePod Spec

      您在CPAN page 中看到的所有信息都是由嵌入在模块中的 POD 生成的。与 ActiveState 的 ActivePerl 文档相同。

      POD 格式通常与 MANPAGES 格式相同。所以你会有以下部分作为=head1

      • 姓名
      • 概要
      • 描述
      • 选项
      • 另见
      • 错误
      • 作者
      • 版权所有

      除此之外,我还倾向于嵌入一个 $USAGE 变量,以显示该命令的使用方式:

      my $USAGE =<<USAGE;
          myscript.pl -foo <foo> [-bar <bar>] <barfoo>
      
          or
      
          myscript.pl -help
      USAGE
      
      [...]
      
      if ($help) {
         say $USAGE;
         exit 0;
      }
      

      但是,这确实不是必需的,因为您可以使用 Pod::Usage 模块(它是标准 Perl 发行版的一部分)打印出我们 Pod 文档的 SYNOPSIS 部分。

      【讨论】:

      • 感谢您的回复和建议。
      • Module::Starter::PBP 提供了不错的模板。
      • @daxim - 谢谢。我知道我以前在某处看到过,但在 POD 文档中找不到。
      【解决方案3】:

      POD 怎么样(或参见perldoc)?

      在 Perl 社区中几乎是标准的,根据我的经验,它是记录脚本和模块的一种很好且一致的方式。

      【讨论】:

        【解决方案4】:

        根据我的经验,这在 Perl 世界中并不常见,因此,不,我无法告诉您使用任何格式。不过,只需稍作修改,即可轻松使用现有格式:

        #!/usr/bin/env perl
        
         #############################################################################
         #
         # @file code.c
         # @brief
         # @b Description: temperature measurement
         # @ university of east london, All rights reserved.
         #
         # $Header: //data/name/source/code.c#10 $
         # $DateTime: 2011/01/18 16:06:25 $
         #
         #############################################################################
        
        use strict;
        use warnings;
        

        请注意,Perl 本身没有块 cmets,但 line cmets 无论如何都适合您现有的格式。另请记住,#! 行(如果存在)必须是文件的绝对第一行,否则它不会执行任何操作。

        【讨论】:

        • POD 非常常见和标准。我会看看 Sodved 的答案
        • @Matteo:是的。 OP 在询问文件头,我习惯于看到 POD 内联或最后,所以我错过了这种可能性。
        猜你喜欢
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        • 1970-01-01
        • 2012-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多