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

Re: Pearls (not really) of Perl programming

by Anonymous Monk
on Nov 24, 2004 at 20:37 UTC ( #410240=note: print w/ replies, xml ) Need Help??


in reply to Pearls (not really) of Perl programming

$default_title = substr($default_title, 0, 50);


Comment on Re: Pearls (not really) of Perl programming
Re^2: Pearls (not really) of Perl programming
by revdiablo (Prior) on Nov 24, 2004 at 21:35 UTC

    What's bad about this? Sure, there are other ways to do it, but it doesn't seem particularly egregious to me. For those following along at home, here are some other ways:

    $default_title =~ s/(.{50}).*/$1/; $default_title =~ s/./$ctr++<50?$&:""/ge; $default_title = pack "A50", $default_title; $default_title = join "", (split //, $default_title)[0..50];

    Personally, I like the original much better than these alternatives. The only other way that I would possibly use is:

    substr($default_title, 50) = "";

    But substr as an lvalue is not something everyone knows about, so it's not clearly the best option.

      More regex solutions: (don't forget the s modifier)

      ($default_title) = $default_title =~ /(.{0,50})/s; $default_title =~ s/(?<=.{50}).*//s;
      I'd also want to add
      substr($default_title, 50, length($default_title), '');
      and second you and the original isn't that bad.

      ihb

      See perltoc if you don't know which perldoc to read!
      Read argumentation in its context!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2014-07-28 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (206 votes), past polls