| 1 | package Math::Matlab::Local; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use vars qw($VERSION $ROOT_MWD $CMD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | $VERSION = sprintf "%d.%03d", q$Revision: 1.8 $ =~ /: (\d+)\.(\d+)/; | |||||||||||||||||||||||||||
| 8 | } | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Math::Matlab; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use base qw( Math::Matlab ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Cwd qw( getcwd abs_path ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | ##----- assign defaults, unless already set externally ----- | ||||||||||||||||||||||||||||
| 16 | 2 | ▶ | $CMD = 'matlab -nodisplay -nojvm' unless defined $CMD; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | $ROOT_MWD = getcwd unless defined $ROOT_MWD; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ##----- Public Class Methods ----- | ||||||||||||||||||||||||||||
| 20 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 4 | my ($class, $href) = @_; | |||||||||||||||||||||||||||
| 22 | my $self = { | ||||||||||||||||||||||||||||
| 23 | cmd => defined($href->{cmd}) ? $href->{cmd} : $CMD, | ||||||||||||||||||||||||||||
| 24 | 4 | ▶ | root_mwd => defined($href->{root_mwd}) ? $href->{root_mwd} : $ROOT_MWD, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 25 | err_msg => '', | ||||||||||||||||||||||||||||
| 26 | result => '', | ||||||||||||||||||||||||||||
| 27 | wrapper_fn => '', | ||||||||||||||||||||||||||||
| 28 | script_fn => '', | ||||||||||||||||||||||||||||
| 29 | output_fn => '', | ||||||||||||||||||||||||||||
| 30 | generated_script => undef, | ||||||||||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | bless $self, $class; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | ##----- Public Object Methods ----- | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 0 | my ($self, $code, $rel_mwd, $script_fn) = @_; | |||||||||||||||||||||||||||
| 39 | 0 | my $success = 0; | |||||||||||||||||||||||||||
| 40 | 0 | my ($cwd, $cmd); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | ## clear err_msg | ||||||||||||||||||||||||||||
| 43 | 0 | $self->clear_err_msg; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | ## save current directory and change to Matlab working directory | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $cwd = getcwd if $self->root_mwd or $rel_mwd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->root_mwd or $rel_mwd
MC/DC0%0/2
$self->root_mwd or $rel_mwd
$self->root_mwd
$rel_mwd
Truth table0%0/3
A or B
A= $self->root_mwdB= $rel_mwd
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($self->root_mwd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | chdir $self->root_mwd or die("Couldn't chdir to '@{[ $self->root_mwd ]}'"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ($rel_mwd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | my $mwd = abs_path( $rel_mwd ); | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | chdir $mwd or die("Couldn't chdir to '$mwd'"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | ## create input files | ||||||||||||||||||||||||||||
| 56 | 0 | $self->_create_input_files($code, $script_fn); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | ## set up command to fire off Matlab with the input file | ||||||||||||||||||||||||||||
| 59 | 0 | $cmd = sprintf('%s -r %s -logfile %s', | |||||||||||||||||||||||||||
| 60 | $self->cmd, substr($self->wrapper_fn, 0, -2), $self->output_fn); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | ## run it | ||||||||||||||||||||||||||||
| 63 | 0 | my $err = `$cmd 2>&1`; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (open(Matlab::IO, $self->output_fn)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | $self->{'result'} = join('', <Matlab::IO>); | |||||||||||||||||||||||||||
| 66 | 0 | close(Matlab::IO); | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($self->{'result'} =~ /-----MATLAB-BEGIN-----\n-----SUCCESS/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 68 | 0 | $success = 1; | |||||||||||||||||||||||||||
| 69 | 0 | $self->remove_files; | |||||||||||||||||||||||||||
| 70 | } elsif ($self->{'result'} =~ /-----MATLAB-BEGIN-----\n-----ERROR/) { | ||||||||||||||||||||||||||||
| 71 | ## runtime error | ||||||||||||||||||||||||||||
| 72 | $self->err_msg( | ||||||||||||||||||||||||||||
| 73 | sprintf("MATLAB RUNTIME ERROR\n[%s] in [%s] returned:\n%s", | ||||||||||||||||||||||||||||
| 74 | 0 | $cmd, getcwd, $self->{'result'} ) | |||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | ## couldn't execute Matlab code (compile err, license err) | ||||||||||||||||||||||||||||
| 78 | $self->err_msg( | ||||||||||||||||||||||||||||
| 79 | sprintf("MATLAB INITIALIZATION ERROR\n[%s] in [%s] returned:\n%s", | ||||||||||||||||||||||||||||
| 80 | 0 | $cmd, getcwd, $self->{'result'} ) | |||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | ## couldn't launch Matlab (no output file created) | ||||||||||||||||||||||||||||
| 85 | 0 | $self->err_msg( | |||||||||||||||||||||||||||
| 86 | sprintf("MATLAB LAUNCH FAILURE\n[%s] in [%s] returned:\n%s", | ||||||||||||||||||||||||||||
| 87 | $cmd, getcwd, $err ) | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | ## restore current working directory | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($cwd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | chdir $cwd or die("Couldn't chdir to '$cwd'"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | return $success; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub cmd { my $self = shift; return $self->_getset('cmd', @_); } | ||||||||||||||||||||||||||
|
Subroutine
cmd: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub root_mwd { my $self = shift; return $self->_getset('root_mwd', @_); } | ||||||||||||||||||||||||||
|
Subroutine
root_mwd: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub wrapper_fn { my $self = shift; return $self->_getset('wrapper_fn', @_); } | ||||||||||||||||||||||||||
|
Subroutine
wrapper_fn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub script_fn { my $self = shift; return $self->_getset('script_fn', @_); } | ||||||||||||||||||||||||||
|
Subroutine
script_fn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub output_fn { my $self = shift; return $self->_getset('output_fn', @_); } | ||||||||||||||||||||||||||
|
Subroutine
output_fn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub remove_files { | ||||||||||||||||||||||||||
|
Subroutine
remove_files: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | unlink $self->script_fn if $self->{generated_script} && -f $self->script_fn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"generated_script"} and -f $self->script_fn
MC/DC0%0/2
$self->{"generated_script"} and -f $self->script_fn
$self->{"generated_script"}
-f $self->script_fn
Truth table0%0/3
A and B
A= $self->{"generated_script"}B= -f $self->script_fn
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | unlink $self->wrapper_fn if -f $self->wrapper_fn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | unlink $self->output_fn if -f $self->output_fn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | $self->{script_fn} = ''; | |||||||||||||||||||||||||||
| 112 | 0 | $self->{wrapper_fn} = ''; | |||||||||||||||||||||||||||
| 113 | 0 | $self->{output_fn} = ''; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub _create_input_files { | ||||||||||||||||||||||||||
|
Subroutine
_create_input_files: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 117 | 0 | my ($self, $code, $script_fn) = @_; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ## set script file name | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if (defined($script_fn)) { ## name given | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (-f $script_fn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($code) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $self->{generated_script} = undef; | |||||||||||||||||||||||||||
| 124 | 0 | die("File '$script_fn' already exists"); | |||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||
| 126 | 0 | $self->{generated_script} = 0; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } else { ## generate random name | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | while (!defined($script_fn) or -f $script_fn ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $script_fn or -f $script_fn
MC/DC0%0/2
not defined $script_fn or -f $script_fn
not defined $script_fn
-f $script_fn
Truth table0%0/3
not(A) or B
A= not defined $script_fnB= -f $script_fn
| |||||||||||||||||||||||||||||
| 131 | 0 | $script_fn = 'mm'.(int rand 10000000).'.m'; ## generate random file name | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | $self->{generated_script} = 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | ## create script | ||||||||||||||||||||||||||||
| 137 | 0 | $self->script_fn( $script_fn ); | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ($self->{generated_script}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | $self->_create_script_file( $code ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | ## create command wrapper | ||||||||||||||||||||||||||||
| 143 | 0 | $self->_set_wrapper_fn; | |||||||||||||||||||||||||||
| 144 | 0 | $self->_create_wrapper_file; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | ## set output file name | ||||||||||||||||||||||||||||
| 147 | 0 | $self->_set_output_fn; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | return 1; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | ## Private class methods | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub _gen_script_name { return 'mm'.(int rand 10000000).'.m'; } | ||||||||||||||||||||||||||
|
Subroutine
_gen_script_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | ## Private object methods | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub _set_wrapper_fn { | ||||||||||||||||||||||||||
|
Subroutine
_set_wrapper_fn: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 157 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 158 | 0 | return $self->wrapper_fn( substr($self->script_fn, 0, -2).'_wrap.m' ); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub _set_output_fn { | ||||||||||||||||||||||||||
|
Subroutine
_set_output_fn: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 161 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 162 | 0 | return $self->output_fn( substr($self->script_fn, 0, -2).'_out.txt' ); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub _create_script_file { | ||||||||||||||||||||||||||
|
Subroutine
_create_script_file: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 166 | 0 | my ($self, $code) = @_; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | my $fn = $self->script_fn; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | open(Matlab::IO, ">$fn") || die "Couldn't open '$fn'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | print Matlab::IO $code; | |||||||||||||||||||||||||||
| 171 | 0 | close(Matlab::IO); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | return 1; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub _create_wrapper_file { | ||||||||||||||||||||||||||
|
Subroutine
_create_wrapper_file: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 177 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | my $fn = $self->wrapper_fn; | |||||||||||||||||||||||||||
| 180 | 0 | ▶ | open(Matlab::IO, ">$fn") || die "Couldn't open '$fn'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | print Matlab::IO <<END_OF_CODE; | |||||||||||||||||||||||||||
| 182 | fprintf('-----MATLAB-BEGIN-----\\n'); | ||||||||||||||||||||||||||||
| 183 | try | ||||||||||||||||||||||||||||
| 184 | 0 | rv = evalc('@{[ substr($self->script_fn, 0, -2) ]}'); | |||||||||||||||||||||||||||
| 185 | fprintf('-----SUCCESS\\n\%s', rv); | ||||||||||||||||||||||||||||
| 186 | catch | ||||||||||||||||||||||||||||
| 187 | fprintf('-----ERROR\\n\%s\\n\%s', lasterr); | ||||||||||||||||||||||||||||
| 188 | end | ||||||||||||||||||||||||||||
| 189 | quit; | ||||||||||||||||||||||||||||
| 190 | END_OF_CODE | ||||||||||||||||||||||||||||
| 191 | 0 | close(Matlab::IO); | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | return 1; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 2 | 1; | |||||||||||||||||||||||||||
| 199 | __END__ | ||||||||||||||||||||||||||||