line | stmt | bran | cond | sub | pod | time | code |
1 | | | | | | | package HTML::Display::TempFile; |
2 | | | | | | | use strict; |
3 | | | | | | | use parent 'HTML::Display::Common'; |
4 | | | | | | | use vars qw($VERSION); |
5 | | | | | | | $VERSION='0.38'; |
6 | |
7 - 25 | | =head1 NAME
HTML::Display::TempFile - base class to display HTML via a temporary file
=head1 SYNOPSIS
=for example begin
package HTML::Display::External;
use parent 'HTML::Display::TempFile';
sub browsercmd {
# Return the string to pass to system()
# %s will be replaced by the temp file name
};
=for example end
=cut |
26 | |
27 | | | | | | | sub display_html { |
28 | | | | | | | # We need to use a temp file for communication |
29 | | | | | | | my ($self,$html) = @_; |
30 | |
31 | | | | | | | $self->cleanup_tempfiles; |
32 | |
33 | | | | | | | require File::Temp; |
34 | | | | | | | my($tempfh, $tempfile) = File::Temp::tempfile(undef, SUFFIX => '.html'); |
35 | | | | | | | print $tempfh $html; |
36 | | | | | | | close $tempfh; |
37 | |
38 | | | | | | | push @{$self->{delete}}, $tempfile; |
39 | |
40 | | | | | | | my $cmdline = sprintf($self->browsercmd, $tempfile); |
41 | | | | | | | system( $cmdline ) == 0 |
42 | | | | | | | or warn "Couldn't launch '$cmdline' : $?"; |
43 | | | | | | | }; |
44 | |
45 | | | | | | | sub cleanup_tempfiles { |
46 | | | | | | | my ($self) = @_; |
47 | | | | | | | for my $file (@{$self->{delete}}) { |
48 | | | | | | | unlink $file |
49 | | | | | | | or warn "Couldn't remove tempfile $file : $!\n"; |
50 | | | | | | | }; |
51 | | | | | | | $self->{delete} = []; |
52 | | | | | | | }; |
53 | |
54 | | | | | | | sub browsercmd { $_[0]->{browsercmd} }; |
55 | |
56 | | | | | | | 1; |