]> git.donarmstrong.com Git - deb_pkgs/libstatistics-r-perl.git/blob - lib/Statistics/R/Win32.pm
New upstream version 0.34
[deb_pkgs/libstatistics-r-perl.git] / lib / Statistics / R / Win32.pm
1 package Statistics::R::Win32;
2
3 use strict;
4 use warnings;
5 use File::Spec    ();
6 use File::DosGlob ();
7 use Env qw( @PATH $PROGRAMFILES );
8
9 use vars qw{@ISA @EXPORT};
10 BEGIN {
11    @ISA     = 'Exporter';
12    @EXPORT  = qw{
13       win32_path_adjust
14       win32_space_quote
15       win32_space_escape
16       win32_double_bs
17    };
18 }
19
20 our $PROG = 'R';
21
22
23 =head1 NAME
24
25 Statistics::R::Win32 - Helper functions for Statistics::R on MS Windows platforms
26
27 =head1 DESCRIPTION
28
29 B<Do not use this module directly. Use L<Statistics::R> instead.>
30
31 Helper functions to deal with environment variables and escape file paths on
32 MS Windows platforms.
33
34 =head1 SYNOPSIS
35
36    if ( $^O =~ m/^(?:.*?win32|dos)$/i ) {
37       require Statistics::R::Win32;
38    }
39
40 =head1 METHODS
41
42 =over 4
43
44 =item win32_path_adjust( )
45
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.
48
49 =item win32_space_quote( )
50
51 Takes a path and return a path that is surrounded by double-quotes if the path
52 contains whitespaces. Example:
53
54    C:\Program Files\R\bin\x64
55
56 becomes
57
58    "C:\Program Files\R\bin\x64"
59
60 =item win32_space_escape( )
61
62 Takes a path and return a path where spaces have been escaped by a backslash.
63 contains whitespaces. Example:
64
65    C:\Program Files\R\bin\x64
66
67 becomes
68
69    C:\Program\ Files\R\bin\x64
70
71 =item win32_double_bs
72
73 Takes a path and return a path where each backslash was replaced by two backslashes.
74  Example:
75
76    C:\Program Files\R\bin\x64
77
78 becomes
79
80    C:\\Program Files\\R\\bin\\x64
81
82 =back
83
84 =head1 SEE ALSO
85
86 =over 4
87
88 =item * L<Statistics::R>
89
90 =back
91
92 =head1 AUTHORS
93
94 Florent Angly E<lt>florent.angly@gmail.comE<gt> (2011 rewrite)
95
96 Graciliano M. P. E<lt>gm@virtuasites.com.brE<gt> (original code)
97
98 =head1 MAINTAINERS
99
100 Florent Angly E<lt>florent.angly@gmail.comE<gt>
101
102 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
103
104 =head1 COPYRIGHT & LICENSE
105
106 This program is free software; you can redistribute it and/or
107 modify it under the same terms as Perl itself.
108
109 =head1 BUGS
110
111 All complex software has bugs lurking in it, and this program is no exception.
112 If you find a bug, please report it on the CPAN Tracker of Statistics::R:
113 L<http://rt.cpan.org/Dist/Display.html?Name=Statistics-R>
114
115 Bug reports, suggestions and patches are welcome. The Statistics::R code is
116 developed on Github (L<http://github.com/bricas/statistics-r>) and is under Git
117 revision control. To get the latest revision, run:
118
119    git clone git@github.com:bricas/statistics-r.git
120
121 =cut
122
123
124 # Adjust PATH environment variable when this module is loaded.
125 win32_path_adjust();
126
127
128 # Find potential R directories in the Windows Program Files folder and
129 # add them to the PATH environment variable.
130 sub win32_path_adjust {
131
132    # Find potential R directories, e.g.  C:\Program Files (x86)\R-2.1\bin
133    #                                 or  C:\Program Files\R\bin\x64
134    my @prog_file_dirs;
135    if (defined $PROGRAMFILES) {
136       push @prog_file_dirs, $PROGRAMFILES;                   # e.g. C:\Program Files (x86)
137       my ($programfiles_2) = ($PROGRAMFILES =~ m/^(.*) \(/); # e.g. C:\Program Files
138       if ( defined $programfiles_2 and $programfiles_2 ne $PROGRAMFILES ) {
139          push @prog_file_dirs, $programfiles_2;
140       }
141    }
142
143    # Append R directories to PATH 
144    push @PATH, grep {
145          -d $_
146       } map {
147          # Order is important
148          File::Spec->catdir( $_, 'bin', 'x64' ),
149          File::Spec->catdir( $_, 'bin' ),
150          $_,
151       } map {
152          File::DosGlob::glob( win32_space_escape( win32_double_bs($_) ) )
153       } map {
154          File::Spec->catdir( $_, $PROG, "$PROG-*" ),
155          File::Spec->catdir( $_, "$PROG-*" ),
156          File::Spec->catdir( $_, $PROG ),
157       } grep {
158          -d $_
159       } @prog_file_dirs;
160
161    return 1;
162 }
163
164
165 sub win32_space_quote {
166    # Quote a path if it contains whitespaces
167    my $path = shift;
168    $path = '"'.$path.'"' if $path =~ /\s/;
169    return $path;
170 }
171
172
173 sub win32_space_escape {
174    # Escape spaces with a single backslash
175    my $path = shift;
176    $path =~ s/ /\\ /g;
177    return $path;
178 }
179
180
181 sub win32_double_bs {
182    # Double the backslashes
183    my $path = shift;
184    $path =~ s/\\/\\\\/g;
185    return $path;
186 }
187
188
189 1;