Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
it is an old legacy app ... we cannot redo all of the many places where we compile strings.

I understand your point - to a degree. What I've got so far is you've got a large codebase where HTML is stitched together via "<html>$str</html>" and the like, and you've discovered that $str sometimes needs to be encoded, and you can't decide whether or not until output (for reasons you haven't explained yet, but ok).

But like you showed in the root node, you're going to have to make changes to that code base: 1. you're going to have to change all the places where variables like $str are created (my $str = str::new('<encode this later>');), and 2. you're going to have to change the output code at some places (print $html->encode;).

So what I'm guessing is that the code you don't want to change is all the code like "<html>$str</html>", probably because there is lots of code like that.

But you sometimes have code like the "bad" cases in the root node. You haven't told us how many cases in your code base there are like this, but if it's not too many, perhaps you could weed them out with a warning, like this:

#!/usr/bin/env perl use warnings; use strict; { package StringThing; use Scalar::Util qw/blessed/; use Carp; use overload fallback => 0, '""' => \&str_warn, '.' => \&concat, '.=' => \&concat; sub new { my ($class, $self) = @_; return $self if blessed($self) && $self->isa(__PACKAGE__); return bless \"$self", ref($class)||$class; } sub str_warn { carp "unintended stringification"; goto &str; } sub str { return ${+shift} } sub concat { my ($self,$other,$swap) = @_; $other=$$other if blessed($other) && $other->isa(__PACKAGE__); return $self->new( $swap ? $other.$$self : $$self.$other ); } } my $x = StringThing->new("foo"); my $y = StringThing->new("bar"); my $z = "$x $y"; print $z->str, "\n"; my $zz; $zz = "$x $y"; # warns print $zz->str, "\n"; # dies __END__ foo bar unintended stringification at line 34. Can't locate object method "str" via package "foo bar" (perhaps you fo +rgot to load "foo bar"?) at line 35.

Another idea might be to inspect the optree of your source code to find stringifications that haven't been optimized away:

$ perl -MO=Concise,-src | perl -ne \ '/^#\s+\d+\s*:/&&($a=$_);/(?<!ex-)stringify/&&print$a,$_' syntax OK # 34: my $zz; $zz = "$x $y"; # warns 1a <@> stringify[$zz:257,258] sK/TARGMY,1 ->1b

Although that feels kind of hackish, and I don't have enough experience with B::Concise to say if there are any big downsides.

I could get into the Why ... but it's a long and boring story.

Telling us the background might prompt some other ideas for other solutions :-)

In reply to Re^5: Overloading Weirdness by haukex
in thread Overloading Weirdness by pudge

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (8)
    As of 2020-01-22 16:39 GMT
    Find Nodes?
      Voting Booth?