Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: trimming space from both sides of a string

by BrowserUk (Patriarch)
on Oct 25, 2010 at 04:54 UTC ( [id://867138]=note: print w/replies, xml ) Need Help??


in reply to Re^2: trimming space from both sides of a string
in thread trimming space from both sides of a string

It certainly works, but it's interesting to see what happens under the covers.

If you use a regex that doesn't use captures:

$a = ' fred and bill ';; Dump $a;; SV = PV(0x15be50) at 0x1b7ad8 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1d3558 " fred and bill "\0 CUR = 32 LEN = 40 $a =~ s[^\s*|\s*$][]g;; Dump $a;; SV = PV(0x15be50) at 0x1b7ad8 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1d3558 "fred and bill"\0 CUR = 13 LEN = 40

You'll notice that the PV (the pointer to the memory holding the actual string) hasn't changed. Although the leading and trailing whitespace has been "removed", this has been done by juggling a few offsets into the original string.

Now doing it your way:

$b = ' fred and bill ';; Dump $b;; SV = PV(0x13be50) at 0x13e0d8 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1b3558 " fred and bill "\0 CUR = 32 LEN = 40 $b =~ s[^\s*(.*?)\s*$][$1];; Dump $b;; SV = PVMG(0x207928) at 0x13e0d8 REFCNT = 1 FLAGS = (SMG,POK,pPOK) IV = 0 NV = 0 PV = 0x38b4388 "fred and bill"\0 CUR = 13 LEN = 16 MAGIC = 0x391bda8 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_LEN = -1

Notice that the PV changed, meaning that it had to: calculate the offset and length of the "remainder"; it then allocated a new lump of space to hold it; then copy it from the original string to the new string; and then free the old space.

And in the process, it upgraded the original PV to a PVMG and attached some magic to it, meaning several more allocations and frees. I'm not sure quite what that magic does in this case?

So, whilst the end result is semantically the same, the route getting there is a lot further around.

Sometimes, on a nice day, a slow, meandering route to the shops is a pleasant diversion, but few of would deliberately take a circuitous route habitually if we know a better one.

Of course, if you really want to "do it the right way", you'd replace these simple one liners with a CPAN module like Text::Trim which uses the more efficient two regex method. Of course, by doing so you'd be throwing away the performance gain of the two regex method by a) calling a subroutine; b) having that subroutine do inanities like assigning @_ to itself! (But only if it actually contains something!)

But hey. It's on CPAN, so it's got to be good right!


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-29 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found