Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a handle to - source?

Tags:

perl

perl-io

It looks like there is a symbol in main called '_<-' (without the quotes) in the same fashion as the other things that look like they could be handles: '_</usr/perl/lib/Carp.pm', for example.

Is there some way to use it?

Or would I have to use a source filter if I hope to read the input source?


In reply to mob: I don't know where Debug would be getting turned on. After I dump out the base table, a dump of %INC shows:

$VAR1 = {
      'warnings/register.pm' => 'C:/strawberry/perl/lib/warnings/register.pm',
      'XSLoader.pm' => 'C:/strawberry/perl/lib/XSLoader.pm',
      'English.pm' => 'C:/strawberry/perl/lib/English.pm',
      'Tie/Hash/NamedCapture.pm' => 'C:/strawberry/perl/lib/Tie/Hash/NamedCapture.pm',
      'unicore/lib/Perl/_PerlIDS.pl' => 'C:/strawberry/perl/lib/unicore/lib/Perl/_PerlIDS.pl',
      'unicore/Heavy.pl' => 'C:/strawberry/perl/lib/unicore/Heavy.pl',
      'warnings.pm' => 'C:/strawberry/perl/lib/warnings.pm',
      'utf8.pm' => 'C:/strawberry/perl/lib/utf8.pm',
      'Config.pm' => 'C:/strawberry/perl/lib/Config.pm',
      'overloading.pm' => 'C:/strawberry/perl/lib/overloading.pm',
      'Symbol.pm' => 'C:/strawberry/perl/lib/Symbol.pm',
      'Carp.pm' => 'C:/strawberry/perl/lib/Carp.pm',
      'bytes.pm' => 'C:/strawberry/perl/lib/bytes.pm',
      'Exporter/Heavy.pm' => 'C:/strawberry/perl/lib/Exporter/Heavy.pm',
      'utf8_heavy.pl' => 'C:/strawberry/perl/lib/utf8_heavy.pl',
      'strict.pm' => 'C:/strawberry/perl/lib/strict.pm',
      'Exporter.pm' => 'C:/strawberry/perl/lib/Exporter.pm',
      'vars.pm' => 'C:/strawberry/perl/lib/vars.pm',
      'constant.pm' => 'C:/strawberry/perl/lib/constant.pm',
      'Errno.pm' => 'C:/strawberry/perl/lib/Errno.pm',
      'overload.pm' => 'C:/strawberry/perl/lib/overload.pm',
      'Data/Dumper.pm' => 'C:/strawberry/perl/lib/Data/Dumper.pm'
    };
like image 740
Axeman Avatar asked Aug 03 '12 20:08

Axeman


People also ask

How do I get a current window handle?

To get the handle to the foreground window, you can use GetForegroundWindow. To get the window handle to the active window in the message queue for another thread, use GetGUIThreadInfo.

How do you get thread handles?

If you have a thread identifier, you can get the thread handle by calling the OpenThread function. OpenThread enables you to specify the handle's access rights and whether it can be inherited.

What is a handle to a process?

A process handle is an integer value that identifies a process to Windows. The Win32 API calls them a HANDLE; handles to windows are called HWND and handles to modules HMODULE. Threads inside processes have a thread handle, and files and other resources (such as registry keys) have handles also.

How can I get current process ID?

You can get the process ID of a process by calling getpid . The function getppid returns the process ID of the parent of the current process (this is also known as the parent process ID). Your program should include the header files unistd. h and sys/types.


2 Answers

Or would I have to use a source filter if I hope to read the input source?

If the source file has an __END__ or __DATA__ tag, then the DATA filehandle is available. ...that in and of itself is boring. What's interesting is that you can seek to position 0, and that will take you to the top of the source file:

use Carp;

print "Just another Perl hacker,\n";

eval { 
    no warnings qw/unopened/;
    seek DATA, 0, 0 
      or croak "Script lacking __END__ or __DATA__ tag has no DATA filehandle.";
};
if( !$@ ) {
    while(<DATA>){
        print;
    }
}
else {
    carp $@;
}

__END__

This script will execute (printing 'Just another Perl hacker,'), and then will finish up by printing its own source.

In the code above, if the eval block does trap an exception, the fallback could be to use FindBin and $0, open the source file, and then read it. Putting it all together, here's how it looks:

BEGIN {
    use Carp;

    sub read_source {
        my $source;
        local $/ = undef;
        eval {
            no warnings qw( unopened );
            my $DATA_position = tell DATA;
            croak "'tell DATA' failed: Probably no __END__ or __DATA__ segment."
              if $DATA_position < 0;
            seek DATA, 0, 0
              or croak
              "'seek DATA' failed: Probably no __END__ or __DATA__ segment.";
            $source = <DATA>;
            seek DATA, $DATA_position, 0 or croak    # Must leave *DATA usable.
              "seek to reset DATA filehandle failed after read.";
        };
        if ($@) {
            croak $@ if $@ =~ /reset/;    # Unstable state: Shouldn't be possible.
            eval {
                require FindBin;
                no warnings 'once';
                open my $source_fh, $FindBin::Bin . '/' . $0 or croak $!;
                $source = <$source_fh>;
            };
            croak "Couldn't read source file from *DATA or \$0: $@" if $@;
        }
        return $source;
    }
};

print read_source(), "\n";

This snippet first tries to read from DATA, which eliminates the need to load FindBin and open a new file handle. If that fails, then it tries the FindBin approach. If both fail, it throws an exception. The final successful state slurps the entire source file into $source_code. The DATA handle will also be restored to the same state it was in before calling this snippet.

That should robustly handle the question of how to read the source file without resorting to a source filter.

like image 78
DavidO Avatar answered Oct 11 '22 16:10

DavidO


You are seeing this in the perl debugger? That is likely where those symbol table entries come from: see the DATA STRUCTURES MAINTAINED BY CORE section of the perldoc in your perl5db.pl file.

The only way I can see to get the _<- entry in the symbol table is to start perl with just the -d switch and then enter a Perl program into standard input, e.g.:

$ perl -d

Loading DB routines from perl5db.pl version 1.32
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

print "Hello world\n";
<Ctrl-D>
main::(-:1):    print "Hello world\n";
  DB<1>

From here, @{"_<-"} (or @{$main::{"_<-"}}) contains your input, ${"_<-"} or ${$main::{"_<-"}} contains the "name" of your file (just -), and %{"_<-"}/%{$main::{"_<-"}} holds information about breakpoints and actions for stepping through code from the standard input.

Without strict refs, you could also view this data with something like

  DB<6> $name="_<-"

  DB<7> p ${$name}
-
  DB<8> p @{$name}
BEGIN { require 'perl5db.pl' };
print "Hello world\n";

  DB<9> p %{$name}

There is no filehandle associated with the symbol table entry for _<- (or for any other _<... symbols).

like image 23
mob Avatar answered Oct 11 '22 16:10

mob