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.


In reply to Re: Sub Return Value Help by blazar
in thread Sub Return Value Help by Anonymous Monk

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.