I also saw a video about how the JSON module in Perl is quite slow, could that be it?

No, because you are not using the JSON module, you are using the JSON::XS module. The two are quite different and the latter is purposely much faster than the former. And even if it weren't we are not talking about a particularly large chunk of JSON to decode.

I have not profiled your Perl code because as posted it fails to compile:

Missing right curly or square bracket at 11162986.pl line 36, at end o +f line syntax error at 11162986.pl line 36, at EOF 11162986.pl had compilation errors.

While I can guess what is wrong, this shows that the code you have posted is not the code you are running, so who knows what else might actually be in there to slow it down.

Regardless, a fair guess would be that the slow part is this line:

my $result = qx{ i3-msg -t get_workspaces }; # returns [{},{},{}]

Shelling out 100 times is going to be slow however you do it. Try obtaining the JSON once and then loop 100 times over the rest of the processing and see what difference that makes.


🦛


In reply to Re: Adjusting variable context when working with JSON data? by hippo
in thread Adjusting variable context when workign with JSON data? by unmatched

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.