Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: Transform ASCII into UniCode (escape_metas)

by LanX (Saint)
on Mar 23, 2021 at 18:18 UTC ( [id://11130228]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Transform ASCII into UniCode (escape_metas)
in thread Transform ASCII into UniCode

> Why not test it directly, then?

I wanted to have a generic method for escaping selected metas while keeping others as is.

> what did I do wrong?

Took me a moment to understand (well guess) what's happening

Take case #2:

a\\b is internally the 3 char string a\b , so this escape b is untouched.

but you do a string interpolation for

eval "\$string =~ tr/$s/$r/r"

so whats happening is

DB<57> say "a\\b" =~ tr/a\b/xyz/r # expected 'xyz' x\b DB<58>

Actually I'm not sure what tr's interpretation of \b is here

Question is if your expectation was right, because the literal code gives my result (?)

C:\tmp\t_wperl>perl -E"say 'a\\b' =~ tr/a\b/xyz/r # expected 'xyz'" x\b C:\tmp\t_wperl>

update

I.o.W.

Your expectation is to have a 1-to-1 mapping of characters.

My expectation was to emulate tr like implemented and to catch injections.

It's a question of definition.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^5: Transform ASCII into UniCode (escape_metas)
by LanX (Saint) on Mar 23, 2021 at 19:08 UTC
      I see. I would expect the strings coming in to contain the characters to operate on, not strings that Perl can turn into the characters to operate on by evaluating them.

      The \b is tricky, as I can use it to get three different results from my three subroutines:

      # Add this to the list of cases: [ "a\b\\b" => '\\/', 'xy', "a\bxb"] # Results: not ok 22 - simulate # Failed test 'simulate' # at ./2.pl line 64. # got: 'xb' # expected: 'xb' ok 23 - cheat Replacement list is longer than search list at (eval 45) line 1. not ok 24 - use # Failed test 'use' # at ./2.pl line 66. # got: '\b' # expected: 'xb'

      Switching to Test2::V0 explains the test 22:

      not ok 22 - simulate # Failed test 'simulate' # at ./2.pl line 64. # +-------+----+-------+ # | GOT | OP | CHECK | # +-------+----+-------+ # | a\bxb | eq | xb | # +-------+----+-------+

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Sorry I'm too tired now, I'll check tomorrow.

        Multiple levels of escaping are often kind of brainf*ck.

        > would expect the strings coming in to contain the characters to operate on, not strings that Perl can turn into the characters to operate on by evaluating them.

        this escape_metas can also be used with a character class [METAS] for with multiple metas.

        > The \b is tricky, as I can use it to get three different results from my three subroutines:

        exactly, as I said, it depends what the intended behavior is.

        FWIW

        my intention was to implement a sub tri = "transliterate interpolated" which allowed to write tri($input,$left,$right) and implemented $input =~ tr/LEFT/RIGHT/ while sticking close to tr's behaviour but without breaking.

        I'll try again tomorrow. after GPW. :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found