File Coverage

File:lib/HTML/Display/TempFile.pm
Coverage:100.0%

linestmtbrancondsubpodtimecode
1package HTML::Display::TempFile;
2use strict;
3use parent 'HTML::Display::Common';
4use 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
27sub 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
45sub 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
54sub browsercmd { $_[0]->{browsercmd} };
55
561;