Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w use strict; print "template v1.1 - General purpose templating system\n". " (c) 2003 - JaWi, janwillem.janssen\@lxtreme.nl\n\n"; my %config = configure( "/home/jawi/.templaterc" ); $config{ filename } = question( "Please give template (output) filenam +e" ); ( $config{ base_filename } ) = ( $config{ filename } =~ m/^([^.]+)/ ); my %templates = get_templates( $config{ templates } ); my $template_no = ask_for_template( %templates ); my @template = make_template( $templates{ $template_no }, %config ); my $output_file = $config{ filename }; if ( ! -e $output_file || question( "Overwrite? [y/n]" ) =~ /y(es)?/i +) { open OUTPUT, ">$output_file" or die "Can't write to $output_file...\ +n"; print OUTPUT foreach ( @template ); close OUTPUT; } ### END ### sub make_template { my ( $file, %config ) = @_; my @template; open TPL, "<$file" or die "Can't open template file!\n"; @template = <TPL>; close TPL; s/%(\w+)%/$config{lc $1}?$config{lc $1}:question(lc $1)/eig foreach ( @template ); @template; } # make_template sub ask_for_template { my ( %templates ) = @_; my @indexes = sort keys %templates; my $idx = 0; printf " [% 2d] %s\n", ++$idx, $indexes[ $idx - 1 ] foreach ( @inde +xes ); $idx = 0; while ( $idx < 1 || $idx > scalar @indexes ) { $idx = question( "Template number" ); } $indexes[ $idx - 1 ]; } # ask_for_template sub get_templates { my ( $template_dir ) = @_; my %template; while ( <$template_dir/*.tpl> ) { /^$template_dir\/([\w_.-]+)\.tpl$/ and $template{ $1 } = $_ if ( - +e && -R ); } %template; } # get_templates sub configure { my ( $config_file ) = @_; my %config = ( name => "Your full name", nick => "Your nick name", company => "Your company", email => "Your email address", templates => "Path to templates" ); if ( ! -e $config_file ) { print "First time configuration:\n\n"; $config{ $_ } = question( $config{ $_ } ) foreach ( keys %config ) +; open CONFIG, ">$config_file" or die "Can't write to $config_file.. +.\n"; print CONFIG "# template configuration file\n"; print CONFIG " $_ = $config{$_}\n" foreach ( keys %config ); close CONFIG; print "\n"; } elsif ( -R $config_file ) { open CONFIG, "<$config_file" or die "Can't read from $config_file. +..\n"; while ( <CONFIG> ) { next if /^#/ or /^\s*$/; my ( $key, $value ) = ( /\s*(\w+)\s*=\s*([^\n#]+)?/ ); $config{ $key } = $value if defined $config{ $key }; } close CONFIG; } else { die "Can't read from $config_file...\n"; } %config; } # configure sub question { printf( "%20s: ", $_[0] ); my $retval = <>; chomp $retval; $retval; } # question =pod =head1 Name template =head1 Description Simple templating system for source files. This program allows you to generate various files based on simple templates. For example, you could generate a basic Perl skeleton with your name, email and other information automatically filled in. =head2 Predefined variables There are currently several predefined variables defined for template. Variable names exist only of printable characters (that is a-z, 0-9, '.' and '_') and are wrapped with a leading and trailing percentage sign (%). =head3 %name%, %nick%, %company%, %email% These variables are set upon the first time you run template. It will expand to your name, nickname, company and email-address. =head3 %filename% This variable will expand to the full filename of the output file, that is, with extension. =head3 %base_filename% This variable will be replaced with the base name of the output filename (e.g. without extension(s)). This could be useful, for example, when you want to generate Java or C++ class files. =head2 Adding variables You can add your own variables by simply inserting them into your templates. Remember to embed them inside two percentage signs! If the program comes across a variable it doesn't know, it will prompt you for a value before continuing. =head1 Templates In principal every file can act as template file. The only restriction is that the extension ends in C<.tpl>. An example for a template providing a basic Perl snippet skeleton would look like this: #!/usr/bin/perl -w use strict; ### END ### Z<>=pod Z<>=head1 Name %base_filename% Z<>=head1 Description ... Z<>=head1 Updates ... Z<>=head1 Author %nick%, CZ<><%email%> Z<>=cut =head1 Todos Suggestion of template file based on the extension of the output file. Add some verbose input checking. =head1 Updates 2003-01-08 21:15 Fixed a minor bug regarding multiple variables on a single line. =head1 Author JaWi, C<janwillem.janssen@lxtreme.nl> =cut

In reply to template by JaWi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2022-10-06 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (26 votes). Check out past polls.

    Notices?