Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print .txt file

Status
Not open for further replies.

rob51383

Programmer
Jun 23, 2004
134
US
How do I open a .txt or HTML file that contains HTML and print the entire page to the browser? Here is a coded visual example of what i want to do:


Code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";

open (IN, "/the/path/head.txt");
while (<IN>)
 {
 print every line of the .txt file
 }
close (IN);

#A bunch of code that is filled in after the html#

open (IN, "/the/path/foot.txt");
while (<IN>)
 {
 print every line of the .txt file
 }
close (IN);


 
Code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";

open (IN, "/the/path/head.txt");
@HEAD = <IN>;
close <IN>;
printarray(@HEAD);

#A bunch of code that is filled in after the html#

open (IN, "/the/path/foot.txt");
@FOOT = <IN>;
close IN;
printarray(@FOOT);

sub printarray{
  my (@in) = @_;
  foreach (@in) {
    print $_;
  }
}
 
Works good (You used close <IN> in first statement, ommit the <> like the second one and it is good). Works like I expected it to though..Didn't pass my variables I have coded in the .txt file. Is there any way for it to read the variables I have (i.e.):

The script looks liike this:
Code:
$path="../products";

open (IN, "/the/path/head.txt");
@HEAD = <IN>;
close IN;
printarray(@HEAD);

sub printarray{
  my (@in) = @_;
  foreach (@in) {
    print $_;
  }
}

#### Which can print an HTML line like this#########

<img src="$path/img.jpg">



So, basically, is there any way to pass variable values into the extracted HTML prior to printing it?

 
You will have to do more fiddling to do that, looks like you are doing a template system type thing.

You can do:

Code:
$path="../products";

open (IN, "/the/path/head.txt");
@HEAD = <IN>;
close IN;
printarray(@HEAD);

sub printarray{
  my (@in) = @_;
  foreach (@in) {
    print $_;
  }
}

sub dosubs{
  my $line = shift;

  # put the substitutions in here for each variable
  $line = s/\$path/$path/g;

  return $line;
}
 
Try again!!! ;)

Code:
$path="../products";

open (IN, "/the/path/head.txt");
@HEAD = <IN>;
close IN;
printarray(@HEAD);

sub printarray{
  my (@in) = @_;
  foreach (@in) {
    print dosubs($_);
  }
}

sub dosubs{
  my $line = shift;

  # put the substitutions in here for each variable
  $line = s/\$path/$path/g;

  return $line;
}
 
You may want to consider looking into using a template module to complete what you're looking for to save time and energy in reinventing the wheel. I've used both the Template Toolkit and HTML::Template modules and they work wonders for what you're trying to accomplish.

- Rieekan
 
I've not used the template systems myself, but I wrote a similar one just for a couple of simple things I did. Your page would look like this:

Code:
%replace = ("path" => "../products");

open (IN, "/the/path/head.txt");
@HEAD = <IN>;
close IN;
printarray(@HEAD);

sub printarray{
  my (@in) = @_;
  foreach (@in) {
    print dosubs($_);
  }
}

sub dosubs{
  my $line = shift;
  my $key;
  my $val;

  while (($key, $val) = each %replace) {
    $line =~ s/REPLACE:$key/$val/g;
  }
  return $line;
}


and instead of having:
Code:
<img src="$path/img.jpg">

you have:
Code:
<img src="REPLACE:path/img.jpg">
 
For you info... if you need to pring all the elements of an array... you don't need to do a foreach...
Code:
# THIS WORKS!
open(IN, "<myfile.txt") ; # '<' meaning "open for reading!"
my @tmpArray = <IN> ;
print @tmpArray ;
close(IN) ;

Just to complete everything that has been said!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top