Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Pulling white space off before/after string?

by monkey_boy (Curate)
on Apr 17, 2007 at 09:48 UTC ( #610486=note: print w/ replies, xml ) Need Help??


in reply to Pulling white space off before/after string?

Regexs are soooo last decade!
use String::Util

use String::Util qw(trim) my $s = trim(' here are lots of nasty spaces ');

UPDATE: seems i accidently caused a small argument with my tounge in cheek comments. However, IMHO this is a nice little module, with the trim() function being one of many nice little snipits provided that i use often, worth checking out!. Also, just becuase it hasnt been updated in a year does not make it a obsolete, its more likely that there are no bugs to be fixed, as all the subs are quite small & simple.


This is not a Signature...


Comment on Re: Pulling white space off before/after string?
Download Code
Re^2: Pulling white space off before/after string?
by Fletch (Chancellor) on Apr 17, 2007 at 15:18 UTC

    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:

    #--------------------------------------------------------------------- +--------- # trim # =head1 trim(string) Returns the string with all leading and trailing whitespace removed. Trim on undef returns undef. =cut sub trim{ my ($val) = @_; if (defined $val) { $val =~ s|^\s+||s; $val =~ s|\s+$||s; }; return $val; } # # trim #--------------------------------------------------------------------- +---------

    If it really bothers you just add one line to your code and be done with it.

      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 );

        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
Node Status?
node history
Node Type: note [id://610486]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-12-26 19:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls