Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: sub return value in qq{}

by davido (Cardinal)
on Nov 02, 2022 at 14:16 UTC ( [id://11147919]=note: print w/replies, xml ) Need Help??


in reply to sub return value in qq{}

The dereferenced arrayref technique works fine: "a string @{[foo()]}\n". But it's a little magical; it requires some tribal knowledge or testing for a reader of the code to understand what is happening.

Perl also provides printf, which is well documented and should be clearer for a reader:

sub foo { return "bar" } printf "foo returns %s\n", foo();

Additionally, there are a lot of template systems available to Perl. Template::Toolkit, Text::Template, Mojo::Template, Template::Tiny, and many others. Here is an example using Template::Toolkit:

#!/usr/bin/env perl use strict; use warnings; use Template; sub foo {return "bar"} my $output; Template ->new ->process( \"foo returns [% f %]\n", {f => foo()}, \$output, ); print $output;

This code works as-is with Template::Tiny if you just replace any mention of Template with Template::Tiny.


Dave

Log In?
Username:
Password:

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

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

    No recent polls found