Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

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

ok nothing Ammazzingg here really, just a little bit neat. (And what the heck 1 vote to go for next level Scribe :D)

looking at simple package / subroutines I wrote a sub that had a print operation as its last argument. I called the sub from within a print.

the starting problem was that my print statements were all jumbled up with the print from the sub being evaluated then the print itself and finally a return of 1 or true from the call to the sub at the end of the print.

package Htmlz; sub ah{ print 'an experiment' } package Effect; print 'im conducting ', Htmlz::ah; ------- an experimentim conducting1

:( right so i eventually realise pass a scalar back! duh!

package Htmlz; sub ah{ my $fizix = 'an experiment' } package Effect; print 'im conducting ', Htmlz::ah; -------- im conducting an experiment

:) hooray this is how it is supposed to be! but as I say I was grappling with what was happenning so I tried wantarray ? : to see if maybe the caller of the sub had been expecting an array at some point. And something interesting happened. The sub evaluation returned the print as in the first attempt. So it was truly expecting an array afer being called from a print. Or was it? However, as the last statement was now a scalar, instead of returning true or 1, the scalar itself was also returned during the evaluation of the print statement. So it was also expecting a scalar??

At which point I hold my hands up and start thinking 'is this what they mean by a side effect?' and post the code

package Htmlz; sub ah{ wantarray? my @givearr = print 'anexperiment' : my $fizix = ' kerpow!' } package Effect; print 'im conducting ', Htmlz::ah; ------- anexperimentim conducting kerpow!

Still printing out the wrong way round, but the 1 is replaced with kerpow. So what is intersting is, how about I do..

package Htmlz; sub ah{ my $p = my $htmlparagraphelement = shift; wantarray ? my @givearr = print '<',$p,'>' : my $fizix = '</'.$p.'>' } package Effect; print 'im conducting kerpow! an experiment', Htmlz::ah('p'); #pass p ------- <p>im conducting kerpow! an experiment</p>

I think this is why I like perl.

Unfortunately though, this doesnt nest too well when you start to add more elements. I do however seem to spend a lot of time previewing my posts here...

print 'Coyote',Htmlz::ah('H1');

In reply to new way to code html? by Don Coyote

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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (7)
    As of 2018-05-22 20:33 GMT
    Find Nodes?
      Voting Booth?