-
Notifications
You must be signed in to change notification settings - Fork 1
/
URLTool.pm
56 lines (47 loc) · 1.18 KB
/
URLTool.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/perl
package URLTool;
##
## URLTool.pm
##
## Right now just has one method called key_replace for doing search and replace
## on placeholders in URLs
##
## eg:
##
## my $url = key_replace('http://some_url/script.cgi?tax_id={taxon_id}&type={some_type}', {
## taxon_id => '4081',
## some_type => 'a_type',
## });
##
## This is to use for storing generic URLs in a DB (or elsewhere) and then subsequently
## instantiating real URLs with the values populated programmatically.
##
## Brett Whitty
## whitty@msu.edu
##
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(key_replace); # symbols to export
use strict;
use warnings;
use Carp;
sub key_replace {
my ($url, $args, $open, $close) = @_;
unless(defined($open)) {
$open = '{';
}
unless(defined($close)) {
$close = '}';
}
while ($url =~ /\Q$open\E(.*?)\Q$close\E/gs) {
my ($m_start, $m_end) = ($-[0], $+[0]);
my $key = $1;
if (defined($args->{$key})) {
$url = substr($url, 0, $m_start) . $args->{$key} . substr($url, $m_end);
} else {
carp "Unmatched placeholder '$key' in:\n$url";
}
}
return $url;
}
1;