Not really an obfu?

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.

The problem:

trim quotes from string. Do not molest any quotes that might be embedded inside the string -- only balanced quotes at ^ and $ may be removed.

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.

    Soren/Intrepid

-- 
use PerlMonk::Tye qw(:wisely);

In reply to Is this an obfu? - alternate ways of trimming quotes by Intrepid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.