Here is what I came up with after a quick run through at your request:
#!/usr/bin/perl -w
=head Changes
Be more consistant with your spacing.
I don't see the use for which() since you are just duplicating code th
+at
the kernel will do but less portably (under Win32, you need to spl
+it
on ';', not on ':').
Used a more "standard" method for picking an editor.
Since you only have one enclosing loop, you don't need the STUDY: labe
+l,
but I don't mind you keeping it since it makes it easy to find tha
+t loop.
On non-Unix systems, $ENV{HOME} may not be set.
Declaring subs in the middle of code just makes them hard to find.
Added a default for $ENV{SANDBOX}
s/[A-Za-z]/0/g more efficient as tr/A-Za-z/0/
Allow easy highlight with a color other than 'GREEN'
Replace repeated highlighting code with a subroutine
Treat $hide_help and $clear as Booleans instead of integers
Your first "next" doesn't do anything (I just commented it out)
Your first "redo" doesn't do anything (commented out)
Don't have a [my] variable with the same name as a global, STUDYPATH
close SCRIPT when you are done with it
=cut
use strict;
use Term::ANSIColor 'color';
my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || $ENV{EDIT}
or $^O =~ /^MSWin/ ? 'notepad' : 'vi';
my $index = @ARGV ? shift(@ARGV) :0;
my $clear = 1;
my $hide_help = 0;
my $highlight = $ENV{STUDYCOLOR} || 'GREEN';
my $homedir = $ENV{HOME} || '.';
my $sandbox = $ENV{SANDBOX} || 'sandbox';
my $STUDYPATH = "$homedir/$sandbox";
sub change_snippet_dir{
my $sandbox= shift;
my $path= "$homedir/$sandbox";
if( -d $path ) {
$STUDYPATH= $path;
}
}
sub highlight {
return color($highlight), @_, color('reset');
}
STUDY:
while (1) {
my @files= glob "$STUDYPATH/*";
if( $clear ) {
system ('clear');
} else {
$clear++;
}
$index =~ tr/A-Za-z/0/;
$index= 0 if $index < (0-$#files)-1 || $index > $#files;
printf( "%50s <- (%5s)\n", $files[$index], $index );
print "
",highlight("[C]"),"HANGE editor. Currently $EDITOR
",highlight("[E]"),"DIT to open the snippet in an editor
",highlight("[G]"),"oTo to select file index: -", ($#files+1)," to
+ $#files
",highlight("[H]"),"ELP to (un)hide this menu
",highlight("[N]"),"EXT to skip this snippet
",highlight("[R]"),"EDO to revisit the previous snippet
",highlight("[Q]"),"UIT to exit
e",highlight("[X]"),"ecute
Any other key +",highlight(" [ENTER] "),"to print out the snippet
-> "
unless $hide_help;
my $input = <>;
if ($input =~ /^N/i){
++$index;
# next; This doesn't do anything
}elsif($input =~ /^G/i){
print "enter a number from -",$#files+1," to $#files
-> ";
$index = <>;
chomp $index;
}elsif($input =~ /^H/i){
$hide_help= ! $hide_help;
}elsif($input =~ /^C/i){
$EDITOR = <>;
chomp $EDITOR;
# redo STUDY;
}elsif($input =~ /^R/i){
--$index;
}elsif($input =~ /^(SANDBOX)/i){
print "$homedir/";
my $sandbox=<>;
chomp $sandbox;
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>;
close SCRIPT;
}
}
-
tye
(but my friends call me "Tye")
-
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 How to display code and escape characters
are good places to start.