Im a newbie in perl. So the question might sound something naive.
I have two following functions
#This function will return the reference of the array
sub getFruits1
{
my @fruits = ('apple', 'orange', 'grape');
return \@fruits;
}
But in the following case?
#How it returns?
sub getFruits2
{
my @fruits = ('apple', 'orange', 'grape');
return @fruits;
}
Will getFruits2
return a reference and a new copy of that array will be created?
Perl always passes by reference. It's just that sometimes the caller passes temporary scalars. Perl passes by reference.
Perl provides a return statement which can be used to exit a function, and provide a value when a function call is evaluated. In the absence of a return statement, the value of a function call will be the last expression which was evaluated in the body of the function.
To return a value from a subroutine, block, or do function in Perl, we use the return() function. The Value may be a scalar, a list, or a hash value. Its context is selected at runtime.
return() function in Perl returns Value at the end of a subroutine, block, or do function. Returned value might be scalar, array, or a hash according to the selected context.
The getFruits2
subroutine returns a list, which can be assigned to a new array like this
my @newfruits = getFruits2();
And yes, it will produce a copy of the data in the array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With