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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.