13 Parameters: string, regexp
15 Returns the number of times of regexp occurs in string.
21 my( $str, $regexp ) = @_;
24 while( $str =~ /$regexp/s ) {
33 =head2 findCommonPrefix
35 Parameters: string, string
37 Returns the prefix common to both strings. An empty string
38 is returned if the strings have no common prefix.
44 my @s1 = split( "/", $_[0] );
45 my @s2 = split( "/", $_[1] );
47 my $len = ($#s2 > $#s1 ) ?
$#s1 : $#s2;
49 for my $i ( 0..$len ) {
50 # print "Compare: $i '$s1[$i]', '$s2[$i]'\n";
51 last if $s1[ $i ] ne $s2[ $i ];
52 $accum .= $s1[ $i ]."/";
58 =head2 makeRelativePath
60 Parameters: localpath, destpath
62 Returns a relative path to the destination from the local path,
63 after removal of any common prefix.
69 my ( $from, $to ) = @_;
72 $from .= '/' unless $from =~ m
#/$#;
73 $to .= '/' unless $to =~ m
#/$#;
75 my $pfx = findCommonPrefix
( $from, $to );
81 # print "Prefix is '$pfx'\n";
85 $pfx = countReg
( $from, '\/' );
87 my $rel = "../" x
$pfx;
96 my @hostenvs = qw( HOST HOSTNAME COMPUTERNAME );
99 foreach my $evar ( @hostenvs ) {
100 next unless defined $ENV{ $evar };
102 $host = $ENV{ $evar };
117 my @userenvs = qw( USERNAME USER LOGNAME );
120 foreach my $evar ( @userenvs ) {
121 next unless defined $ENV{ $evar };
123 $who = $ENV{ $evar };
128 if ( $who = `whoami` ) {
131 elsif ( $who - `who am i` ) {
132 $who = ( split (/ /, $who ) )[0];
140 Helper to split a list using a delimiter, but looking for
141 nesting with (), {}, [] and <>.
142 Example: splitting int a, QPair<c,b> d, e=","
143 on ',' will give 3 items in the list.
145 Parameter: delimiter, string
146 Returns: array, after splitting the string
148 Thanks to Ashley Winters
150 sub splitUnnested
($$) {
159 my(%close) = reverse %open;
163 my $indoublequotes = 0;
164 my $insinglequotes = 0;
165 while($string =~ /($delim|<<|>>|[][}{)(><\"\'])/g) {
167 if(!$insinglequotes and !$indoublequotes) {
168 if(!$depth and $c eq $delim) {
169 my $len = pos($string) - $start - 1;
170 push @ret, substr($string, $start, $len);
171 $start = pos($string);
172 } elsif( $c eq "'") {
174 } elsif( $c eq '"') {
178 } elsif($close{$c}) {
181 } elsif($c eq '"' and $indoublequotes) {
183 } elsif ($c eq "'" and $insinglequotes) {
188 my $subs = substr($string, $start);
189 push @ret, $subs if ($subs);