-1

上下文: 为了调试由文件组成的损坏的 taskwarrior 任务列表:backlog.datacompleted.data,我从 https://taskwarrior.org/tools/ 的 taskwarrior 挂钩修改了一个pending.dataperl脚本。当我调试损坏的文件时,我知道脚本会产生错误。我正在尝试查找该错误的位置。它已经以错误消息中返回的 JSON 代码的有限形式给出(图中的示例)。undo.data.dataexport-html.pl

错误位置的提示已经作为任务的 JSON 给出。

但是,由于我不知道与错误消息一起返回的短字符串在完整任务列表中出现的频率,我试图在错误发生时或之前获取任务/计数器的索引。

困难:

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

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

尝试返回包含错误的任务 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;

################################################################################
4

2 回答 2

1

您的语法错误缺失;在......的最后

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}) {
于 2018-12-31T16:56:23.940 回答
0

回答问题所需的最后一步是在 ysth 建议的 for 循环中添加导致错误的行。它导致了这个脚本,它返回引发错误的任务号(从 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;

################################################################################
于 2019-01-01T14:46:02.817 回答