Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I boiled your examples down to the case of "$str1 $str2", and I can confirm the behavior you observed on Perl 5.26 as well. The answer seems to be that Perl optimizes away the extra stringification step in certain cases - note how in the below, stringify becomes ex-stringify:

$ perl -MO=Concise -e 'my $x; $x = "$y $z"' b <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 3 <0> padsv[$x:1,2] vM/LVINTRO ->4 4 <;> nextstate(main 2 -e:1) v:{ ->5 a <@> stringify[$x:1,2] sK/TARGMY,1 ->b - <0> ex-pushmark s ->5 9 <2> concat[t3] sKS/2 ->a 7 <2> concat[t2] sK/2 ->8 - <1> ex-rv2sv sK/1 ->6 5 <$> gvsv(*y) s ->6 6 <$> const(PV " ") s ->7 - <1> ex-rv2sv sK/1 ->9 8 <$> gvsv(*z) s ->9 -e syntax OK $ perl -MO=Concise -e 'my $x; $x = ""."$y $z"' c <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 3 <0> padsv[$x:1,2] vM/LVINTRO ->4 4 <;> nextstate(main 2 -e:1) v:{ ->5 b <2> concat[$x:1,2] sK/TARGMY,2 ->c 5 <$> const(PV "") s ->6 - <1> ex-stringify sK/1 ->b - <0> ex-pushmark s ->6 a <2> concat[t3] sKS/2 ->b 8 <2> concat[t2] sK/2 ->9 - <1> ex-rv2sv sK/1 ->7 6 <$> gvsv(*y) s ->7 7 <$> const(PV " ") s ->8 - <1> ex-rv2sv sK/1 ->a 9 <$> gvsv(*z) s ->a -e syntax OK $ perl -MO=Concise -e 'my $x = "$y $z"' a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 9 <2> sassign vKS/2 ->a - <1> ex-stringify sK/1 ->8 - <0> ex-pushmark s ->3 7 <2> concat[t3] sKS/2 ->8 5 <2> concat[t2] sK/2 ->6 - <1> ex-rv2sv sK/1 ->4 3 <$> gvsv(*y) s ->4 4 <$> const(PV " ") s ->5 - <1> ex-rv2sv sK/1 ->7 6 <$> gvsv(*z) s ->7 8 <0> padsv[$x:1,2] sRM*/LVINTRO ->9 -e syntax OK $ perl -MO=Concise -e 'my $x; $x = $y." ".$z' c <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 3 <0> padsv[$x:1,2] vM/LVINTRO ->4 4 <;> nextstate(main 2 -e:1) v:{ ->5 b <2> sassign vKS/2 ->c 9 <2> concat[t3] sKS/2 ->a 7 <2> concat[t2] sK/2 ->8 - <1> ex-rv2sv sK/1 ->6 5 <$> gvsv(*y) s ->6 6 <$> const(PV " ") s ->7 - <1> ex-rv2sv sK/1 ->9 8 <$> gvsv(*z) s ->9 a <0> padsv[$x:1,2] sRM* ->b -e syntax OK

I'm not yet sure what you could do about this, or even if you can do something about it, because apparently the final stringification is always part of Perl's intended operations, but it's just optimized away in some cases. Plus, at the moment I can't remember and can't find in the docs whether Perl even makes any guarantees as to what order the concatenations and stringifications are executed in a case like "$str1 $str2", which one might think is equivalent to $str1." ".$str2, but apparently is not, because the former sometimes involves an extra stringification. Normally the order doesn't really make a difference, except unfortunately when you rely on overloading. One might even argue that the aforementioned inconsistency could be seen as bug in Perl.

Of course the other possible argument is that in this case, relying on overloading is the problem. If I take a step back and look at this from an "XY Problem" angle: You've got objects that you want to behave as normal strings, except that they (loosely speaking) have an extra layer of encoding wrapped around them. You're generating HTML, so that encoding is for example "<>" to "&lt;&gt;". You're worried because you see this encoding happening too early. But my question here is: you've got some strings that should be encoded (your str objects), and others that shouldn't (plain strings). But in the end, everything is going to become a plain string anyway, so why do you care when the encoding step happens? (Maybe I'm missing something.)

I completely understand that this kind of an implementation is "neat" :-) But why not use the existing solutions for HTML generation? For example, Template::Toolkit, Mojo::Template, ...

By the way: Crossposted to StackOverflow. Crossposting is ok, but it is considered polite to inform about it so that efforts are not duplicated.


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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2022-01-26 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (69 votes). Check out past polls.

    Notices?