in reply to Re^3: Can not use JSON module
in thread Can not use JSON module

Hi, Thanks for function. Any alternative of Data::Dumper in the above code. As I do not have Data::Dumper module and can not install it.

Replies are listed 'Best First'.
Re^5: Can not use JSON module
by karlgoethebier (Abbot) on Jun 04, 2017 at 07:47 UTC

    Try...

    my $hash_ref = from_json($json); while ( my ( $k, $v ) = each %$hash_ref ) { print qq($k => $v\n) }

    ...which doesn't help if things get more complex. But this doesn't matter. Just use the result - it's a Perl data structure ;-)

    See also Re: How can I visualize my complex data structure?

    «The Crux of the Biscuit is the Apostrophe»

    Furthermore I consider that Donald Trump must be impeached as soon as possible

      Hi, Thank you everyone for helping me. Thanks karlgoethebier again for providing the function, it solved my problem. Although, I tried all JSON modules suggested in the discussion and they are working fine on ubuntu but not on the board as I am unable to install modules on the board. Problem resolved. one query as below : I have pasted the function in one of the cgi file and tested it.It is working fine and I am able to extract json. As I have multiple cgi files, do I need to copy-paste that function in each file or is there any way to keep that function in separate file and use it, if possible how? Thanks
        As I have multiple cgi files, do I need to copy-paste that function in each file or is there any way to keep that function in separate file and use it, if possible how?

        This sort of code re-use is performed in Perl using modules. See Simple Module Tutorial for how to create and use a module for such a purpose. There are other, similar guides for Creating and Distributing Modules in our Tutorials section.

        The traditional approach to keeping an often-used function in a file is named a "module". You have already been shown the links to the modules and also links on how to install modules into a Perl.

        Most likely, use lib will be enough, but a look at Yes, even you can use CPAN and/or local::lib won't hurt either.

        "Thanks..."

        My pleasure. Next step: Go over that bridge named "module" - as some fellow monks mentioned already.

        Like this, less or more:

        package YourModule; use strict; use warnings; use Exporter qw(import); our ( $VERSION, @EXPORT_OK ); $VERSION = 1.00; @EXPORT_OK = qw(from_json); sub from_json { # stuff... } 1;

        See also Re: How to parse the logs in a generic manner

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        Furthermore I consider that Donald Trump must be impeached as soon as possible

      Hi,

      Thanks.

      I tried the given original code by you(keeping the Data dumper). I have changed the "shebang line" and then executed it. I got following error as below

      Use of uninitialized value in regexp compilation at jjson.pl line 93. Use of uninitialized value in regexp compilation at jjson.pl line 93. Eval-group not allowed at runtime, use re 'eval' in regex m/ (?&VALUE) (?{ $_ = $^R->[1] }) (?(DEFINE) (?<OBJECT> (?{ [$^R, {}] }) { (?: (?&KV) # [[$^R, {}], $k, $v] .../ at jjson.pl line 93.

      I executed as ./jjson.pl. Also as perl jjson.pl. Both from command prompt.

      I tried to search error, But did not find anything helpful.

      Please help.

        I tried to search error, But did not find anything helpful. Please help.

        The solution is right there in the error message you quoted. Simply add

        use re 'eval';

        somewhere in scope (eg. right after use warnings;). See perlre for details on what this affects.