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!

Displaying as HTML 1

Status
Not open for further replies.

IWIT

Vendor
Mar 5, 2004
13
GB
I have a program the same as the code before and I was wondering how you can display this as HTML in a browser. As in I have a file that I wish to display the contents of as a web page.

Thank You

Code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI "Vars";
use 5.004;
use Fcntl qw(:DEFAULT :flock);
use CGI::Carp qw(fatalsToBrowser);

my ($filename, @all, $list, @items, $item);


$filename = "msgs";

open(MYFILE,"< $filename");
@all=<MYFILE>;

$list ="@all";
@items = split /__FINISH__\n/ , $list;


for my $item (@items)
{
	my($name, $email, $subject, $text) =split /\n/, $item, 4;
	
print "Name:- $name\n";
print "Email:- $email\n";
print "Subject:- $subject\n";
print "Message:- $text\n";
}
 
what I meant by it is that I have the above program which takes the information from the file "msgs" and returns it to the screen. This above program works on our Samba server using its software. What I want to do is have the same output but in Internet Exploer. I know that to do this I need to add HTML tags. I have included below my first attempt. The below attempt does not work.

Code:
#!/usr/bin/perl

use strict;
use warnings;
use CGI "Vars";
use 5.004;
use Fcntl qw(:DEFAULT :flock);
use CGI::Carp qw(fatalsToBrowser);


my($query, $fname, $date, @all, $list, @items, $title);

$query = new CGI;                 # Object captures the command line 
$fname=$query->param('private_file');

#
# if there as a parameter private_file, use that name as the data file
# otherwise default to data.txt.
#
if (defined $fname)
{
    $title="Experimental bulletin board for $fname";
}
else
{
    $title="Experimantal bulletin board";
    $fname="../board.data";
}


#
# now get exclusive access
#

sysopen(FH, "$fname", O_RDONLY)
        or die "can't open $fname: $!";
flock(FH, LOCK_EX)
        or die "can't lock $fname: $!";

seek FH,0,0;     # now go to start

@all=<FH> or die "Could not open file\n";
close FH;

$list ="@all";
@items = split /__ITEM__\n/, $list;
print $query->header,
    $query->start_html(-title=>$title,
		       -style=>{'src' => '../mystyle.css'}),
    $query->h1 ($title),

    '<div class="indent"><div class="shaded"><p>This is an experimental
    bulletin board that only offers the most elementary features. If
    it proves to be useful, more features may be added in the
    future.</p></div></div><br><br>',

    $query->hr;

my $count = 1;
for my $item (@items)
{
    my($date, $name, $email, $subject, $text)=split /\n/, $item,5;
    $text = join('<br>',split(/\n/,$text));
    print <<FINE
<b>Item: $count</b><br>
From: <a href=\"mailto:$email\">$name</A><br>
Date: $date<br>
Subject: $subject<br>
<br>
    $text
<hr>
FINE
    ;
    $count++;
}

print $query->end_html;
 
Check your web server logs for errors and report them. Is anything being displayed on the web browser when you access this script?

 
Sorry that was the wrong code. I am getting the following errors in the browser from the below code.


Software error:
Not enough arguments for sysopen at f:\student-homes\01\0100358\cgi-bin\display.pl line 17, near ""$filename")"
Global symbol "$Subject" requires explicit package name at f:\student-homes\01\0100358\cgi-bin\display.pl line 36.
Execution of f:\student-homes\01\0100358\cgi-bin\display.pl aborted due to compilation errors.




Code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI "Vars";
use 5.004;
use Fcntl qw(:DEFAULT :flock);
use CGI::Carp qw(fatalsToBrowser);


my ($query,$filename, @all, $list, @items, $item);

$query = new CGI;
$filename=$query->param('private_file');

$filename = "msgs";

sysopen(MYFILE,"$filename");
flock(MYFILE, LOCK_EX);
seek MYFILE,0,0;	#now go to start

@all=<MYFILE>;
close MYFILE;

$list ="@all";
@items = split /__FINISH__\n/ , $list;

print $query->header,
      $query->start_html,
      $query->hr;
	
my $count = 1;
for my $item (@items)
{
	my($name, $email, $subject, $text) =split /\n/, $item, 5;
	$text = join('<br>',split(/\n/,$text));
	print <<FINE
<b>Item: $count</b><br>
From:$name<br>
Email:$email<br>
Subject: $Subject<br>
<br>
     $text
<hr>
FINE
    ;
    $count++;
}

print $query->end_html;
 
are you testing your code before submitting it to the browser?

perl -c script.cgi

and
./script.cgi xxx

you need an additional parameter for sysopen
perl here documents are
func <<"EHERE";
 
Why are you using sysopen at all, if all you're doing is reading the file?

Code:
open FH, "<myfile.txt";
print "<pre>\n";
while (<FH>) {
  print $_;
}
close FH;
print "</pre>\n";

--Paul
 
I have now changed the program so that the variable subject are both the same and I have also changed the sysopen to just open. my previous errors have now gone and I now have new ones.


CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:


Code:
open(MYFILE,"< $filename");
 
are you at all testing your code before submitting it to the browser?

perl script.cgi xxxx
 
It actually works now. The problem before was that it was running on a Windows Server and when I moved it to a UNIX one it worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top