【发布时间】:2018-12-31 15:24:24
【问题描述】:
背景:
为了调试由以下文件组成的损坏的 taskwarrior 任务列表:backlog.data、completed.data、pending.data 和 undo.data 我修改了a perl script from the taskwarrior hookshttps://taskwarrior.org/tools/。当我调试损坏的.data 文件时,我知道export-html.pl 脚本会产生错误。我正在尝试查找该错误的位置。它已经以错误消息中返回的 JSON 代码的有限形式给出(图中示例)。
但是,由于我不知道与错误消息一起返回的短字符串在完整任务列表中出现的频率,我试图在错误发生时或之前获取任务/计数器的索引。
难度:
- for 循环中实际发现错误的代码似乎根本没有执行,或者:
- 我还不知道如何在这个评估任务 JSON 格式的 for 循环中打印索引(用
count<Some Letter>表示)。
问题:
在找到错误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;
################################################################################
【问题讨论】:
-
缺少
;? -
感谢您的敏锐洞察力,确实我在双重打印语句测试中错过了一个分号。我正在重新尝试双重打印语句,包括现在产生错误的原始循环中的分号,以查看它是否允许单独打印(任务)索引。
标签: json perl debugging ubuntu-16.04 taskwarrior