Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re^3: trimming space from both sides of a string by BrowserUk
in thread trimming space from both sides of a string by szabgab

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-05-17 12:18 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (65 votes). Check out past polls.