Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
I don't know where else this goes, so I'm putting in here. Add to this list as you see fit. Make it a good place for people to look. While there are only a few things here now, I'd like to see this get quite large.

Do as little as possible

Compare these two lines of code:
@small = grep { $_->size < 100 } sort $query->all_records; # vs. @small = sort grep { $_->size < 100 } $query->all_records;
Notice a difference? As the number of records increases, the first method will get slower. Why? Because you weren't smart enough to sort only the data you need. The two produce identical lists (unless you have severe magic going on, in which case, I don't wanna hear about it), but the second will produce it in better time, since you're only sorting the matches, not all the records.

Use the proper tool

Don't use m// where substr() will do. Don't use substr() where unpack() will do. Don't use m// where index() (or even better, rindex()) will do. Don't use s/// where tr/// will do.

Knowing when to use a hammer and when to use a jackhammer is a valuable skill in programming; and even moreso in Perl, where TMTOWTDYOG (there's more than one way to dig your own grave). Here are examples of the above:
  1. m// vs. substr()
    ($short) = $desc =~ /^(.{0,100})/; # breaks on embedded newlines, and is better as $short = substr($desc, 0, 100);
  2. substr() vs. unpack()
    $name = substr($rec, 0, 20); $age = substr($rec, 20, 5); $job = substr($rec, 25, 25); # repeated calls to substr() better as unpack() ($name,$age,$job) = unpack 'A20 A5 A25', $rec;
  3. m// vs. index() or rindex()
    if ($str =~ /jeff/) { ... } # why not if (index($str,'jeff') > -1) { ... } # or if you know that if it's there, it's toward the end: if (rindex($str,'jeff') > -1) { ... }
  4. s/// vs. tr///
    # (on average) s/[abc]*//g; # is FAR slower than s/[abc]//g; # is slower than s/[abc]+//g; # which is slower than tr/abc//d;

Love your string functions

Regexes are not the answer. They're an answer. I prefer lc($A) eq lc($B) to some convoluted, anchored regex like $A =~ /\A\Q$B\E\z/i (for reasons I hope you can see). And check this crazy trick -- to find the first occurrence of a lowercase or uppercase letter in a string, there's no need to use a regex, just use: $pos = index(lc($str), lc($c)).
Add more. That's more than a suggestion.

japhy -- Perl and Regex Hacker

In reply to Code Smarter by japhy

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.