Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

The Backslash Challenge

by choroba (Cardinal)
on Mar 24, 2021 at 20:12 UTC ( [id://11130291]=obfuscated: print w/replies, xml ) Need Help??

To return the character that's used as the delimiter in a quote-like operator, you can use a backslash.
'/' eq q/\// or die "Failed";

Your task is to return a backslash using a backslash as the delimiter. Use qq\\ for level easy, use q\\ for level guru.

There are definitely many ways how to do it for level easy.

I haven't found a way how to do it with the single q.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re: The Backslash Challenge (updated)
by haukex (Archbishop) on Mar 24, 2021 at 20:29 UTC
      > then I clicked on your spoiler...

      I should have done this earlier ...

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

Re: The Backslash Challenge
by choroba (Cardinal) on Mar 24, 2021 at 22:11 UTC
    OK, here are some solutions I was able to find:

    Are you sure you don't want to try yourself?

    Double spoilers don't work correctly on PerlMonks, but they hide the contents twice. Give it a few more minutes.

    I started with the most obvious one. In double quotes, you can use the "babycart" operator (see perlsecret); inside it, you can have any code you like (well, in our case, any code not containing a backslash, because, as haukex correctly found in perlop, you can't include a backslash literally in a qq\\). To most straightforward way is to use chr:

    1.

    qq\@{[chr 92]}\

    There are infinitely many ways how to get a backslash. We can use pack

    2.

    qq\@{[pack "c", 92]}\

    or sprintf

    3.

    qq\@{[sprintf '%c', 92]}\

    Another way is to use a bitwise operation, e.g. xor. Here are just two examples I liked:

    4.

    qq\@{['{'^"'"]}\ qq\@{['~'^'"']}\

    Then I skimmed perlfunc searching for functions that can return a backslash. I found a few that return a bit more so we need to combine them with substr or a match:

    5.

    qq\@{[substr quotemeta ";", 0, 1]}\ qq\@{[prototype("CORE::push") =~ /^(.)/]}\

    We can also require a core module that outputs a backslash.

    6.

    qq\@{[require File::Spec::Win32 and File::Spec::Win32->rootdir]}\ qq\@{[do{require Data::Dumper;substr Data::Dumper::Dumper("'"),-5,1}]} +\

    OK. Is there any other way than using the array dereference? I've found a way! Let's use a string dereference instead. We need to create a string reference inside, which I can't do without a backslash, but fortunately there's eval that makes it possible to generate the backslash without using a backslash.

    7.

    qq\${eval chr(92).'"'.(chr(92)x2).'"'}\

    Finally, here's a joke. What a pity it doesn't work. Update: In a way, it does! See Tux's reply below!

    ∞.

    qq\@{[reverse "/"]}\

    To sum it up:

    print qq\@{[chr 92]}\, qq\@{[pack "c", 92]}\, qq\@{[sprintf '%c', 92]}\, qq\@{['{'^"'"]}\, qq\@{['~'^'"']}\, qq\@{[substr quotemeta ";", 0, 1]}\, qq\@{[prototype("CORE::push") =~ /^(.)/]}\, qq\@{[require File::Spec::Win32 and File::Spec::Win32->rootdir]}\, qq\@{[do{require Data::Dumper;substr Data::Dumper::Dumper("'"),-5,1}]} +\, qq\${eval chr(92).'"'.(chr(92)x2).'"'}\, qq\@{[reverse "/"]}\;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Two more options

      • Encode::encode("cp1047","*")
      • charnames::string_vianame("REVERSE SOLIDUS")

      Enjoy, Have FUN! H.Merijn
Re: The Backslash Challenge
by LanX (Saint) on Mar 25, 2021 at 02:18 UTC
    The goal is maximum obfuscation, right? ;)

    DB<267> x qq\@{[ (sort grep { $_ gt '[' } %::)[0] ]}\; 0 '\\' DB<268> x qq\@{[ grep { '[' lt $_ lt ']' } %:: ]}\; 0 '\\' DB<269>

    The second one requires a recent Perl but can be easily rewritten to work with older versions. :)

    update

    also tried this but see comment

    print "\\\n" eq qq\@{[<<'']}\; \ # is there an easy way to chomp a here doc?

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

    update

    added trailing ; to debugger examples

      Nice trick!

      Interestingly, it doesn't work for me. In the debugger, I'm getting

      cont: Can't find string terminator "\" anywhere before EOF at (eval 13)[/usr +/lib/perl5/5.26.1/perl5db.pl:738] line 2. at (eval 13)[/usr/lib/perl5/5.26.1/perl5db.pl:738] line 2. eval '' called at /usr/lib/perl5/5.26.1/perl5db.pl line 738 DB::eval called at /usr/lib/perl5/5.26.1/perl5db.pl line 3140 DB::DB called at -e line 1

      And if I run it in a script instead, the output is _, not \ :-o

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        > cont:

        A backslash at EOL marks a multiline statement in DB. I had a trailing whitespaces to avoid this, but the C&P from cmd.exe erased them. :( Fixed now!

        > And if I run it in a script instead, the output is _, not \ :-o

        both lines? lemme check...

        ...

        Argh! Seems that the context of the debugger is initializing a "crucial thing" which can't be taken for granted :(

        $\ is not in the symbol table til it's first set

        update

        yes a use DB; at the start of the script is "fixing" it.

        Actually that could be a source of a Heisenbug.

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

Re: The Backslash Challenge
by LanX (Saint) on Mar 24, 2021 at 20:56 UTC

    > There are definitely many ways how to do it for level easy.

    Please show me.

    The only idea I had were control sequences, but they all start with a backslash.

    update

    well I consider it cheating, but since you keep repeating that there are no rules ;-)

    DB<163> $a='\\'; say $a eq qq\$a\ 1 DB<164>

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

      qq\@{[chr 92]}\

        yeah I updated something similar.

        Mine is simpler, yours is less cheating... more obfuscated! ;-)

        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: obfuscated [id://11130291]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found