1 package Statistics::R::Win32;
6 use File::Spec::Functions;
7 use File::DosGlob qw( glob );
8 use Env qw( @PATH $PROGRAMFILES );
10 use vars qw{@ISA @EXPORT};
27 Statistics::R::Win32 - Helper functions for Statistics::R on MS Windows platforms
31 Helper functions to deal with environment variables and escape file paths on
36 if ( $^O =~ m/^(?:.*?win32|dos)$/i ) {
37 require Statistics::R::Win32;
44 =item win32_path_adjust( )
46 Looks for paths where R could be installed, e.g. C:\Program Files (x86)\R-2.1\bin
47 and add it to the PATH environment variable.
49 =item win32_space_quote( )
51 Takes a path and return a path that is surrounded by double-quotes if the path
52 contains whitespaces. Example:
54 C:\Program Files\R\bin\x64
58 "C:\Program Files\R\bin\x64"
60 =item win32_space_escape( )
62 Takes a path and return a path where spaces have been escaped by a backslash.
63 contains whitespaces. Example:
65 C:\Program Files\R\bin\x64
69 C:\Program\ Files\R\bin\x64
73 Takes a path and return a path where each backslash was replaced by two backslashes.
76 C:\Program Files\R\bin\x64
80 C:\\Program Files\\R\\bin\\x64
88 =item * L<Statistics::R>
94 Florent Angly E<lt>florent.angly@gmail.comE<gt> (2011 rewrite)
96 Graciliano M. P. E<lt>gm@virtuasites.com.brE<gt> (original code)
100 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
102 =head1 COPYRIGHT & LICENSE
104 This program is free software; you can redistribute it and/or
105 modify it under the same terms as Perl itself.
109 All complex software has bugs lurking in it, and this program is no exception.
110 If you find a bug, please report it on the CPAN Tracker of Statistics::R:
111 L<http://rt.cpan.org/Dist/Display.html?Name=Statistics-R>
113 Bug reports, suggestions and patches are welcome. The Statistics::R code is
114 developed on Github (L<http://github.com/bricas/statistics-r>) and is under Git
115 revision control. To get the latest revision, run:
117 git clone git@github.com:bricas/statistics-r.git
122 # Adjust PATH environment variable when this module is loaded.
126 sub win32_path_adjust {
127 # Find potential R directories in the Windows Program Files folder and add
128 # them to the PATH environment variable
130 # Find potential R directories, e.g. C:\Program Files (x86)\R-2.1\bin
131 # or C:\Program Files\R\bin\x64
134 if (defined $PROGRAMFILES) {
135 push @prog_file_dirs, $PROGRAMFILES; # e.g. C:\Program Files (x86)
136 my ($programfiles_2) = ($PROGRAMFILES =~ m/^(.*) \(/); # e.g. C:\Program Files
137 push @prog_file_dirs, $programfiles_2 if not $programfiles_2 eq $PROGRAMFILES;
139 for my $prog_file_dir ( @prog_file_dirs ) {
140 next if not -d $prog_file_dir;
142 my @globs = ( catfile($prog_file_dir, $PROG), catfile($prog_file_dir, $PROG.'-*') );
143 for my $glob ( @globs ) {
144 $glob = win32_space_escape( win32_double_bs( $glob ) );
145 push @subdirs, glob $glob; # DosGlob
147 for my $subdir (@subdirs) {
148 my $subdir2 = catfile($subdir, 'bin');
150 my $subdir3 = catfile($subdir2, 'x64');
152 push @r_dirs, $subdir3;
154 push @r_dirs, $subdir2;
156 push @r_dirs, $subdir;
160 # Append R directories to PATH (order is important)
167 sub win32_space_quote {
168 # Quote a path if it contains whitespaces
170 $path = '"'.$path.'"' if $path =~ /\s/;
175 sub win32_space_escape {
176 # Escape spaces with a single backslash
183 sub win32_double_bs {
184 # Double the backslashes
186 $path =~ s/\\/\\\\/g;