#!/usr/bin/perl -w
######################################################################
# nph-boom.pl
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

package ServerPush;

use CGI qw/:standard/;                  # export header()

sub new {
    my ($class, $sep) = shift;          # $sp = ServerPush->new($sep)

    my $self               = {};        # instance variable hash
    $sep                 ||= "PartDocSeparator";
    $self->{separator}     = $sep;      # partial document separator
    $self->{header_sent}   = 0;         # first header sent?
    $| = 1;                             # debuffer output
    bless($self, $class);
}

sub server_push {                    
    my ($self, $content, $terminate) = @_; 

    if(!$self->{header_sent}) {
        print header('-nph'  => 1,      # output header
                     '-type' => 
               "multipart/x-mixed-replace;boundary=$self->{separator}"
                    );
        print "\n--$self->{separator}\n"; 
        $self->{header_sent} = 1;       # set flag
    }
 
    print header(), "$content\n";
    print "\n--$self->{separator}", 
          defined $terminate ? "--" : "", "\n";
}

######################################################################
package main;

use CGI qw/:standard/;                      # export header()

$sp = ServerPush->new();                    # new server oush object

$sp->server_push(h1(2));                    # <H1>2</H1> with separator
sleep(1);
$sp->server_push(h1(1));                    # <H1>1</H1> with separator
sleep(1);
                                            # <H1>Boom!</H1> with 
$sp->server_push(h1("Boom!"), "terminate"); # terminating separator

