Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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, ">$") || 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!


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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2018-01-23 06:22 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (240 votes). Check out past polls.