in reply to reference question

It's all a matter of what's being passed in, and how the function accepts it. You can pass anything you want into a function, so you need to be sure that you're passing what the function wants.

Here's two samples:

my @files = qw( foo bar bat ); @files = byvalue( @files ); byreference( \@files ); sub add_txt_by_value { my @array = @_; foreach ( @array ) { $_ .= ".txt"; } return @array; } sub add_txt_by_reference { my $arrayref = shift; foreach ( @$arrayref ) { $_ .= ".txt"; } }
Note that when we pass by value, we have to pass back the array as a return value. When we pass by ref, we're modifying the array in the caller's space.

Updated: s/$array/$arrayref/ in the second example. Thanks, Hofmator.

xoxo,
Andy
--
<megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

Replies are listed 'Best First'.
Re: Re: reference question
by Hofmator (Curate) on Aug 14, 2001 at 19:57 UTC

    and as a third alternative - change the passed arguments directly:

    sub add_txt_directly { foreach ( @_ ) { $_ .= '.txt'; } }

    -- Hofmator