in reply to Simple Perl Question: Printing the Return Value of a Function

As is typical for Perl, there's more than one way to do it.

You can use it in the list of arguments to print:

print "Result: ", $object->function_call(), "\n";

You can concatenate it as a string into a single argument to print:

print "Result: " . $object->function_call() . "\n";

There is also a "trick" you can use to evaluate a function in the middle of a quoted section of text:

print "Result: @{ [ $object->function_call() ] }\n";

This causes Perl to create an anonymous array (the square brackets) with the results of the function and immediately dereference it to an array ( the @{ } part ). Within double-quotes, that array is joined with spaces and inserted into the string.

That extra formatting is often a nice touch versus the other options above, though some people think it's clearer to do something like this:

print "Result: " . join(" ", $object->function_call() ) . "\n";

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.