in reply to losing html in print calling function.

Within the function, you're modifying variables not declared within the function—is it possible you're clobbering variables declared elsewhere? In specific, the use of $record looks suspicious. What happens if you say:

sub fn1 { my ($fh, $ucsearch) = @_; my $count = 0; while (my $record = <$fh>) { next unless uc($record) =~ /$ucsearch/; print $record; $count++; } return $count; }

You'll have to change how you call the function (and you'll have to use its return value), but the lexical encapsulation may help avoid weird action at a distance.

If that doesn't help, you'll have to show more code.


Improve your skills with Modern Perl: the free book.

Replies are listed 'Best First'.
Re^2: losing html in print calling function.
by LesleyB (Friar) on Nov 21, 2011 at 16:17 UTC

    Apple seemed to work fine for me on your search page

    Definitely a +1 from me on Chromatic's suggestion, which made your intention much clearer to me.

    It is wise to encapsulate actions as suggested and I would also urge you to review the way you are managing the HTML. If you decide at some point in the future to modify the HTML output, then you've got a bug maintenance/consistency problem.

    My principle is that if I start copying and pasting bits of code then it needs to be extracted and made a subroutine. The first if statement block is similar all the way through with the content variation dependent on the file name. The second block, aka the else block, differs but is presumably still a function of the file + some other component.

    So I'd take chromatic's code and add in

    sub fn1 { my ($fh, $ucsearch ) = @_; my $count = 0; while (my $record = <$fh>) { next unless uc($record) =~ /$ucsearch/; print $record; $count++; } return $count; } ## data is a reference to an array of hashes data structure containing ## the full file name, the file basename, title, and something that + helps decide what you are going to print if the file doesn't open. ## setdata is a subroutine which creates and fills the data structure, + then returns a reference to it ## hint: look the core module File::Basename (http://perldoc.perl.or +g/File/Basename.html) to extract the file basename my $data=setdata(); foreach my $datum (@$data) ( if (open(my $fh, $prestring.$data->{'filename'}) { print "<center><img border=0 src=/article_separator.png><br>< +B>".$data->{'basename'}.":".$data->{'title'}."</B></center><br>"; fn1($fh,$ucsearch); } else { ## print according to the characteristic that decides if the fil +e has no ATOCI or does not exist } }

    That helps with maintaining the HTML a little. It would be better to then look at HTML::Template, (which has a tutorial here), for a way to separate your HTML from your code.

      Apple now works.

      The problem has cleared, but I am not sure just which change fixed it. Chromatic I think hit it, I changed from $record to $record1 inside the function.

      Now I have inconsistancy in how many page breaks are displayed. I suspect a string trim of np characters function will need to be added.

      Let me experiment a bit more before I report back.

Re^2: losing html in print calling function.
by Librum (Initiate) on Nov 21, 2011 at 16:17 UTC

    Chromatic,

    No, no redeclaring of functions declared elsewhere. Triple checked. But I think you have pointed me in the right directions. the $ucsearch function... I wonder if it uses the $record

    Going back to test that now...

    Thanks!