| 1 | package PITA::Scheme::Perl::Discovery; | ||
| 2 | |||
| 3 | # Provides a mechanism for discovering the platform of an arbitrary | ||
| 4 | # perl interpreter, by using Process::Delegatable. | ||
| 5 | |||
| 6 | use 5.005; | ||
| 7 | use strict; | ||
| 8 | use File::Spec (); | ||
| 9 | use Params::Util ('_STRING'); | ||
| 10 | use Process (); | ||
| 11 | use Process::Storable (); | ||
| 12 | use Process::Delegatable (); | ||
| 13 | use PITA::XML (); | ||
| 14 | |||
| 15 | use vars qw{$VERSION @ISA}; | ||
| 16 | BEGIN { | ||
| 17 | $VERSION = '0.43'; | ||
| 18 | @ISA = qw{ | ||
| 19 | Process::Delegatable | ||
| 20 | Process::Storable | ||
| 21 | Process | ||
| 22 | }; | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | ##################################################################### | ||
| 30 | # Constructor and Accessors | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my $class = shift; | ||
| 34 | my $self = bless { @_ }, $class; | ||
| 35 | |||
| 36 | # Check that the program exists. | ||
| 37 | # The path should not be the command to call, it should be | ||
| 38 | # the actual file path of the Perl executable. | ||
| 39 | unless ( _STRING($self->{path}) ) { | ||
| 40 | Carp::croak("Did not provide a path to perl"); | ||
| 41 | } | ||
| 42 | unless ( File::Spec->file_name_is_absolute($self->{path}) ) { | ||
| 43 | Carp::croak("The path $self->{path} is not absolute"); | ||
| 44 | } | ||
| 45 | unless ( -f $self->{path} ) { | ||
| 46 | Carp::croak("The path $self->{path} does not exist"); | ||
| 47 | } | ||
| 48 | unless ( -x $self->{path} ) { | ||
| 49 | Carp::croak("The path $self->{path} is not executable"); | ||
| 50 | } | ||
| 51 | |||
| 52 | # Check that it is indeed Perl | ||
| 53 | my @output = `$self->{path} -v`; | ||
| 54 | chomp @output; | ||
| 55 | shift @output if $output[0] =~ /^\s*$/; | ||
| 56 | unless ( $output[0] =~ /^This is perl/ ) { | ||
| 57 | Carp::croak("The path $self->{path} is not Perl"); | ||
| 58 | } | ||
| 59 | |||
| 60 | $self; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub path { | ||
| 64 | $_[0]->{path}; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub platform { | ||
| 68 | $_[0]->{platform}; | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | ##################################################################### | ||
| 76 | # Process::Delegatable | ||
| 77 | |||
| 78 | # Allow delegate to be called without a param. | ||
| 79 | # And shortcut if already run. | ||
| 80 | sub delegate { | ||
| 81 | my $self = shift; | ||
| 82 | return 1 if defined $self->platform; | ||
| 83 | $self->SUPER::delegate( $self->path, @_ ); | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | ##################################################################### | ||
| 91 | # Process Methods | ||
| 92 | |||
| 93 | sub prepare { | ||
| 94 | # We are already running inside the Perl interpreter we want | ||
| 95 | # to capture at this point, and we have already loaded | ||
| 96 | # PITA::XML::Platform, so there's nothing else we need to do. | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub run { | ||
| 101 | my $self = shift; | ||
| 102 | |||
| 103 | # Do the autodetection | ||
| 104 | my $platform = eval { | ||
| 105 | PITA::XML::Platform->autodetect_perl5; | ||
| 106 | }; | ||
| 107 | if ( $platform ) { | ||
| 108 | $self->{platform} = $platform; | ||
| 109 | } else { | ||
| 110 | $self->{errstr} = $@; | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | ##################################################################### | ||
| 121 | # Error Methods | ||
| 122 | |||
| 123 | sub errstr { | ||
| 124 | $_[0]->{errstr}; | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; |