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
|