in reply to The Backslash Challenge

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]

Replies are listed 'Best First'.
Re^2: The Backslash Challenge
by Tux (Canon) on Mar 25, 2021 at 11:03 UTC

    Two more options

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

    Enjoy, Have FUN! H.Merijn