use JSON;
$jsonArr = false;
if ($json_string) {
eval { $jsonArr = decode_json $json_string };
}
if ($jsonArr) {
for (@{$jsonArr}) {
$pic = $_->{'name'};
$uuid = $_->{'uuid'};
# do stuff
}
} sub myJSONDecoder {
my @finalArr;
$_ = $_[0];
s/^\s*\[\s*{\s*|\s*}\s*\]\s*$//g;
my @firstArr = split(/\s*}\s*,\s*{/);
for ($i = 0; $i <= $#firstArr; $i++) {
my @secondArr = split(/\s*,\s*/, $firstArr[$i]);
for (@secondArr) {
# I recognize that this next regex would break if there's a ' inside of a " or
# vice versa. That's not an issue for my usage, but YMMV
s/["']//g;
my @thirdArr = split(':');
$finalArr[$i]{$thirdArr[0]} = $thirdArr[1];
}
}
return @finalArr;
}
@jsonArr = myJSONDecoder($json_string);
for (@jsonArr) {
$pic = $_->{'name'};
$uuid = $_->{'uuid'};
# do stuff
} I have a few functions that I use in multiple scripts, so I have them in a separate file that I include using require foo.lib;.
But I've read some posts (mostly much older) saying that "require" is very slow, and it's better to use a module.
Am I right that this is all that it takes to convert it to a module?
[edited by: phranque at 3:08 am (utc) on Dec 6, 2022]
Or more like 296 if I change the variable names strategically,
Are modules by nature faster to process? Or is it just that it will be more likely to be stable?