Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Use Perl Debugger, where input is the output from shell script

by pemungkah (Priest)
on Jul 24, 2013 at 02:26 UTC ( [id://1045984]=note: print w/replies, xml ) Need Help??


in reply to Use Perl Debugger, where input is the output from shell script

Feeding input to the debugger is tricky. There are three supported mechanisms: the typeahead stack, the input file handle stack, and the remote port.

If your commands are predetermined before the debugger starts, you can use the debugger's @typeahead array to queue the lines up; the debugger's readline subroutine will read these lines preferentially over the filehandles in the filehandle stack, so you can simply add the lines you want the debugger to execute here and it will run all of them before prompting the user for further input. If you finish off with a 'q' command, it'll even exit.

If you want to generate commands dynamically, you can use either of the other options. The input filehandle stack @DB::cmdfhs, managed by DB::readline, can be set up prior to the actual execution of the debugger so that a filehandle pointing to your program (a pipe is probably simplest here) is on the top of the stack. The debugger normally reads from the top filehandle until it reaches EOF, then pops it and moves on to the next file handle. Once we run out, it defaults to reading from the terminal. You can, instead, feed the debugger commands and then a 'q'. This is essentially what the debugger's source command does.

You can also use the debugger's remote debugging option (which I admit I do not understand well, and which should be documented better than it is). This allows you to select a port that the debugger will listen on (essentially as the top of the filehandle stack) and interact with the debugger over that port.

I have personally used the typeahead buffer to do setup in a debugger session (load modules, etc.) and can confirm that works fine. The readahead stack may be your simplest option if your shell script can just happily spit out commands without checking back with the debugger. If you need to actually interact with the debugger, you're going to have to use the socket option.

Summarizing:

  • Use the readahead buffer if you can pregenerate a fixed set of commands.
  • Use the filehandle stack if you can't predetermine the commands you'll use but don't need to read any responses or output from the debugger to generate further commands.
  • Use the socket option if you really want to drive the debugger from a separate program. It may be easier to write a secondary "glue" script between your shell script and the debugger that lets the shell script think it's just reading STDIN and printing to STDOUT, but it's actually being proxied into the debugger's remote port.

user:LanX's comments point you to the options that can be used to set up the port; I personally recommend using .perldb and either stacking the commands in @DB::typeahead or opening a pipe to your shell script and pushing that onto @DB::cmdfhs.The socket option is significantly more complicated than those two.

If this seems a bit confusing, the only real place this is documented is in the debugger source code. Reading the debugger POD and comments may help you, especially in the case of the socket option. The code concerned with the socket option could use better comments, and if you puzzle it out better, submitting a patch to more precisely describe how it works probably wouldn't be a bad idea.

  • Comment on Re: Use Perl Debugger, where input is the output from shell script

Replies are listed 'Best First'.
Re^2: Use Perl Debugger, where input is the output from shell script
by Anonymous Monk on Jul 24, 2013 at 16:34 UTC
    Thanks. I was tryin to use sockets and signalhandlers to combine my script and perl debugger. thank you so much
Re^2: Use Perl Debugger, where input is the output from shell script
by Anonymous Monk on Jul 24, 2013 at 19:17 UTC

    could you please give some resources with examples

      Take a gander at Devel::Command::Tdump for an example of using the typeahead buffer. Debug::Client seems to be a client to talk to the debugger port.

      I don't see any examples of using the filehandle stack - you probably have to do that one yourself if it turns out to be the solution you need - but the basic setup that Devel::Command::Tdump uses, of setting things up in .perldb, is probably the right point to start from; open a pipe to your client program in the BEGIN block, and push it onto @DB::cmdfhs. The debugger will read from it until it returns EOF or a 'q'commmand is seen.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1045984]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found