Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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!


In reply to Re: snippet browser by bbfu
in thread 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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-25 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found