Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Prototype mismatch: sub main::trim: none vs ($) at mysqlDB_serial.pl line 188.

by hippo (Bishop)
on Feb 08, 2017 at 15:27 UTC ( [id://1181425]=note: print w/replies, xml ) Need Help??


in reply to Prototype mismatch: sub main::trim: none vs ($) at mysqlDB_serial.pl line 188.

++haukex on everything from me too.

Did you (bNathan) know that these three functions already exist? They are part of String::Util so there is no need to reinvent the wheel. Remember that laziness is one of the three great virtues.

Replies are listed 'Best First'.
Re^2: Prototype mismatch: sub main::trim: none vs ($)
by haukex (Archbishop) on Feb 08, 2017 at 15:36 UTC

    Hi hippo & bNathan,

    Or, just use the regexes directly ;-)

    $string =~ s/^\s+//; # ltrim $string =~ s/\s+$//; # rtrim $string =~ s/^\s+|\s+$//g; # trim

    Regards,
    -- Hauke D

      As a nit, Perl FAQ trimming spaces says that running both the 2 regex'es above (ltrim and rtrim) is faster than the single regex trim. This post on StackOverflow Why is single regex slower? goes into more detail about exactly why that is true.

      These 2 lines are such an idiom that I just type them from memory without needing to use a sub. With Perl, not every "shorter" formulation is faster.

        Hi Marshall,

        You're right, I didn't mention that because of what the FAQ says: "That might not matter to you, though". That attitude reminds me of the last time I read the Camel, I realized that for a large part of the book no mention is made of performance at all. I found that quite enlightening, along the lines of "until performance becomes an issue, don't worry about it" (a friendlier version of "premature optimization is the root of all evil"). I'm not advocating totally ignoring performance, but instead for worrying less when one can allow oneself to. Personally, I prefer the one-regex solution for its brevity, and so far I've usually been in a position where that performance difference doesn't matter. Of course, that can be a luxury, so thank you for mentioning the issue in case it does matter to the OP :-)

        Regards,
        -- Hauke D

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-29 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found