tags:

views:

83

answers:

1

Hello,

I am unable to evaluate 'modern perl' code inside the perl debugger. It works OK when debugging the code in a file, but not from the prompt.

minimal example:

# activating 5-10 features with -E (it works)
$  perl -E 'say "x"'
x
# calling the debugger with -E
# it works for infile code but for prompt line code...
$  perl -dEbug    Loading DB routines from perl5db.pl version 1.33
    DB say "x"
    String found where operator expected at (eval 16)[/local-perl/lib/5.12.1/perl5db.pl:638] line 2, near "say "x""
    at (eval 16)[/local-perl/lib/5.12.1/perl5db.pl:638] line 2
        eval '($@, $!, $^E, $,, $/, $\\, $^W) = @saved;package main; $^D = $^D | $DB::db_stop;say "x";

[note: the same happens with "use feature ':5.10'"]

Am I missing something?

+6  A: 

This is an interesting question, and one I never thought of, so kudos on that.

I found a reference to the issue here, but it's about a year old. However, the relevant portion of the perl source hasn't changed since, and can be seen here. Essentially, if you take a look at toke.c in the perl source, you see the following:

if (PL_perldb) {
    /* Generate a string of Perl code to load the debugger.
     * If PERL5DB is set, it will return the contents of that,
     * otherwise a compile-time require of perl5db.pl.  */

    const char * const pdb = PerlEnv_getenv("PERL5DB");
            ...
}
...
if (PL_minus_E)
    sv_catpvs(PL_linestr,
          "use feature ':5." STRINGIFY(PERL_VERSION) "';");

Basically, the debugger is loaded before the -E flag is processed, so the features aren't yet enabled when the debugger gets loaded. The gist of this is that you can't currently use -E with the -d command. If you want to use say, switch, or any other feature from the debug prompt, you have to do it like this:

  DB<1> use feature 'say'; say "x"
  x

The closest I've seen to a solution is:

  1. copy perl5db.pl from your PERL5LIB to either somewhere in PERL5LIB or the current directory, with a different name, say myperl5db.pl
  2. Edit myperl5db.pl to have use feature ':5.10'; (or just 'state', or just 'say') on the first line.
  3. Set the environment variable PERL5DB to "BEGIN { require 'myperl5db.pl' }"

Which I found at PerlMonks.

eldarerathis
Hummm, thanks for your answer, it explains why it happens. But now I am more puzzled: I have tried before loading these features in the debugger with 'use feature 'say'' and this didn't work. Now I saw the trick: It should be in the same line. Why??? Why 'feature' module exports the functions localized to the current scope (the DB line)?? How 'use' can do that?. I though that it was always global. If you wanted something localized you use 'require' isn't it?. I would appreciate any enlightenment on this issue.
Pablo Marin-Garcia
I thought that was unusual, too. To be honest, I'm not really sure on the "why" part, but it does seem like you're absolutely correct - the `use feature x` statements act as though they're scoped to the DB line. My guess is that each line is executed as a different block/scope, because declaring a variable such as `my $x = 1` will also make `$x` unavailable on subsequent lines (since it is not in the scope of the next line, only the current line). This would be consistent, since pragmas (like `feature`) are lexically scoped to the end of the block in which you use them.
eldarerathis
thanks, I missed the `feature` lexical scope. This explains the 'only in the line action'. But do you know why if my module or script starts with the `use feature` it does not work either? Does it mean that it would happen with all `use feature ...`?. And finally, if you are using perl >5.10 features, in you script, how do you debug them when the line has an error and you want to play with it in the debugger? Would you use Devel::REPL to play with it?
Pablo Marin-Garcia
Hm, so if you run a script that has a `use feature` pragma in the debugger it doesn't work as expected? That might be a bug or your environment might be set up strangely. I tested with a very simple two line program: `use feature "say"; say "Hello";` (on separate lines which I can't represent in this comment) and it worked as expected. Are you pressing `c` (or `n`) once the debugger starts? Execution of he script, by default, needs to be manually started.
eldarerathis
No, the scripts works perfect inside the debugger. The problem is when I write the code directly in the debugger while I am debugging a script with `use feature ':5.10'`. And as you explained before adding the `use feature` in the debugger does not help because it only works in the same line but does not persist outside it, so it is an overkill solution. Unfortunately setting a break before an offending line, and copy-paste-modify-try_it_interactively does not work for '5.10 features' inside the debugger :-(
Pablo Marin-Garcia