Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

How to debug Perl applications with Eclipse

EPIC plug-in offers rich debugging environment

Martin Brown, Freelance Writer, Freelance Developer
Martin C. Brown has been a professional writer for more than eight years. He is the author of numerous books and articles across a range of topics. His expertise spans myriad development languages and platforms -- Perl, Python, Java, JavaScript, Basic, Pascal, Modula-2, C, C++, Rebol, Gawk, Shellscript, Windows, Solaris, Linux, BeOS, Mac OS/X, and more -- as well as Web programming, systems management and integration. Martin is a regular contributor to ServerWatch.com, LinuxToday.com, and IBM developerWorks, and a regular blogger at Computerworld, The Apple Blog, and other sites, as well as a Subject Matter Expert (SME) for Microsoft.

Summary:  Debugging Perl applications can be a frustrating process. Many Perl programmers rely on print statements and so-called "postmortem debugging." Others use the built-in Perl debugger. Neither provides a coherent execution environment for monitoring the execution of a script, and neither supports the debugging of a Common Gateway Interface (CGI) script during execution. In this tutorial, we will look at the debugging functionality offered by the Eclipse Perl Integration (EPIC) plug-in for Eclipse, which offers a rich debugging environment available and integrated with the EPIC Perl development environment.

Date:  07 Feb 2006
Level:  Intermediate PDF:  A4 and Letter (2328 KB | 34 pages)Get Adobe® Reader®

Activity:  51637 views
Comments:  

Debugging a standard application

Now that you understand how to use EPIC to move through a project and run an application, it is time to jump into the debugging of a standard application.

All you have to do is change the configuration of a standard Run environment into a debugging configuration. Eclipse then provides a debugging perspective that includes support for standard debugging tools like breakpoints and the ability to watch variables during execution.


Configuring an application for debugging


To configure a script in your project to be executed:

  1. Choose Debug from the Run menu. You'll be presented with the dialog shown in Figure 12.


    Figure 12. Debug configurations
    Debug configurations
  2. Select the Perl Local item and configure the perspectives to be used when debugging a local Perl script (not remote or CGI). Your perspective will automatically switch to the perspective shown here when you run the script. Since you are configuring for debugging, choose the Eclipse Debug perspective.
  3. You can, optionally, create a new configuration, or use the existing configuration. To create a new configuration, select Perl Local from the list of configurations on the left of this dialog box and choose New. You'll be presented with a new Run configuration just like Figure 13.

    Figure 13. Adding a new debug configuration
    Adding a new debug configuration
  4. Configure the name of the project and script you want to execute. Note that you can also add any command-line options to your script. Remember to add a configuration name, and be aware that this configuration is specific to this script in this project.
  5. Click Apply, then Close to save the configuration.

Once your configuration is active, your script is ready to debug. Click Debug to start the debugging process.


Eclipse debugging interface

The Eclipse debugging perspective is shared and used by most debugging systems in Eclipse, including when debugging C/C++ and Java applications. You can see a sample of the debug perspective in Figure 14.


Figure 14. Debug perspective
 Debug perspective

The debug perspective is made up of a number of default views, including:

Debug view
This shows running scripts, including multiple threads in a single script. You can see a close-up in Figure 15. Here, it's showing a running script (the Main Thread) and the Perl Debugger thread (used as an interface for running the script). Note that the current line of the script on which execution has been paused is also shown. If you select this item from the view, the corresponding line from your source code will be shown.
Variables view
This can be used to show the values of variables, and it has an output similar to the information shown by the Dumper() function examined in this tutorial when looking at traditional debugging methods. You'll take a closer look at how to configure variables to be watched shortly.
Breakpoints view
This shows the configured breakpoints for the application being debugged. Configuring breakpoints will be covered in more detail shortly.

The remainder of the views should already be familiar to you, such as the editor views, project outline, and console views.

Once you have started to debug your application, you will want to start stepping through the lines of your code and monitoring the output. The debugger automatically stops at the first line of executable code (after any modules have been imported).


Stepping through a debugged application

Stepping through an application being debugged enables you to monitor the execution of the script on a line-by-line or function-by-function call basis. By stepping through, you can trace the execution through function calls, test statements, and so on; and monitor the value of variables during the execution of the code.

Eclipse supports five step modes, although in most situations, only the first three are supported by the EPIC plug-in. Step modes can be selected by using the step buttons on the toolbar (the last five buttons on the right of the toolbar panel):

  1. Step Into -- This steps into a function, loop, or other statement. If the function is available, the debugger will show the source code and allow you to step through the source code.
  2. Step Over -- This steps over a statement to the next statement in the code.
  3. Step Return -- If you have stepped into a function, then step return will execute the code up to and including the return statement for that function.

You can also restart execution until the next configured breakpoint or the end of the script -- whichever comes first -- by clicking the "play" button (just your VCR play button), and pause execution by using the VCR-like pause button, and stop execution altogether by pressing the stop button.

If you stop an execution and want to relaunch a debug process on the same script, choose Relaunch from the menu when you right-click on the process.


Adding/Removing breakpoints

Breakpoints are lines in the code where you want execution to pause or stop during the execution of the script. They do exactly what you expect: break execution at that point.

To configure breakpoints, right-click in the border next to the line your source code where you want to add a breakpoint.

You'll be prompted with a menu that will allow you to add a breakpoint at that location in your code. You may find it useful to enable line numbers (also available through the same menu).

When a breakpoint has been added, you will get a breakpoint icon next to the line, as can be seen in Figure 15. The breakpoint icon is shown next to line 6 of your source code.


Figure 15. Code with breakpoints
Code with breakpoints

You can also access a list of configured breakpoints by using the Breakpoints view (Figure 16) that lists the type, file and line number of all the configured breakpoints in a project.


Figure 16. Breakpoint view
Breakpoint view

To remove a project, either right-click on the line from the source code again and choose Remove Breakpoint or select it from the list of breakpoints, right-click, and remove it that way. You can also enable and disable breakpoints, which can be more convenient if you are debugging the same code and want to monitor the same locations, but not necessarily at the same time.


Monitoring variables

Once you select a running thread, a list of all of the variables in the current scope will be shown in the Variables view. You can see a sample of the Variables view in Figure 17.


Figure 17. Debugger variables
Debugger variables

Unlike most debuggers that show variables only when you have specifically added them to a list of variables to be monitored, the EPIC debugger automatically shows all variables within the current scope. As you can see, the full details of the variable are made available, and you can use a tree-like interface to interrogate the values of complex structures like nested arrays and hashes from the script. Note that local variables have an icon with an "L" embedded; all other variables use the standard variable icon.

Variables that have changed since the last execution step are highlighted in red. This makes it easy to find changed variables while stepping through the application.

In addition to the variables defined in your script (both global and local to the current block), the variable view will also, by default, show some global variables that you have implied the use of in your script. For example, it will show the values of the special STDIN, STDOUT, and STDERR variables.

You can also optionally display internal Perl variables (such as $$, %ENV, etc.). Click on the View menu and choose Show Perl Internal Variables. A sample of the Variable view with this enabled is shown in Figure 18.


Figure 18. Showing standard internal variables
Showing standard internal variables

Remember that the Variable view shows only the variables within the current scope or global variables. It does not show variables that are not available in the current scope (function, loop, block, or even module or package).

5 of 9 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=133500
TutorialTitle=How to debug Perl applications with Eclipse
publish-date=02072006
author1-email=
author1-email-cc=troy@backstopmedia.com