2010-09-24 14 views
5

मैं पर्ल में बहुत सारे प्रोग्रामिंग करता हूं और सोच रहा था कि क्या लोगों के पास "डिफ़ॉल्ट" टेम्पलेट पर्ल स्क्रिप्ट है जिसका उपयोग वे करते हैं और साझा करने के इच्छुक हैं।क्या आपके पास एक अच्छा पर्ल टेम्पलेट स्क्रिप्ट है?

मैंने अपनी पुरानी स्क्रिप्ट्स में से एक को कॉपी करना शुरू किया जिसमें Getopt फ़ंक्शन हैं। मुझे लगता है कि लोग कुछ ऐसा ही करेंगे?

उत्तर

5

जैसा कि लोग कहते हैं कि मेरे पास मॉड्यूल में मेरे तरीके टेम्पलेट्स हैं: use PMG::PMGBase; और प्रारंभिक स्क्रिप्ट escafolding के लिए, एक 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") 
) 

थोड़ा थकाऊ है। तो पर अंत मुझे एक पर्ल टेम्पलेट स्क्रिप्ट (नीचे देखें) के लिए, और से कॉल करने के लिए आसान है चलाने के आदेश-ऑन-क्षेत्र:

#!/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

मेरा बहुत आसान है।

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

यह getopt तरह बातें की बात आती है, वहाँ लिपियों मैं इसके लायक बनाने के लिए, जबकि एक और अधिक वर्बोज़ टेम्पलेट होने लिखने के बीच काफी समानताएं नहीं हैं।

+1

ब्लेह, महान सुझाव , [लेकिन 'आधुनिक :: पर्ल' पर्याप्त नहीं है ...] (https://rt.cpan.org/Public/Bug/Display.html?id=43061) –

+0

आप इसे' # बनाना चाहते हैं!/usr/bin/perl' या यहां तक ​​कि '#!/usr/bin/env perl'। –

+1

@Evan - यह व्यक्तिपरक – Quentin

6

जब मुझे कई समान स्क्रिप्ट के लिए मूल टेम्पलेट की आवश्यकता होती है, तो मैं समान मॉड्यूल को मॉड्यूल में बदल देता हूं।

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 { ... } 

... 

इस तरह की चीज़ के लिए सीपीएएन पर कुछ ढांचे हैं, लेकिन मुझे लगता है कि यह स्वयं करना आसान है और आप वास्तव में क्या प्राप्त करते हैं जिन हिस्सों की आपको आवश्यकता नहीं है उनसे निपटने के बिना ईड।

7

मेरी .vimrc फ़ाइल में मैं

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

जो

#!/usr/bin/perl 

use strict; 
use warnings; 

लिखते हैं किसी भी नए पर्ल स्क्रिप्ट किया है। मैं भी

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

मॉड्यूल के लिए है, लेकिन मैं वैसे भी उन लोगों के लिए Module::Starter इस्तेमाल करते हैं।

+0

शायद एक अज्ञानी सवाल है, लेकिन '\ n' के बजाय' \ r' क्यों? – Telemachus

+5

@Telemachus क्योंकि यह वही काम करता है? कभी-कभी विम एक अजीब जानवर हो सकता है। –

+0

यही वह है जो मुझे परीक्षण के लिए नहीं मिलता है। फिर भी विम के बारे में एक और बात मुझे पहले कभी नहीं पता था (ध्यान दिया गया था)। अन्य आगंतुकों के लिए, http://stackoverflow.com/questions/71323/how-to-replace-a-character-for-a-newline-in-vim और http://stackoverflow.com/questions/71417/why देखें -इस-आरए-न्यूलाइन-फॉर-वीम – Telemachus

0

: C-u M-| :~/scripts/perl-start-template.pl Emacs से एक खाली बफर में एक अंतरिक्ष चयन करने के बाद मेरे पास दो है। एक पुरानी एक है जो एक पर्ल एक लाइनर के लिए एक आवरण और एक दूसरे अधिक कार्य करता है और उदाहरण है कि मैं अक्सर उपयोगी पाते है कि छोटे से अधिक है:

#!/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__ 

__END__ आमतौर पर केवल प्रयोग किया जाता है अगर हम कोड के बाद पॉड के लिए आप के लिए कदम "हो गया है कि" पॉड ऊपर तो __END__ अधिक भावना मेरे लिए बनाता है जा रहे हैं।

जितना चाहें उतना ही इन दोनों को हैक करने के लिए स्वतंत्र महसूस करें। मैं यहां अच्छी शैली या प्रथाओं का कोई दावा नहीं करता हूं, (और मैं कभी-कभी छोटे से शुरू करता हूं और लंबे समय से ब्लॉक में पेस्ट करता हूं क्योंकि मुझे ट्रोलॉल्स के लिए दो कोड शैलियों के साथ समाप्त होने की आवश्यकता होती है।)

संबंधित मुद्दे