Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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

by ackohno (Scribe)
on Jul 13, 2002 at 03:13 UTC ( #181441=categorized answer: print w/replies, xml ) Need Help??

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

The question is a bit vague, I assume you mean to do something like this:
#!/usr/bin/perl -w use strict; my ($num1,$num2); $num1=2; $num2=sum($num1,$num1); print "$num2\n"; # Sum: returns the sum of all arguments. sub sum { my $ret; $ret=0; for(@_){$ret+=$_} return $ret; }
return tells perl what the subroutine should give back when called, so $num2 is given the final value of $ret in the subroutine sum.

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2018-06-18 08:11 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.