Mario R. Sanchez, Ph.D. wrote:

> dear gurus
> 
> i have the following  script pl.pl (yes, ugly but it works...)
> -------------------

My version at end.  PS: Nobody uses counters these days.

> --------------------
> 
> this script works as i want it to - the counter adds and the graphics
> display. verify if you like at;
> http://www.dbiq.com/cgi-bin/mi/pl.pl
> 
> ok. so i want to  call it this way (no, i cant use  inline  frame, or the
> <img> thing or ssi)
> 
> <html>
> <head>
> </head>
> <body>
> hi
> <div><SCRIPT LANGUAGE="Javascript"
> SRC="http://www.dbiq.com/cgi-bin/mi/pl.pl";></SCRIPT></div>
> </body>
> </html>

Here's one way, but it will leave your script URL in the location field:

<html>

<head>
<title>Test counter</title>
</head>

<body>
<div>
<SCRIPT type="text/javascript">
window.location="http://www.dbiq.com/cgi-bin/ctr.pl";
</SCRIPT>
</div>
</body>

</html>

> the script actually executes - confirmed because 1 is added to count.txt
> BUT ... the graphics are never sent back. in fact, even regular text is
> not sent back to the browser. i've tried changing the Content-type to
> text/javascript. taking out of the print "<html ... line does not work
> either - and in this case the direct invocation stops working.
> 
> your wisdom on how the graphics/anything can be displayed back on the
> browser when the script is invoked as in the above html??

Here's my version of your script with file locking added (ctr.pl) :

#!perl --

use strict;
use warnings;
use Fcntl qw(:DEFAULT :flock);

my $file = 'count.txt';
my $digits = 4;

print "Content-type:text/html\n\n";
print <<'EOD';
<html>
<head>
<title>Test Counter</title>
</head>
<body>
ALO
EOD

END { print "</body></html>\n"; }

sysopen FH, $file, O_RDWR | O_CREAT or die "open $file: $! ($^E)";
my $ofh = select FH; $| = 1; select $ofh;       # autoflush FH
flock FH, LOCK_EX or die "LOCK_EX $file: $! ($^E)";

my $num = <FH> || 0; chomp $num; ++$num; $num = sprintf '%08u', $num;

seek FH, 0, 0 or die "rewind $file: $! ($^E)";
print FH "$num\n" or die "write $file: $! ($^E)";

truncate FH, tell (FH) or die "truncate $file: $! ($^E)";
close FH or die "close $file: $! ($^E)";

my $tmp = substr ($num, -$digits);
my @d = split //, $tmp;

# assumes gifs of 0-9 (0.gif .. 9.gif) are in /gif dir

print <<"EOD";
<br>
<center>
EOD

for (1 .. $digits) {
        my $num = $d[$_ - 1];
        print qq{<img border="0" src="/gif/$num.gif" width="36" height="40">\n};
}

print <<"EOD";
</center>

EOD

exit;

__END__
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to