The following code fails with an error saying that parse_json
is undefined.
use strict;
use JSON::Parse;
my $x = "['a', 'b']";
my $json = parse_json($x);
But, this page claims it works: https://metacpan.org/pod/JSON::Parse
What am I doing wrong?
2
Answers
The documentation repeatedly uses
and
while you used
While all three load the module, only the former two exports
parse_json
.I believe it’s a good practice to list your imports even when you’re not required as it makes it a whole lot easier to work with code down the line. This is not relevant here because listing the imports is required.
What @ikegami said, but here’s a little more info.
Each module can decide how it "exports" symbols (variables or subs) to the loading namespace. And, since this is Perl, individual modules make different decisions about this. You know what they are doing by their documentation rather than what you have seen from another module.
Some module export symbols without you asking if you don’t specify an import list:
Some modules require that you explicitly ask for symbols in your import list, and only export those:
And you can ask to import nothing with an empty import list:
Some modules export only some symbols by default, while reserving some less commonly used symbols to be named explicitly in the import list:
Once you specify an import list, you only get the symbols you explicitly list. You most list any symbol that was otherwise exported by default:
Some modules specify group of symbols and "tag" them. These names typically start with a colon:
One more thing
Now, those are the modules that use the conventional
exporter
to do its work, and that’s what affects the original problem. There are other modules that use the import list to do other goofy things that don’t involve symbols. Since the "exporting" happens at compile time, some modules use the opportunity to configure themselves and do other tricks:And some modules use it to configure your calling code rather than themselves: