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

Answer: How can I write a function's output to a scalar?

( #182092=categorized answer: print w/ replies, xml ) Need Help??

Q&A > subroutines > How can I write a function's output to a scalar? contributed by Abigail-II

You can do it with a pipe-open, as described in man perlipc.
#!/usr/bin/perl use strict; use warnings 'all'; sub my_function { # Does a lot, then writes print "Foobar!"; } my $pid = open my $kid => "-|"; die "Failed to fork: $!\n" unless defined $pid; unless ($pid) { my_function; exit; } my $result; { local $/; $result = <$kid>; close $kid or die "Failed to close pipe: $!\n"; } print "Got: $result\n";

Abigail

Comment on Answer: How can I write a function's output to a scalar?
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2015-07-30 05:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls