Alien-Gnuplot

 view release on metacpan or  search on metacpan

lib/Alien/Gnuplot.pm  view on Meta::CPAN

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

package Alien::Gnuplot;

use strict;
our $DEBUG = 0; # set to 1 for some debugging output

use parent qw( Alien::Base );

use File::Spec;
use File::Temp qw/tempfile/;
use File::Which;
use Time::HiRes qw/usleep/;
use POSIX ":sys_wait_h";
use Fcntl qw/SEEK_SET/;
use Env qw( @PATH );

# VERSION here is for CPAN to parse -- it is the version of the module itself.  But we

lib/Alien/Gnuplot.pm  view on Meta::CPAN

variable or make sure your PATH contains it.  If you do not have
gnuplot, you can reinstall Alien::Gnuplot (and its installation 
script will try to install gnuplot) or get it yourself from L<https://gnuplot.sourceforge.net/>.
};
    }
    
##############################
# Execute the executable to make sure it's really gnuplot, and parse
# out its reported version.  This is complicated by gnuplot's shenanigans
# with STDOUT and STDERR, so we fork and redirect everything to a file.
# The parent process gives the daughter 2 seconds to report progress, then
# kills it dead.
    my($pid);
    my ($undef, $file) = tempfile();

    # Create command file
    open FOO, ">${file}_gzinta";
    print FOO "show version\nset terminal\n\n\n\n\n\n\n\n\n\nprint \"CcColors\"\nshow colornames\n\n\n\n\n\n\n\nprint \"FfFinished\"\nexit\n";
    close FOO;

    if($^O =~ /MSWin32/i) {

lib/Alien/Gnuplot.pm  view on Meta::CPAN

		    open STDIN, "<${file}_gzinta";
		    seek STDIN, 0, SEEK_SET;
		    no warnings; 
		    exec($exec_path);
		    print BAR "Execution of $exec_path failed!\n";
		    exit(1);
		}; 
		print STDERR "Alien::Gnuplot: Unknown problems spawning '$exec_path' to probe gnuplot.\n";
		exit(2); # there was a problem!
	    } else {
		# parent
		# Assume we're more POSIX-compliant...
		if($DEBUG) { print "waiting for pid $pid (up to 20 iterations of 100ms)"; flush STDOUT; }
		for (1..20) {
		    if($DEBUG) { print "."; flush STDOUT; }
		    if(waitpid($pid,WNOHANG)) {
			$pid=0;
			last;
		    }
		    usleep(1e5);
		}
		if($DEBUG) { print "\n"; flush STDOUT; }
		
		if($pid) {
		    if( $DEBUG) { print "gnuplot didn't complete.  Killing it dead...\n"; flush STDOUT; }
		    kill 9,$pid;   # zap
		    waitpid($pid,0); # reap
		}
	    } #end of parent case
	} else {
	    # fork returned undef - error.
	    die "Alien::Gnuplot: Couldn't fork to test gnuplot! ($@)\n";
	}
    }
    
##############################
# Read what gnuplot had to say, and clean up our mess...
    open FOO, "<$file";
    my @lines = <FOO>;



( run in 0.233 second using v1.01-cache-2.11-cpan-4d50c553e7e )