How to debug Perl (according to Exercism)

So in the hello world exercise for Perl, there’s this note:

" How to debug

When a test fails, a message is displayed describing what went wrong. If you would like additional output, you can use the subroutine ::YYY() around the code you would like to inspect. (:: is a shortcut for main::). This subroutine is part of the XXX module."

What does this actually end up looking like? I’ve not been successful so far and most of my searches are returning results for date-time content or generic strings.

For example, in the ETL exercise, if you wanted to debug the input data:

sub transform ($data) {
    ::YYY($data);
}

In the online test runner, the tests will contain a section called ‘Your Output’, which in this case would look something like this:

---
'1':
- A
- E
- I
- O
- U
...

at /mnt/exercism-iteration/t/../lib/ETL.pm line 9

1 Like

Ah! Awesome, thank you.

Whoa! Way glad I stumbled onto this post. I missed the mentioned note in the Hello World exercise (actually, still can’t find it) and it’s been a real pill not to be able to just print out some debugging statements. I actually like the WWW method over YYY as it keeps executing rather than dying. Reading up more on the Test harness is on my ToDo list… FWIW, I run locally, so I had to install the XXX module.

2 Likes

That’s interesting, YYY should not be dying, only XXX and ZZZ. Do you have an example of code which dies with YYY?

1 Like

hmmmm… Apparently not. Perhaps it was XXX or I had something else wrong. It was the Anagram exercise where I first used it. But I went back through my file history and could not find a save point that failed the way I thought it did. It was also the first time I used the module so high odds I just did something wrong. I also see exactly what you say in the perldoc, so not sure what I did. I need to experiment more with it anyway just to increase my familiarity with it and working with the test harness in general.

Thanks for the correction.

P.S. Where exactly is this document? Is there more to it than this? For the life of me I cannot find it. Can someone post a link?

The information should show up in the “How to debug” section on the “Introduction” tab in the online editor. I’m not sure if this documentation is included when the CLI is used though? But since there are no instructions for installing this module, perhaps some separate information for debugging needs to be written.

1 Like

I wondered if I should check the online editor. I do everything locally and then submit through the CLI. I think I’ve only used the online editor a couple times and each time I made no code changes. Just something quirky happened when I submitted and it seemed to timeout or something, because when I opened the online editor and ran the tests, both times it just worked. I used it one other time and determined a module I installed and used locally was not available in the Exercism test area so I had to refactor without it. FWIW, The module was DateTime::Format::ISO8601.

you can use the subroutine ::YYY() around the code you would like to inspect.

This wording was confusing and misleading for me. It doesn’t really “inspect” “code”, it just takes the resulting values and outputs them. So it’s similar to print or dump. Don’t use it to wrap whole for(){} loops for example.

Suggestions are welcome! “Inspect” is meant here in the manual human sense, I could probably replace “code” with “data”.

I’m curious because I’m currently trying to figure out how to debug my solutions locally, rather than in the web editor. Did you come up with a decent local workflow?

Frequently, the errors from prove are completely unusable (at least for a novice like me). Using ::YYY doesn’t seem to display the value unless some prerequisite is met. So, I end up just doing all my debugging in vim and running perl modulename.pm and using say to print values with dummy input until I see what I expect.

I suspect (and hope) this is an extremely suboptimal approach.

Here is the documentation on the XXX module:

I’m new to using the module and it’s been a while since I’ve done some exercisim problems (I should go do some more) so I may come back with a better answer. I also seem to recall having some inconsistent results using the module. As in I made a claim about it not working, then went back to try it again and it worked. I also always remove all the debug code when I submit so don’t have many examples.

That being said, I dug through my git history and found examples of using WWW instead of the other methods. eg:

use XXX;
sub saddle_points ($matrix) {
...
        XXX::WWW(@columns);
        XXX::WWW("Row Count: ", $#$matrix);
...
}

There are subtle differences between the various XXX methods and YYY specifically notes modified behavior when using Test::More.

FWIW, I use VSCode and run my code in a terminal window. The above code snippet was taken from running code. As you can see, you should be able to put anything you want in the method argument list.

The debugging instructions are provided alongside the documentation given in the online test runner, where the XXX module has been installed and set up to be loaded as standard. Unfortunately there are no debugging instructions yet for doing so on your own machine, hopefully I can find the time to change that.

This is the setup the online test runner uses: Use XXX without “use XXX;”

If you want to use XXX in your own code, you will need to install the module on your machine and either use the same setup as above, or explicity use XXX; in your code.

YYY outputs to stdout, and WWW outputs to stderr, so if you are using YYY in combination with prove, you would need to use the -v flag to enable verbose mode.

There used to be instructions on enabling the verbose flag, but I can’t seem to find them so I’m either misremembering or they’ve gone missing at some point.

There are a variety of different modules for displaying data or debugging, one alternative being Data::Dump::Color - Like Data::Dump, but with color - metacpan.org. Perl has built in debugger which can be run with perl -d t/test-file.t, but I wouldn’t advise this for a beginner as it can be quite overwhelming to get the hang of when you’re starting out: perldebug - Perl debugging - Perldoc Browser.

1 Like