Subject: | Suggested code: perlify_plist() |
Date: | Thu, 23 Feb 2012 12:02:14 +0000 |
To: | bug-Mac-PropertyList [...] rt.cpan.org |
From: | James Gilbert <jgrg [...] sanger.ac.uk> |
Hi Brian,
I just used Mac::PropertyList (I was using 1.33_02) to write some code
which sets http_proxy, ftp_proxy etc... in %ENV, based on the current
network config found in:
/Library/Preferences/SystemConfiguration/preferences.plist
Needing to navigate the parsed data, I though plist_as_perl() sounded
like what I wanted, but it wasn't useful. So I wrote the appended
code, which I thought you might like to include, or something with
similar functionality.
Thanks for writing Mac::PropertyList,
James
# Takes a Mac::PropertyList data structure and coverts its objects
into a
# (more easily usable) plain perl datastructure.
sub perlify_plist {
my ($item) = @_;
if ($item->type eq 'dict') {
my $hash = {$item->value};
while (my ($key, $val) = each %$hash) {
$hash->{$key} = perlify_plist($val);
}
return $hash;
}
elsif ($item->type eq 'array') {
my $array = [];
foreach my $val ($item->value) {
push(@$array, perlify_plist($val));
}
return $array;
}
else {
return $item->value;
}
}
--
The Wellcome Trust Sanger Institute is operated by Genome Research
Limited, a charity registered in England with number 1021457 and a
company registered in England with number 2742969, whose registered
office is 215 Euston Road, London, NW1 2BE.