Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

script-starter

by polettix (Vicar)
on May 02, 2006 at 19:33 UTC ( #546975=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility Scripts
Author/Contact Info Flavio Poletti (flavio at polettix dot it)
Description: A template for creating new scripts, much in the Module::Starter spirit, together with a script that filters the template to actually create the new script. Ok, it's simpler than what I've described!

The script has documentation, but it's not based on the template - just because the template evolved with time (e.g. english translation).

Update: followed clever suggestions from chanio.

Update: followed clever suggestion from http://perlbuzz.com/2009/11/the-horrible-bug-your-command-line-perl-program-probably-has.html.

This is the template (living in Linux, I put it into $HOME/.module-starter/script.tpl):
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use Pod::Usage qw( pod2usage );
use Getopt::Long qw( :config gnu_getopt );

use version; my $VERSION = qv('0.0.1');

my %config;
GetOptions(\%config, 'usage', 'help', 'man', 'version')
   or pod2usage(-verbose => 99, -sections => 'USAGE');;
pod2usage("$0 v$VERSION") if $config{version};
pod2usage(-verbose => 99, -sections => 'USAGE') if $config{usage};
pod2usage(-verbose => 99, -sections => 'EXAMPLES|USAGE|OPTIONS')
  if $config{help};
pod2usage(-verbose => 2) if $config{man};

# Other recommended modules (uncomment to use):
#  use IO::Prompt;
#  use Readonly;
#  use Data::Dumper;
#  use Log::Log4perl qw( :easy );

# Script implementation here

__END__

=head1 NAME

<SCRIPT-NAME> - [A line to describe the script]


=head1 VERSION

See version at beginning of script, variable $VERSION.

=head1 EXAMPLES

   shell$ <SCRIPT-NAME>

=for the author:
   Some brief example to show the most common usage for the script. Th
+is
   will probably be the most read section, so include meaningful
   examples.
  
=head1 USAGE

   <SCRIPT-NAME> [--usage] [--help] [--man] [--version]

=for the author:
   Include a complete usage block of text, like any other good command
  
=head1 DESCRIPTION

=for the author:
   A complete description of the script and its characteristics, with 
+a
   possible structure into deeper sections (via =head2, =head3). Leave
   options description for the next section!


=head1 OPTIONS

=for the author
   A description of all the available options that the script accepts.
   The pre-defined ones are already included, of course.

=over

=item --usage

print a concise usage line and exit.

=item --help

print a somewhat more verbose help, showing usage, this description of
the options and some examples from the synopsis.

=item --man

print out the full documentation for the script.

=item --version

print the version of the script.

=back

=head1 DIAGNOSTICS

=for the author
   Include all error messages and possible exit conditions, even those
   that "should never happen".


=over

=item C<< Error message here, perhaps with %s placeholders >>

[Description of first error...]

=item C<< Another error message here >>

[Description of another error...]

[... and so on...]

=back


=head1 CONFIGURATION AND ENVIRONMENT

=for the author
   Describe any configuration file that is used by the script, and
   any environment variable that affects the its behaviour. Include
   details about the position of the files, their formats, etc.
  
<SCRIPT-NAME> requires no configuration files or environment variables
+.


=head1 DEPENDENCIES

=for the author
   A list of all modules the script is based for, together with an
   indication of their version and the required perl version.

None.


=head1 BUGS AND LIMITATIONS

=for the author
   A list of all known problem about the script, with an indication of
   when (and if) they will be eventually solved. Also include a
   description of all feature restrictions and limitations.

No bugs have been reported.

Please report any bugs or feature requests to the AUTHOR


=head1 AUTHOR

Flavio Poletti C<flavio [at] polettix.it>


=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006, Flavio Poletti C<flavio [at] polettix.it>. All rig
+hts reserved.

This script is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>
and L<perlgpl>.


=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WH
+EN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. TH
+E
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.

=cut

This is the script that creates a new script based on the template above, I named it script-starter
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use Pod::Usage ();

use version; my $VERSION = qv('0.0.1');
my $original = "$ENV{HOME}/.module-starter/script.tpl";

# Other recommended modules (uncomment to use):
#  use IO::Prompt;
#  use Perl6::Export;
#  use Perl6::Slurp;
#  use Perl6::Say;
#  use Regexp::Autoflags;
#  use Readonly;

# Script implementation here
my $script_name = shift;
if (! defined $script_name ) {
   Pod::Usage::pod2usage(-verbose => 1);
   exit 0;
}
if ($script_name =~ /-h|--help/) {
   Pod::Usage::pod2usage(-verbose => 2);
   exit 0;
}
croak "file '$script_name' already exists, stopped" if -e $script_name
+;
open my $input_fh, '<', $original
  or croak "open() for original '$original': $!";
open my $output_fh, '>', $script_name
  or croak "open() on '$script_name': $!";
while (<$input_fh>) {
   s/<SCRIPT-NAME>/$script_name/g;
   print {$output_fh} $_;
}
close $input_fh;
close $output_fh;

__END__

=head1 NAME

script-starter - create the base for a new script


=head1 VERSION

This document describes script-starter version 0.0.1


=head1 SYNOPSIS

   # Ask for help
   shell$ script-starter
   
   # Generate a new script from the template
   shell$ script-starter nome-script

  
=head1 DESCRIPTION

This script helps to create other scripts, in a way much similar to
module-starter. It takes the $original script (see start of script)
and copies to the destination one, setting the script name on the
fly.


=head1 INTERFACE

The script requires only one argument, which should be a valid
filename for a non-existing file.

When called without options, it prints the examples in the L<SYNOPSIS>
+.
When called with -h/--help, it perldoc-s the script to show this
help page.


=head1 DIAGNOSTICS

=over

=item C<< file '$script_name' already exists, stopped at... >>

The name of the script to be generated refers to an already existing f
+ile.
This is not allowed, script-starter won't let you shoot at your feet s
+o
easily. Note that there is still space for a race condition between th
+e
existence test and the actual file open for writing, be sure to aim we
+ll.

=item C<< open() for original '/path/to/template': <specific error> >>

There was a problem opening the template file.

=item C<< open() on '/path/to/target-script': <specific error> >>

There was a problem opening the target file.

=back


=head1 CONFIGURATION AND ENVIRONMENT

Given the fact that I'll never publish this script, it can be easily
tweaked modifing the C<$original> variable at the beginning, which
points to the template model.


=head1 DEPENDENCIES

A few:

=over

=item -

version

=item -

Pod::Usage

=back


=head1 INCOMPATIBILITIES

None reported.


=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests through http://rt.cpan.org/


=head1 AUTHOR

Flavio Poletti C<< flavio@polettix.it >>


=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006, Flavio Poletti C<< flavio@polettix.it >>. All righ
+ts reserved.

This script is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>
and L<perlgpl>.

Questo script è software libero: potete ridistribuirlo e/o
modificarlo negli stessi termini di Perl stesso. Vedete anche
L<perlartistic> e L<perlgpl>.


=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WH
+EN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. TH
+E
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.

=head1 NEGAZIONE DELLA GARANZIA

Poiché questo software viene dato con una licenza gratuita, non
c'è alcuna garanzia associata ad esso, ai fini e per quanto permesso
dalle leggi applicabili. A meno di quanto possa essere specificato
altrove, il proprietario e detentore del copyright fornisce questo
software "così com'è" senza garanzia di alcun tipo, sia essa espress
+a
o implicita, includendo fra l'altro (senza però limitarsi a questo)
eventuali garanzie implicite di commerciabilità e adeguatezza per
uno scopo particolare. L'intero rischio riguardo alla qualità ed
alle prestazioni di questo software rimane a voi. Se il software
dovesse dimostrarsi difettoso, vi assumete tutte le responsabilità
ed i costi per tutti i necessari servizi, riparazioni o correzioni.

In nessun caso, a meno che ciò non sia richiesto dalle leggi vigenti
o sia regolato da un accordo scritto, alcuno dei detentori del diritto
di copyright, o qualunque altra parte che possa modificare, o redistri
+buire
questo software così come consentito dalla licenza di cui sopra, potr
+à
essere considerato responsabile nei vostri confronti per danni, ivi
inclusi danni generali, speciali, incidentali o conseguenziali, deriva
+nti
dall'utilizzo o dall'incapacità di utilizzo di questo software. Ciò
include, a puro titolo di esempio e senza limitarsi ad essi, la perdit
+a
di dati, l'alterazione involontaria o indesiderata di dati, le perdite
sostenute da voi o da terze parti o un fallimento del software ad
operare con un qualsivoglia altro software. Tale negazione di garanzia
rimane in essere anche se i dententori del copyright, o qualsiasi altr
+a
parte, è stata avvisata della possibilità di tali danneggiamenti.

Se decidete di utilizzare questo software, lo fate a vostro rischio
e pericolo. Se pensate che i termini di questa negazione di garanzia
non si confacciano alle vostre esigenze, o al vostro modo di
considerare un software, o ancora al modo in cui avete sempre trattato
software di terze parti, non usatelo. Se lo usate, accettate espressam
+ente
questa negazione di garanzia e la piena responsabilità per qualsiasi
tipo di danno, di qualsiasi natura, possa derivarne.

=cut

Comment on script-starter
Select or Download Code
Re: script-starter
by chanio (Priest) on May 04, 2006 at 01:39 UTC

    Tante Grazie, Flavio!

    You should consider polishing it a little and putting it at the tutorials.

    • For example, using $ENV{HOME} instead of your home name ;) .
    • Or naming the template with a .tpl extension (4 example) .
    It is a good starting point for most 'well intentioned' perl programers. I am never sure of what is the standard way of presenting a finished script.

    I will, also, copy your template at my .SciTE macros. I find it easier to fill-in data than to start a new script from cero.

    After installing it as you said, I added this lazy way of using it at LINUX...

    >SNAME=$(/usr/bin/Xdialog --stdout --title "(Path/)New Script's Name.. +." --inputbox "/full/script/path/newscript.pl" 0 0 "$HOME/program/scr +ipts/newSname.pl");/usr/bin/perl $HOME/program/script/script-starter. +pl $SNAME ; /usr/bin/scite $SNAME
    This bash one-liner, asks the full new script's name with an Xdialog window. Then, it calls your template-copier and finally, it opens the new script with SCITE. Some day, it could become something better, but by now, it does just what I need.

    Thanks, again!

    alberto

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://546975]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2014-11-23 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls