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

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

上下文: 为了调试由以下文件组成的损坏的 taskwarrior 任务列表:backlog.datacompleted.datapending.dataundo.data 我修改了 a perl script from the taskwarrior hooks at https://taskwarrior.org/tools/。当我调试损坏的 .data 文件时,我知道 export-html.pl 脚本会产生错误。我正在尝试找到该错误的位置。它已经以 JSON 代码的有限形式给出,在错误消息中 returned(图中的示例)。

但是,由于我不知道 return 带有错误消息的短字符串在完整任务列表中出现的频率,我正在尝试获取 task/counter 的正确索引在错误发生时或之前。

难度:

  1. 实际发现错误的 for 循环中的代码似乎根本没有执行,或者:
  2. 我还不知道如何在评估任务 JSON 格式的 for 循环中打印索引(用 count<Some Letter> 表示)。

问题: 如何在发现错误 malformed JSON string .. 之前打印(任务)索引?

尝试 return 包含错误的任务索引 - JSON:

#! /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;

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

你的语法错误是遗漏了;在

结束时
print ("hello world".$countA)

如果你想循环索引,那么就这样做:

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

而不是:

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

回答这个问题所需的最后一步是在 ysth 建议的 for 循环中添加错误行。它导致了这个脚本,其中 returns 抛出错误的任务编号(从 0 开始,而不是 1):

#! /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;
}



## Loop Attempt 5: added the  my $data line that introduces the error to the answer suggested by ysth in SO.
my @task = split "\n", qx{$command};
for my $index (0..$#task) {
    my $task = $task[$index];
    print "Before index nr:,$index \n";
    print "Before task content:,$task \n";
    my $data = from_json ($task);
    print "After index,$index \n\n";
}


exit 0;

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