Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hi everyone, Recently, I've been playing around with an opendir/readdir wrapper. (Much thanks Abigail-II and VSarkiss.) My old way of running through files in a directory was:
#!/usr/sbin/perl -w use strict; #... while ($file = <*.dem>) { open(FILE, "$file") || warn "Warning: can't open $file, skippi +ng.\n"; ($outfile, $right) = split(/\./, $file, 2); open(FILEOUT, ">$outfile.xyz") || die "\nERROR: Can't open $ou +tfile, exiting.\n"; while (<FILE>) { chomp; #...
Using a wrapper, it now looks like this:
#!/usr/sbin/perl -w use strict; use Cwd; my $the_directory = cwd; #... chdir $the_directory or die "Couldn't chdir to $the_directory; $!"; opendir(D, ".") or die "Couldnt open . ($the_directory): $!"; while ($file = readdir D) { next unless $file =~ /\.las$/i; open(FILE, "$file") || warn "Warning: can't open $file, skippi +ng.\n"; while (<FILE>) { chomp; #...
Since I was working with around 4000+ files, the change in the amount of time it took for my scripts to run was impressive to say the least! So then I started thinking that this wrapper was a trick that most, if not every, perl programmer would probably find handy someday. Then I started wondering if there were any other little bits of code that are used often, or bits of code that should be used but aren't widely known. What are some tricks that every perl programmer should have in his/her bag? Thanks!

licking9Volts

Update: I think I was a little too vague in my intentions here. :) I wasn't looking for general tips for programming in perl. I was looking for tricks and shortcuts many of you use to improve upon more commonly used coding solutions. In my above example, I meant to show how even though my original solution worked fine, by using the opendir/readdir wrapper it was improved upon greatly. Sorry for the mix-up. :))

2003-06-03 edit ybiC: balanced <readmore> tags around <code> such that meditative text viewable from The Monastery Gates and Meditations.


In reply to Things every perl programmer should know? by licking9Volts

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (17)
    As of 2014-09-22 13:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (194 votes), past polls