http://qs321.pair.com?node_id=189981

Maclir has asked for the wisdom of the Perl Monks concerning the following question:

Ok, it is late and my brain is fried. But I cannot see what I am doing wrong. I want call a subroutine, passing two variables as references. My understanding is that this is effectively a "call by name", and any modifications the subroutine makes to the variables within the subroutine scope will be reflected in the main program.

Here is a sample program:

#!/usr/bin/perl -w $template_file = "test.pl"; $template = "old value"; print "Before call: $template \n"; $cnt = read_template(\$template_file, \$template); print "$cnt lines read \n"; print "after call: $template \n"; sub read_template { my $file_name = ${shift()}; my $lines_in = ${shift()}; my $line_cnt = 0; if (!open (INPUT, "< $file_name")) { die "Can't open $file_name as input."; } else { while (<INPUT>) { $line_cnt++; $lines_in .= $_; } close (INPUT); } return($line_cnt); }

and here is the output:

Before call: old value 29 lines read after call: old value

What wrong assumptions have I made?

By the way, I am calling the subs this way as I am working on a CGI program that I want to make "mod_perl" safe