Intrepid has asked for the wisdom of the Perl Monks concerning the following question:
This code isn't a JAPH nor is it a properly obscure obfu I think. But it's definitely in "play" mode, so if you are in "play" mode please read on -- and see if you can help me improve it or learn something new.
What I came up with cannot be "production code" because the only way to do this in production code that I think would be countenanced, might be
my $quoted_str = q{"Eat Potatoes Daily"}; $quoted_str =~ s< ^" ([^"]+) "$ > < $1 >x print "Unquoted: $quoted_str\n";
But in play mode I wanted something more opaque ;-) and also thought to myself, "I don't want to pay
the (run-time) penalty for that back-reference ($1) in the replacement expression." So I came
up with this ugly little beast:
my $quoted_str = q{"Eat Potatoes Daily"}; # Remove surrounding quotes, an obfu way: for (my $n = 1, my $ip = '^', my $tp = ''; 3 > $n; $tp = '$') { $ip = '' if $n += $quoted_str =~ s<$ip"$tp><>; } print "Unquoted: $quoted_str\n";
At one point I had in mind that the flip-flop operator " .. " might help me, used in a scalar context, but I couldn't work that out. Anybody see how to use it to toggle between matching at the beginning of the string and at the end? I couldn't.
Any other alternative solutions are definitely appreciated. And if editors feel that I've posted this in the wrong area of the Monastery, please edit Title / move at will.
-- use PerlMonk::Tye qw(:wisely);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is this an obfu? - alternate ways of trimming quotes
by diotalevi (Canon) on Aug 21, 2003 at 19:41 UTC |