string - How can I escaped regular expression metacharacters from an interpolated variable in Perl? -
I need to create an "obscure" function that changes the empty-text password in the line before writing to login gives.
It looks like:
function pass_obscure {my $ logline = shift; My $ pass = "wer32pass $"; # This password is an example, is received as a real parameter, or is already stored in the form of global value $ logline = ~ s / $ pass / ********* / g ; Return $ $ line; }
But this, of course, does not work, the '$' sign in the password string has been projected as the character of an interval, so there is no matching, and therefore The replacement does not work.
How can I solve it?
Thank you.
(Why not just keep the password from the log line in the first place?)
Use quotemeta :
$ pass = "password \ $"; $ Logline = "Password: password \ $"; Print "$ pass \ n"; Print "$ logline \ n"; $ Pass_quoted = quotemeta ($ pass); $ Logline = ~ s / $ pass_quoted / ******** / g; Print "$ logline \ n"; The password is $ password: ********
/ Div>
Comments
Post a Comment