Yes it's weird, I just downloaded the code again, and it worked fine, but I still have the original file which gave me errors. I include it here, just because I can't see where the difference is? Maybe a } got clipped somewhere? It has to be Monday. :-)
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my @AoH = (
{ OutFile => 'albu.htm',
PageId => '3.4.1.5.1',
Menu => 'albu_menu.inc',
Content => 'albu.inc'
},
{ OutFile => 'armi.php',
PageId => '3.4.1.5.2',
Menu => 'armi_menu.inc',
Content => 'armi.inc'
},
{ OutFile => 'bank.htm',
PageId => '3.4.1.5.3',
Menu => 'bank_menu.inc',
Content => 'bank.inc'
},
{ OutFile => 'bate.htm',
PageId => '3.4.1.5.4',
Menu => 'bate_menu.inc',
Content => 'bate.inc'
}
);
my $benc = bEncode(\@AoH);
print $benc,"\n";
my $bdec = bDecode(\$benc);
print Dumper([\$bdec]),"\n";
#
#####
# Provides fuctions for bencoding/decoding
###
# Decodes a bencoded string to a hash/array reference
sub bDecode {
my $dictref = $_[0];
my $retref = undef;
if (substr(${$dictref},0,1) eq "d") {
$retref = {};
${$dictref} = substr(${$dictref},1);
PROCDICTHASH: while (substr(${$dictref},0,1) ne 'e') {
my ($key, $value);
${$dictref} =~ /^(-?[\d]+)/;
$key = substr(${$dictref}, length($1)+1, $1);
${$dictref} = substr(${$dictref}, length($1)+$
+1+1);
if (${$dictref} =~ /^[ld]/) {
$value = bDecode(\${$dictref});
if (!defined($value)) {
return undef;
}
} elsif (${$dictref} =~ /^(-?[\d]+):/) {
$value = substr(${$dictref}, length($1
+)+1, $1);
${$dictref} = substr(${$dictref}, leng
+th($1)+$1+1);
} elsif (${$dictref} =~ /^i(-?[\d]+)e/) {
$value = $1;
${$dictref} = substr(${$dictref}, leng
+th($1)+2);
} else {
return undef;
}
$retref->{$key} = $value;
}
${$dictref} = substr(${$dictref},1);
} elsif (substr(${$dictref},0,1) eq "l") {
$retref = [];
${$dictref} = substr(${$dictref},1);
PROCDICTARR: while (substr(${$dictref},0,1) ne 'e') {
if (${$dictref} =~ /^[ld]/) {
my $value = bDecode(\${$dictref});
if (!defined($value)) {
return undef;
}
push (@$retref, $value);
} elsif (${$dictref} =~ /^(-?[\d]+):/) {
push (@$retref, substr(${$dictref}, le
+ngth($1)+1, $1));
${$dictref} = substr(${$dictref}, leng
+th($1)+$1+1);
} elsif (${$dictref} =~ /^i(-?[\d]+)e/) {
push (@$retref, $1);
${$dictref} = substr(${$dictref}, leng
+th($1)+2);
} else {
return undef;
}
}
${$dictref} = substr(${$dictref},1);
} else {
return undef;
}
return $retref;
}
#Encodes a hash/array ref to a bencoded string
sub bEncode {
my $dictref = $_[0];
my $retval = '';
# my ($key, $value);
if (ref($dictref) eq 'HASH') {
$retval = 'd';
for my $key (sort keys %$dictref) {
$retval .= length($key).':'.$key;
if (ref($dictref->{$key}) eq 'ARRAY' || ref($d
+ictref->{$key}) eq 'HASH') {
$retval .= bEncode($dictref->{$key});
} elsif ($dictref->{$key} =~ /^[\d]+$/) {
$retval .= 'i'.$dictref->{$key}.'e';
} else {
$retval .= length($dictref->{$key}).':
+'.$dictref->{$key};
}
}
$retval .= 'e';
} elsif (ref($dictref) eq 'ARRAY') {
$retval = 'l';
for (my $i = 0; $i <= $#{$dictref}; $i++) {
if (ref($dictref->[$i]) eq 'ARRAY' || ref($dic
+tref->[$i]) eq 'HASH') {
$retval .= bEncode($dictref->[$i]);
} elsif ($dictref->{$key} =~ /^[\d]+$/) {
$retval .= 'i'.$dictref->[$i].'e';
} else {
$retval .= length($dictref->[$i]).':'.
+$dictref->[$i];
}
}
$retval .= 'e';
}
return $retval;
};