LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Do Perl functions usually modify variables, or return modified copies? Why? (https://www.linuxquestions.org/questions/programming-9/do-perl-functions-usually-modify-variables-or-return-modified-copies-why-286921/)

johnMG 02-06-2005 11:13 AM

Do Perl functions usually modify variables, or return modified copies? Why?
 
Do Perl functions usually modify variables, or return modified copies?

I know I can just try out various functions and find out for myself, but what's the rationale for whichever is more perlish?

paulsm4 02-06-2005 01:00 PM

SHORT ANSWER:
Perl passes references to its parameters (rather than make copies of them). So if a parameter is modified in a Perl subroutine, then that value will also be modified in the caller.

LONGER ANSWER:
There are many different ways to declare and invoke Perl subroutines, and many ways to handle Perl subroutine parameters. You can, for example, do recursion (which requires "pass by value" semantics) in Perl.

johnMG 02-06-2005 09:02 PM

Darn. Sorry Paul. My initial question was faulty. I meant with *built-in* functions. Any further insight is appreciated. :)

chrism01 02-06-2005 10:22 PM

It depends ;)
eg sin(expr) rtns the sin of the value supplied, otoh ''pop': Pops and returns the last value of the array, shortening the array by one element'.
See http://search.cpan.org/~nwclark/perl...Perl_Functions
for a definitive list.
Enjoy :)


All times are GMT -5. The time now is 06:34 PM.