Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

snippet browser

by mkmcconn (Chaplain)
on Jan 11, 2001 at 21:53 UTC ( #51172=snippet: print w/replies, xml ) Need Help??

I've collected hundreds of tiny snippets from books and tutorials, and an especially large number from Perl Monks. These all go into an "examples" directory. Listed, is the script I use to browse those snippets (pardon the several clues that this is only used on a Linux box).

I usually start it with a negative number as the argument, since the snippets are sorted in the directory with the most recent last.

Don't be insulted by the babytalk Perl. "Everybody's got to start somewhere" and this is unapologetically a beginner's work. Consequently, if you're going to vote it down for being stupid or boring, I'd appreciate a lecture on what's wrong.
    Positive comments also welcome!

#!/usr/bin/perl -w
use strict;
my $index = @ARGV ? $ARGV[0]:1;
for (my @files=(glob "$ENV{HOME}/$ENV{SANDBOX}/examples/*")){
print "$files[$index] \n";
open (SCRIPT,"< $files[$index]") 
  or warn "can't open file: $!";
    print while <SCRIPT>;
    do $files[$index];
    print "type [N]EXT to continue browsing examples,
      or another key to repeat the snippet__  "; 
    redo STUDY unless <> =~ /^[Nn]/;
Replies are listed 'Best First'.
Re: snippet browser
by bbfu (Curate) on Jan 15, 2001 at 12:21 UTC

    Well, these are just my suggestions; all of which you can take or leave as you see fit. Most of them are just opinion. Remember, TIMTOWTDI! =)

    1. for (my @files=(glob "$ENV{HOME}/$ENV{SANDBOX}/examples/*")){
      In this instance, the array @files, as well as the variable $index are synthetic variables. An easier, perhaps more elegant, way of doing this would be to use an array slice. Update: Unfortunately, glob() doesn't actually return an array. So I can't figure out a way to not use a temporary, synthetic variable (@files) to do the slice. Someone please let me know if I'm missing something.

      Since the for loop stores the current item in $_, there is no need for the $files[$index] construct inside the loop. If you'd like a named variable index, you can always use the "for my $index (...)" form.
    2. print while <SCRIPT>;
      Hrm. Well, the main thing with this that I can see is: What if the snippet is longer than one screen in length? You might consider using an external program to list the file (less?) or to at least use a counter to pause after a screenfull of info. Also, you might consider allowing an external editor (vi || emacs), as it would have syntax highliting.
    3. do $files[$index]; You want to execute these snippets? What if they don't work so well as stand-alones (as snippets are wont to do)? Oh well. That's your choice.
    4. redo STUDY unless <> =~ /^[Nn]/;
      Um... How do you exit the program? I guess you have to view all the files, eh? Maybe another option would be in order here. Also, STUDY isn't really needed (though it doesn't really hurt either).

    Update: Well, that'll teach me to post in the middle of the night w/o testing my code first. =) I forgot to "my" (or "use vars") $EDITOR and $PATH. For some reason, I failed to realize that using an external viewer obviates the need to open the file at all. I forgot a comma on the print line. And my array slice totally didn't work (see above). Oh, and I also didn't notice (and thus perpetuated) that mkmcconn was using 1 as the default starting index (which skips the first file). *sigh* No more posting from home for me, that's for sure!

    At any rate, this is how I would rewrite it:

    #!/usr/bin/perl -w use strict; my $EDITOR = "/usr/bin/less"; my $PATH = "$ENV{HOME}/$ENV{SANDBOX}/examples"; my @files = glob("$PATH/*"); for (@files[($ARGV[0] || 0) .. $#files]) { system($EDITOR, $_); # do $_; # You can do that if you want to... Pun not intended. # Personally, I'd make Next the default... But whatever. =) print "Type [N]EXT to see the next snippet, [Q]UIT to quit,\n", " or any other key to repeat the last snippet: "; my $input = <>; $input =~ /^N/i && next; # or: next if($input =~ /^N/i); $input =~ /^Q/i && last; # or: last if($input =~ /^Q/i); }

    Well, that's my $0.02. Let me know what you think!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://51172]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2023-05-28 06:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found