John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

In Pod::Html version 1.03, the process_puretext function does this:
# convert double-quotes to single-quotes if( $$quote && $text =~ s/"/''/s ){ $$quote = 0; } while ($text =~ s/"([^"]*)"/``$1''/sg) {}; $$quote = 1 if $text =~ s/"/``/s;
Now I understand what each statement does, but can't figure out how it works! The first regex will find the first (") char and turn it into a pair of single quotes. The /s modifier is superfluous.

The second will find pairs of double quotes and replace the first with backticks and the second with two singles.

Then any remaining ("), if there were an odd number (even to begin with since the first was already taken) that last one is converted to backticks.

So... I'm reading that

stuff "passage one" more stuff "passage two" stuff
would convert the first one to close quotes, then pairs with open/close, then the last to open.
stuff ''passage one`` more stuff ''passage two`` stuff
Isn't that backwards?

—John

Replies are listed 'Best First'.
Re: logic for smart quotes - what does this code mean?
by Gorilla (Sexton) on Jan 17, 2003 at 07:11 UTC
    Your guess of the result is wrong (I will explain why), I have carefully read the whole package, and believe the actually result should read:

    stuff ``passage one'' more stuff ``passage two'' stuff

    Some more detailed explanation:

    $quote is just the reference to an internal flag $ptQuote, which is used GLOBALLY to store the quotes conversion status.

    For example, if you have this piece of two-line text in your pod:
    abcd"ef gh"ijk
    Although they will be processed as two separate lines, but as the quote status is rememebred globally, those two lines would be converted correctly to:
    abcd``ef gh''ijk
    The reason you had the wrong guess, is that you didn't realize $ptQuote is initially set to 0, so the first time you execute that piece of code you showed, the if condition is not satisfied, thus the if block is not executed.
Re: logic for smart quotes - what does this code mean?
by chromatic (Archbishop) on Jan 17, 2003 at 06:26 UTC

    The first will convert a double quote character into two single quotes only if $$quote is true. From that it appears that $$quote is a flag to mark if an opening quote has been found but its partner has not. I'll go further to assume that $text is a chunk at a time, and this takes care of quotes that span chunks.

Re: logic for smart quotes - what does this code mean?
by jryan (Vicar) on Jan 17, 2003 at 08:19 UTC
    Gak, thats what you get into diving into the terrible, terrible, bad, evil, abmonination of source code that is Pod::HTML. There's a reason Sean M. Burke wrote Pod::Simple the way he did!
      Thanks for the pointer to Pod::Simple.