in reply to Re^2: Need Perl book advice
in thread Need Perl book advice
The odd thing about anew is, it was created with anew. Sounds like a chicken & egg sort of deal, but the hint to solve this riddle is in the version number ;-)
#!/usr/local/bin/perl -w anew use warnings; use strict; use Getopt::Std; $Getopt::Std::STANDARD_HELP_VERSION = 1; use Pod::Usage; # ======================================================== SETUP my $VERSION = v2.0.0; my %options; getopts('h', \%options); HELP_MESSAGE() if $options{'h'}; my $filename_chosen = $ARGV[0]; die "No target filename chosen. Try anew --help\n" unless $filename_chosen; my %templates = ( 'pm' => 'anew-mod.pm', 'pmo' => 'anew-obj.pm', 'pmf' => 'anewfunc.pm', 'pl' => 'anewmain.pl', 'c' => 'template.c', 'ly' => 'template.ly', 'htm' => 'template.htm', 'tex' => 'template.tex' ); # ========================================================= MAIN my ($extension_type) = ( $filename_chosen =~ m/\.([^\.]*)$/ ); my $extension; if ($extension_type) { die "Invalid extension. Try anew --help\n" unless $templates{$extension_type}; ($extension) = ( $templates{$extension_type} =~ m/\.([^\.]*)$/ ); } else { $extension = ''; $extension_type = 'pl'; } my $inputfile = $templates{$extension_type}; $filename_chosen =~ s/\.([^\.]*)$//; my $outputfile = $filename_chosen; $outputfile = "$outputfile.$extension" if $extension; print "Creating $outputfile from $inputfile . . . .\n"; die "** ERROR ** File exists ** This program never overwrites.\n" if (-f $outputfile); $inputfile = "/home/mccosar/share/anew/$inputfile"; open MODEL, "< $inputfile" or die "Couldn't open $inputfile : $!"; my @model = <MODEL>; close MODEL; open TARGET, "> $outputfile" or die "Can't open $outputfile : $!"; foreach (@model) { s/FILENAME/$outputfile/; print TARGET $_; } close TARGET; print "File created. Changing permissions . . . .\n"; my $perm = '600'; $perm = '700' if ($extension_type eq 'pl'); print `chmod -v $perm $outputfile` or die "Oops -- unable to chmod $perm $outputfile: $!"; print "Done.\n"; # ========================================================= SUBS sub HELP_MESSAGE { pod2usage ( { -exit_status => 0, -verbose_level => 1 } ); } sub VERSION_MESSAGE { return 1 if (lc $ARGV[0] eq "--help"); print "This program is $0, "; printf "version %vd\n", $VERSION; printf "(Using Perl v%vd)\n", $^V; exit 0; } __END__ =head1 NAME ~/bin/anew =head1 SYNOPSIS anew (filename.ext) Creates a new file from an appropriate template, based on ext: .pl or none : executable perl file .pm : perl module .pmf : perl module (.pm), functional .pmo : perl module (.pm), objective .c : c source code .ly : GNU Lilypond leadsheet .htm : Elementary html document .tex : Basic .tex document With no extension specified, anew assumes you want an executable perl file. =head1 OPTIONS =over 4 =item -h, --help Prints synopsis and options, then exits. =item --version Prints version information and exits. =back =head1 AUTHOR Bruce H. McCosar. =head1 COPYRIGHT Copyright 2004 by Bruce H. McCosar. All Rights Reserved. This program is free software. You may copy or distribute it under the same terms as Perl itself.
And, of course, once you post code in public, you become acutely aware of the hacks . . . . but the important thing is, I wrote it and it works.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Need Perl book advice
by roju (Friar) on Jul 31, 2004 at 21:18 UTC | |
by Ambidangerous (Scribe) on Jul 31, 2004 at 22:45 UTC |