Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Pulling white space off before/after string?

by chromatic (Archbishop)
on Apr 17, 2007 at 19:18 UTC ( [id://610623]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Pulling white space off before/after string?
in thread Pulling white space off before/after string?

In what decate was it good practice introducing a dependency on an external module in order to get the exact same two substitutions hidden in a sub:

The same decade where abstracting operations behind a meaningful name made programs easier to understand. Compare:

# trim whitespace $string =~ s/^\s+//; $string =~ s/\s+$//;

... to:

trim( $string );

Replies are listed 'Best First'.
Re^4: Pulling white space off before/after string?
by Fletch (Bishop) on Apr 17, 2007 at 19:50 UTC

    What part of "If it really bothers you just add one line to your code and be done with it." would indicate I'm against abstraction rather than the introducing an external dependency on a CPAN module that hasn't been updated in over a year which provides a bunch of marginally "string" related subs that are pretty much all wrappers for around 3-4 lines worth of code?

    sub trim { (my $t = shift()) = s/^\s+//; $t =~ s/\s+$//; $t }

    There, done. Add that to your stock program template or define it as a snippet in your editor and Robert is your parent's sibling.

    (Having said that, yes I do have issues to some degree with this level of micoabstraction. Does the putative trim work on just one string, or a list of strings (a la chomp)? If the string contains multiple lines, does it remove spaces from the absolute beginning and end of the string or from the beginning and end of the logical lines? It's easy to tell the difference when s/^\s+// versus s/^\s+//mg (and their corresponding $ anchored cohort) are sitting there in front of you; it's not when you've got to break your train of thought and spend 3-5 minutes API diving. Two lines just seems waaaaaay below the threshold of worth-abstracting-ness.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 02:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found