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.

$jPxu=q?@jPxu?;$jPxu^=q?Whats?^q?UpDoc?;print$jPxu;

Replies are listed 'Best First'.
Re^4: Need Perl book advice
by roju (Friar) on Jul 31, 2004 at 21:18 UTC
    Seems to me that an equally important question would have been to ask for the templates too :).

      I know, but posting that much code at once, I felt sort of like the guy who's asked to do a wedding toast and goes on with a speech for half an hour. Here's two of the templates (out of eight, so there is a quantity of mercy here). The point would be to make your own, according to your own needs.

      First, the simplest perl program that implements the standard --help and --version options: anewmain.pl. (Notice I'm a firm believer in pod.)

      Second, in case any of you have ever seen GNU Lilypond, it is a cool music typesetting program. Although it isn't Perl, I had to do a lot of language learning before I could produce good copy with it. I'm proud of the result (template.ly). The syntax is similar to LaTeX:

      And there you go; 'anew' can be adapted as stated in the original article, according to your needs.

      $jPxu=q?@jPxu?;$jPxu^=q?Whats?^q?UpDoc?;print$jPxu;