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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|