2011-10-10 40 views
0

我想要perl編程的標準方式。我正在編寫perl腳本,但我需要在程序啓動之前編寫一些信息。這個組織和組織的目的是什麼,時間和日期以及文件位置...... e.t.c。例如用c語言編寫,我們這樣寫標準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 $ 
* 
****************************************************************************/ 
/* Include files:               */ 
#include "stdio.h" 

就像上面那樣,註釋用在c語言中。我需要在perl腳本之前編寫相同的描述。有沒有任何格式。我非常開心perl。

回答

4

實際上,有在你的腳本嵌入文檔中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

  • NAME
  • 內容簡介
  • 說明
  • OPTIONS
  • 另請參閱
  • BUGS
  • 作者
  • 版權

最重要的是,我也傾向於嵌入$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文檔的總覽部分。

+0

感謝您的回覆和建議。 – viswa

+0

[Module :: Starter :: PBP](http://p3rl.org/Module::Starter::PBP)提供了很好的模板。 – daxim

+0

@daxim - 謝謝。我知道我以前在某個地方見過,但在POD文檔中找不到它。 –

0

根據我的經驗,這在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不適合了與您現有無論如何格式塊註釋本身,而是行註釋。另外請記住,#!行(如果存在)需要是絕對是第一行文件的行,否則它將不會執行任何操作。

+0

好的謝謝你的回覆@Dave – viswa

+0

POD是很常見和標準的。我會看看Sodved回答 – Matteo

+0

@Matteo:是的。 OP詢問一個文件頭,我習慣於看到POD內嵌或結束,所以我錯過了這種可能性。 –

1

POD(或請參閱perldoc)?

在Perl社區中非常標準,在我的經驗中,它是一種很好且一致的方式來記錄腳本和模塊。

+0

感謝您的回覆和建議。 – viswa

5

我用戶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; 
+0

@soddved。非常感謝你。 – viswa