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');
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: new way to code html?
by ColonelPanic (Friar) on Nov 21, 2012 at 14:29 UTC | |
by Don Coyote (Hermit) on Nov 21, 2012 at 20:48 UTC | |
Re: new way to code html?
by muba (Priest) on Nov 21, 2012 at 01:51 UTC | |
by tobyink (Canon) on Nov 21, 2012 at 07:05 UTC | |
by Anonymous Monk on Nov 21, 2012 at 08:23 UTC | |
Re: new way to code html?
by AnomalousMonk (Archbishop) on Nov 21, 2012 at 00:30 UTC | |
by Don Coyote (Hermit) on Nov 21, 2012 at 08:04 UTC | |
Re: new way to code html? (no, syntax errors never are)
by Anonymous Monk on Nov 20, 2012 at 23:43 UTC |