Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Sub Params as references by Maclir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found