Here's what I've learned since posting snippet browser. There are numerous improvements based on bbfu's comments, in that thread, but lots of things done for which I hardly know the consequences. Criticism (as well as generosity) would be appreciated. (Trying the READMORE tag - here goes...nothing?)

It's more like a full program now - and I'm pleased to say, it took less time to write this, than it did to write the original version (thanks to obsessively reading all of you ).

N.B. again, that this probably only runs on Linux. Also, as in the previous version, as written it requires a $SANDBOX environment variable, to designate a subdirectory under $HOME.

Thank you for indulging a beginner using you all as a sounding board.

    New stuff revised Sat Jan 20 at 04:16
  1. cleaned up a few things following tilly's advice below (for which I'm very grateful).
  2. Improved subroutines for color.
  3. Change menu colors.
  4. You can now specify the editor, and a subroutine called which looks it up in your $PATH. Defaults to "less" if you enter something bogus, by calling which recursively.
  5. Has a menued console interface - with color!
  6. The menu is hideable.
  7. Arbitrarily select snippets by designating an array index.
  8. Change the Sandbox directory interactively (using a hidden option: type "SANDBOX".)
  9. Display a snippet to the screen or
  10. execute the snippet from the menu.
#!/usr/bin/perl -w use strict; my $EDITOR = 'elvis'; my $index = @ARGV ? shift(@ARGV) :0; my $clear = 1; my $hide_help = 0; my $homedir = $ENV{HOME}; my $sandbox = $ENV{SANDBOX}; my $STUDYPATH = "$homedir/$sandbox"; $EDITOR = which($EDITOR); my $color = colorize('GREEN'); my $reset = colorize('reset'); while (1){ my @files = glob "$STUDYPATH/*"; system ('clear') unless $clear == 0; $clear++; $index =~ s/[A-Za-z]/0/g; $index = 0 if $index < (0-$#files)-1 || $index > $#files; printf ( "%50s <- (%5s)\n", $files[$index], $index); print " ",$color,"[C]",$reset,"hange editor. Currently $EDITOR ",$color,"[E]",$reset,"dit to open the snippet in an editor ",$color,"[G]",$reset,"oto to select file index: -", ($#files+1)," + to $#files ",$color,"[H]",$reset,"elp to (un)hide this menu ",$color,"[N]",$reset,"ext to skip this snippet ",$color,"[R]",$reset,"edo to revisit the previous snippet co",$color,"[L]",$reset,"or to change the menu item color ",$color,"[Q]",$reset,"UIT to exit ",$color,"[T]",$reset,"ext to change the menu text color e",$color,"[X]",$reset,"ecute Any other key +",$color," <ENTER> ",$reset,"to print out the snipp +et -> " unless $hide_help==1; my $input = <>; if ($input =~ /^N/i){ ++$index; next; }elsif($input =~ /^G/i){ print "enter a number from -",$#files+1," to $#files -> "; $index = <>; chomp $index; }elsif($input =~ /^H/i){ $hide_help = $hide_help?0:1; }elsif($input =~ /^C/i){ $EDITOR = <>; chomp $EDITOR; $EDITOR = which ($EDITOR); # redo STUDY; }elsif($input =~ /^R/i){ --$index; }elsif($input =~ /^L/i){ print "change menu item color to: "; my $newcolor = <>; chomp $newcolor; $color = colorize($newcolor); }elsif($input =~ /^T/i){ print "change menu text color to: "; my $textcolor = <>; chomp $textcolor; $reset = colorize($textcolor); }elsif($input =~ /^(SANDBOX)/i){ print "$homedir/"; my $sandbox = <>; chomp $sandbox; $STUDYPATH = change_snippet_dir($sandbox); }elsif($input =~ /^E/i){ system ($EDITOR,$files[$index]); }elsif($input =~ /^X/i){ do $files[$index]; print "\n"; $clear = 0; }elsif($input =~ /^Q/i){ last; }else{ $clear=0; open (SCRIPT,"< $files[$index]") or warn "can't open file: $!"; print while <SCRIPT>; } } sub which{ my $editor = shift; foreach (my @PATH = split(":",$ENV{PATH})) { if (-x "$_/$editor") { return "$_/$editor"; last; } } which ('less') if not -x "$editor"; } sub colorize{ my $a = shift; my $newcolor; eval "use Term::ANSIColor 'color'"; return if $@; for (my @valid_colors = split /,/ => qq( blue bold,yellow bold,reset, green bold,red bold,white, bold blue,bold yellow,bold white, bold green,bold red, red,green,yellow,blue)) { s/^\s+//s; if ( $a =~ /($_)/i) { return $newcolor = color($_); } } } sub change_snippet_dir{ my $sandbox = shift; my $STUDYPATH = "$homedir/$sandbox"; -d $STUDYPATH ? return $STUDYPATH: return "$ENV{HOME}/$ENV{SANDBOX}"; }

In reply to newer snippet browser by mkmcconn

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.