Search code examples
jsonperldebuggingubuntu-16.04taskwarrior

Returning the index of a at error: "malformed JSON string" in for loop of perl-JSON script


Context: In order to debug a corrupt taskwarrior task list consisting of the files: backlog.data, completed.data, pending.data and undo.data I modified a perl script from the taskwarrior hooks at https://taskwarrior.org/tools/. As I am debugging the corrupt .data files, I know the export-html.pl script yields an error. I am trying to find the location of that error. It is already given in the limited form of the JSON code that is returned in the error message (Example in figure).

Hint of location of error already given as JSON of task.

However, since I do not know how often that short string that is returned with the error message occurs in the complete tasklist, I am trying to get the index of the task/counter right at- or before the error occurs.

Difficulty:

  1. The code inside the for-loop that actually finds the error appears to not be executed at all, OR:
  2. I do not yet know how to print the index, (indicated with count<Some Letter>) in this for loop that evaluates the task JSON format.

Question: How does one print the (task) index until the error malformed JSON string .. is found?

Attempts to return the index of the task-JSON that contains an error:"

#! /usr/bin/perl
################################################################################
##
## Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez.
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
##
## http://www.opensource.org/licenses/mit-license.php
##
################################################################################

use strict;
use warnings;

# Give a nice error if the (non-standard) JSON module is not installed.
eval "use JSON";
if ($@)
{
  print "Error: You need to install the JSON Perl module.\n";
  exit 1;
}

# Use the taskwarrior 2.0+ export command to filter and return JSON
my $command = join (' ', ("env PATH='$ENV{PATH}' task rc.verbose=nothing rc.json.array=no export", @ARGV));
if ($command =~ /No matches/)
{
  printf STDERR $command;
  exit 1;
}

# Generate output.
print "<html>\n",
      "  <body>\n",
      "    <table>\n",
      "      <thead>\n",
      "        <tr>\n",
      "          <td>ID</td>\n",
      "          <td>Pri</td>\n",
      "          <td>Description</td>\n",
      "          <td>Project</td>\n",
      "          <td>Due</td>\n",
      "        </tr>\n",
      "      </thead>\n",
      "      <tbody>\n";

## This is my own loop Attempt 0
my $countA = 0;
for my $task (split "\n", qx{$command})
{
  ++$countA;
  print ("hello world".$countA);
  print "Entered hello world,$countA \n";
}

## This is my own loop Attempt 1
my $countB = 0;
for my $task (split "\n", qx{$command})
{
  ++$countB;
  print "Entered hello world,$countB \n";
}

# ## This is my own loop Attempt 2 Contains the "malformed JSON string" error but does not print the index.
# my $countC = 0;
# for my $task (split "\n", qx{$command})
# {
#   ++$countC;

#   my $dataC = from_json ($task);

#   print "Entered hello world after data,$countC \n";
# }


# ## This is my own loop Attempt 3 Contains the "malformed JSON string" error but does not print the index.
# my $countD = 0;
# for my $task (split "\n", qx{$command})
# {
#   ++$countD;
#   #print ("hi".$countB)
#   my $dataD = from_json ($task);

#   print " ",($countD->{'test'}       || 'testB'),"some";
# }

## Loop Attempt 4 based on SO suggestion ysth
my @task = split "\n", qx{$command};
for my $index (0..$#task) {
    my $task = $task[$index];
    print "Before index,$task";
    print "The index,$index \n";
}

print "Original loop entering:";

## Original loop
my $count = 0;
for my $task (split "\n", qx{$command})
{
  ++$count;
  my $data = from_json ($task);

  print "        <tr>\n",
        "          <td>", ($data->{'id'}          || ''), "</td>\n",
        "          <td>", ($data->{'priority'}    || ''), "</td>\n",
        "          <td>", ($data->{'description'} || ''), "</td>\n",
        "          <td>", ($data->{'project'}     || ''), "</td>\n",
        "          <td>", ($data->{'due'}         || ''), "</td>\n",
        "        </tr>\n";
}

print "      </tbody>\n",
      "      <tfooter>\n",
      "        <tr>\n",
      "          <td>", $count, " matching tasks</td>\n",
      "        </tr>\n",
      "      </tfooter>\n",
      "    </table>\n",
      "  </body>\n",
      "</html>\n";

exit 0;

################################################################################

Solution

  • Your syntax error is a missing ; at the end of

    print ("hello world".$countA)
    

    If you want to loop over indicies, then just do so:

    my @task = split "\n", qx{$command};
    for my $index (0..$#task) {
        my $task = $task[$index];
        ...
    

    instead of:

    for my $task (split "\n", qx{$command}) {