in reply to Sub Return Value Help

What I am trying to do here is to print the results of the sub "add_row". It just creates a table, but the problem is, I need to call the sub routine as you see and perl executes the sub right there and ignores the $table_rows inside the while. How can I have the results of sub add_row used inside of the while so the template file can read it?
I apologize since it must be me being too dumb, but I really can hardly make any sense of your question; however... on to the code:
###################################################################### +###### sub print_form ###################################################################### +###### {
If you nicely indent your code there should never be any need for such heavy comments...
my $output_file = "template.sthml"; open(OUTPUT, "$output_file") || print "There is no file here, I'll run + away now!";
Huh?!? the "open or die" idiom is one of Perl's most charachteristic ones. But what does make you think that a simple print will make "run away now"?

Also, and just as importantly, if that is to be an $output_file, isn't it that by any chance you could want to open it in write mode?!?

Slightly less importantly:

$table_rows = &add_row;
The &-form of sub call is obsolete and should never be used nowadays unless when one really know what he's doing.
while(<OUTPUT>) { $_=~s/<!-- (TBROWS) \/\/-->/$table_rows/g; print $_; }
Are you sure it is an output file?!?

One nice, fundamental, point about $_ is that it is the topicalizer and as such, the default argument of many functions and operators. Thus either

  1. use named variables, or
  2. just write
    s/<!-- (TBROWS) \/\/-->/$table_rows/g; print;
All in all I suspect you may be after inline editing. In which case you should look up $^I in perldoc perlvar.

Update: re-reading the code I realize that obviously the OP's problem may just be that he calls his sub out of the while loop, then he uses the return value of that sub, saved into a variable, every time it is needed inside the loop. OTOH there are IMHO bigger problems with his code, as signalled above.

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.