Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: How to get content of an XML::easytree output

by tobyink (Canon)
on Mar 08, 2013 at 06:44 UTC ( [id://1022347]=note: print w/replies, xml ) Need Help??


in reply to Re: How to get content of an XML::easytree output
in thread How to get content of an XML::easytree output

"e.g. instead of print "<$element->{name}>";, use print '<', $element->{name}, '>';

Personally, I'd prefer:

printf '<%s>', $element->{name};

Update: just for the heck of it, this ain't too bad either:

print "<$_>" for $element->{name};

Using postfix for to temporarily alias $_ is an idiom I don't see used very much, but is quite cute, and in this case has no speed penalty (quite the opposite in fact)...

use strict; use Benchmark ':all'; open $::dummy, ">", \$::data; cmpthese(100_000, { print_for => q[ $::data = ''; print {$::dummy} "<$_>" for "a" ], printf => q[ $::data = ''; printf {$::dummy} "<%s>", "a" ], }); __END__ Rate printf print_for printf 7663/s -- -79% print_for 36765/s 380% --

(PS: kcott's follow-up was posted before this update, so please don't read it as necessarily endorsing the for postfix technique.)

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^3: How to get content of an XML::easytree output
by kcott (Archbishop) on Mar 08, 2013 at 08:22 UTC

    That, too, is good; it isolates the $element->{name} (++).

    Tworec states "I am a perl newbie" and "<$element->{name}>" didn't seem to scream clarity; in particular, the <$element-> bit.

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1022347]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found