You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "T.J. Mather" <tj...@thoughtstore.com> on 2000/08/09 00:55:33 UTC

weird print bug?

Hi,

I'm running into some very strange behaviour with Apache::print.  print() 
is supposed to dereference any arguments that are scalar references.

However it doesn't work if you apply a non-greedy substitution operator on
the scalar.

The following code outputs in my browser:

CODE:
sub handler {
  my $r = shift;
  my $text = "hello world";
  $text =~ s/hello/hi/;
  $r->print(\$text);
}

OUTPUT:
SCALAR(0x89bd084)

What's really weird is that it works if i change the
substitution operator to greedy:

CODE:
sub handler {
  my $r = shift;
  my $text = "hello world";
  $text =~ s/hello/hi/g;    # <--- changed to /g
  $r->print(\$text);
}

OUTPUT:
hi world

Any ideas?  I'm running Perl 5.6.0/mod_perl 1.24/Apache 1.3.12 on Linux
2.2.12 (red hat 6.1) on i686.

Thanks,
T.J. Mather


RE: weird print bug?

Posted by Ken Williams <ke...@forum.swarthmore.edu>.
dharris@drh.net (David Harris) wrote:
>T.J. Mather [mailto:tjmather@thoughtstore.com] wrote:
>> However it doesn't work if you apply a non-greedy substitution operator on
>> the scalar.
>
>You mean "global", not "greedy"

True, but not especially helpful in this case.

T.J., if you grok XS you might have a look at this part of
write_client() in Apache.xs:

    for(i = 1; i <= items - 1; i++) {
        int sent = 0;
        SV *sv = SvROK(ST(i)) && (SvTYPE(SvRV(ST(i))) == SVt_PV) ?
                 (SV*)SvRV(ST(i)) : ST(i);
        buffer = SvPV(sv, len);


Try looking at your $text with Devel::Peek to see whether there are any
differences in what it contains in the two cases.




RE: weird print bug?

Posted by David Harris <dh...@drh.net>.
T.J. Mather [mailto:tjmather@thoughtstore.com] wrote:
> However it doesn't work if you apply a non-greedy substitution operator on
> the scalar.

You mean "global", not "greedy"

Two snippets from the perl documentation:

s/PATTERN/REPLACEMENT/egimosx

Options are:
    e   Evaluate the right side as an expression.
    g   Replace globally, i.e., all occurrences.
    i   Do case-insensitive pattern matching.
    ....

-- and --

By default, a quantified subpattern is ``greedy'', that is, it will match as
many times as possible (given a particular starting location) while still
allowing the rest of the pattern to match. If you want it to match the minimum
number of times possible, follow the quantifier with a ``?''. Note that the
meanings don't change, just the ``greediness'':

    *?     Match 0 or more times
    +?     Match 1 or more times
    ....

David