Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: snippet browser

by bbfu (Curate)
on Jan 15, 2001 at 12:21 UTC ( [id://51876]=note: print w/replies, xml ) Need Help??


in reply to snippet browser

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-16 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found