Skip to content

Instantly share code, notes, and snippets.

@patch
Created October 29, 2012 16:16
Show Gist options
  • Save patch/3974568 to your computer and use it in GitHub Desktop.
Save patch/3974568 to your computer and use it in GitHub Desktop.
parse key/string pairs in xmlish markup
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dump qw( dump );
# slurp in filehandle as one string
my $input = do { local $/; <DATA> };
my %data;
# parse out key/string pairs into hash
$data{ $+{key} } = $+{string} while $input =~ m{
<key> (?<key> .+? ) </key> \s*
<string> (?<string> .*? ) </string>
}xg;
dump(\%data);
__DATA__
<key>_name</key>
<string>Fibre Channel Domain 0</string>
<key>spfibrechannel_addressidentifier</key>
<string>04:0B:00</string>
<key>spfibrechannel_bus</key>
<string>spfibrechannel_pci_device</string>
<key>spfibrechannel_cablingtype</key>
<string>Fiber Optic</string>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment