Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Scurbbing a string

by BrowserUk (Patriarch)
on Aug 24, 2007 at 21:06 UTC ( [id://634966]=note: print w/replies, xml ) Need Help??


in reply to Scrubbing a string

You'll probably reject this because it uses string eval, but it is simple and effective.

$ex = 'p!u-r-*e'; $ctl = 'pure';; eval "\$ex =~ tr[$ctl][]cd;"; print $ex;; pure

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.

Replies are listed 'Best First'.
Re^2: Scurbbing a string
by Anonymous Monk on Aug 25, 2007 at 09:17 UTC
    Won't that fail, or even produce incorrect results, if $ctl includes tr metacharacters?

      Yes. But remember that $ctl in that snippet is the equivalent of the OPs $pure, not the variable being cleaned.

      Still, I guess they could legitimately contain meta chars. If the OPs $pure variable(s) are sourced from outside of his program or genuinely might contain meta characters, then a quotemeta would probably be appropriate.

      Indeed, it might be good if that were mentioned in the eval tr example in perlop.


      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://634966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2026-01-19 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (122 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.