What I need to do:

- add possibility for the user to cluster frequently used modules/libraries with tags (thinking about using hashes)

A hash of hash references looks like a good idea. Use arrays in the third level to allow more than one library per tag:

my %langTags=( perl => { HEAD => [ '#!/usr/bin/perl', ], DEFAULT => [ 'use strict;', 'use warnings;', ], cgi => [ 'use CGI::Carp qw( fatalsToBrowser );', 'use CGI qw( -nosticky -compile -no_debug -private_tempfil +es -newstyle_urls );', ], moose => [ 'use Moose;' ], # ... }, bash => { HEAD => [ '#!/bin/bash', ], cgi => [ 'source /usr/local/lib/bash/bashcgi-lib.bash', ], csv => [ 'source /usr/local/lib/bash/csv-lib.bash', ], ourlibs => [ 'source /usr/local/lib/bash/foo-lib.bash', 'source /usr/local/lib/bash/bar-lib.bash', ], # ... }, c => { DEFAULT => [ '#include <stdio.h>', '#include <stdlib.h>', '#include <string.h>', ], # ... }, # ... );

Note that you could use pseudo-tags to define a file header, default libraries, and anything else that you may need.

Think about getopt for entering tags. Use Getopt::Long.

How should the command line look like?

Language and libraries as two parameters with values
newscript --lang=perl --use=cgi,moose,dbi foo.pl
newscript --lang=bash --use=cgi,csv,ourlibs foo.bash
Language as argument with value, one flag argument per library tag
newscript --lang=perl --cgi --moose --dbi foo.pl
newscript --lang=bash --cgi --csv --ourlibs foo.bash
Language and libraries as flag arguments
newscript --perl --cgi --moose --dbi foo.pl
newscript --bash --cgi --csv --ourlibs foo.bash
Implicit language, derived from output file name
newscript --cgi --moose --dbi foo.pl
newscript --cgi --csv --ourlibs foo.bash

I prefer "don't make me think" and "don't make me type".

Implicit language is not hard, just guess it from the file extension: .pl and .pm for Perl, .sh and .bash for bash, and so on. Without a file extension, require a language argument. Use a hash for default mappings:

my %extToLang=( pl => 'perl', pm => 'perl', c => 'c', h => 'c', sh => 'bash', bash => 'bash', ); my $filename; my $lang; # setup $filename and perhaps also $lang if (!$lang and ($filename=~/\.([^.]+)$/) { $lang=$extToLang{$1}; } $lang or die "Missing language parameter";

Library tags would be different for different languages, e.g. moose would make no sense for bash at all, so it should result in an error message. When you use Getopt::Long for the first form, you would check the library tags returned from GetOptions(). The language is known at this point, either from a --lang option, or from the file extension. When you use a flag argument for each library tag, you need to know all allowed tags before calling GetOptions(). For that, you have two options:

Legal values for the language are simply available as keys %langTags. Legal values for the library tags are available as keys %{$langTags{$language}}. A little bit of code is required to get a list of all library tags:

use List::MoreUtils qw ( uniq ); my @allTags=uniq map { keys %$_ } values %langTags;

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^4: RFC: newscript.pl , my very first script! by afoken
in thread RFC: newscript.pl , my very first script! by Darfoune

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.