Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "CODE(0x1431a90)" mean when the Perl debugger outputs this for $DB::sub?

Tags:

debugging

perl

I have a question about the output that I'm getting from a simple custom Perl debugger.

If I have a simple Perl debugger like this example:

package DB 
{
  sub DB {}

  sub sub
  { 
    my ($package, $filename, $line_num, $subroutine, $hasargs,
    $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash) = caller(0);

    print "$package $DB::sub\n";

    &$DB::sub;
  }
}

1;

And I run it on a simple program like the following (invoked via perl -d test.pl):

#!/usr/bin/env perl

use strict;
use warnings;

sub hello
{
    print "hello world\n";
}

&hello;

And the output is along the lines of the following:

main CODE(0x1431a90)
main strict::import
main strict::bits
main CODE(0x1431be0)
main warnings::import
 main::hello
hello world

My question is:

What does the "CODE(0x1431a90)" part represent? It seems to represent the main body of the program - since there is no subroutine at the topmost level, this seems to be a memory reference to the main execution of the program.

What assumptions can I make about what "CODE(...)" means when I see it being printed out by my debugger? Is it always this "main" code path? Are there any other special Perl variables that I can use to interpret where the program is at that point?

Thanks!


EDIT - revised example 1:

In reference to the discussion in the comments, adding additional example:

#!/usr/bin/env perl

use strict;
use warnings;

sub hello
{
    print "hello world\n";
}

&hello;

print "hi from main\n";

And output:

main CODE(0x22f0a90)
main strict::import
main strict::bits
main CODE(0x22f0be0)
main warnings::import
 main::hello
hello world
hi from main
like image 947
shedd Avatar asked Jan 23 '26 11:01

shedd


2 Answers

It is a code reference, and probably an anonymous one. See the coderef2text method in B::Deparse to get at (an approximation of) the actual source code, and display the file and linenumber (which you are already extracting from caller) to find where the sub is called from.


Running B::Deparse::coderef2text on your sample script, the code references resolve to

require strict;
do {
    'strict'->import
};

and

use strict 'refs';
require warnings;
do {
    'warnings'->import
};

meaning they are generated and called by perl from your use strict and use warnings statement.

like image 141
mob Avatar answered Jan 25 '26 06:01

mob


CODE(0x1431a90) is stringification of code reference.

like image 25
mpapec Avatar answered Jan 25 '26 08:01

mpapec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!