来自Pidgin插件脚本的STDOUT

时间:2014-06-12 14:04:54

标签: perl capture backticks pidgin

昨天,我为Pidgin 2.10.9编写了一个perl插件脚本,在Windows 7上运行,并使用Strawberry Perl 5.10.1.5

基本上,在收到IM时,它使用反引号来调用控制台应用程序(用.NET编写),并将控制台输出作为IM返回给发件人。

今天早上我不得不重新启动,但自从我重新启动以来,它已停止工作。

所以,我改变了反引号以使用"捕获"。那也没有用,但它至少给了我这个错误:

(15:00:33) Plugin: Error: Error in IPC::System::Simple plumbing: "Can't dup STDOUT" - "Bad file descriptor" at (eval 12) line 53

我不知道从昨天到今天有什么变化,并且想知道是否有人知道可能导致错误的原因是什么?

由于

编辑:我想添加我的代码

use Purple;
#use IPC::System::Simple qw(system systemx capture capturex);
use IPC::System::Simple qw(capture capturex);

%PLUGIN_INFO = (
    perl_api_version => 2,
    name => "PlugIn",
    version => "0.1",
    summary => "AutoResp",
    description => "PlugIn",
    author => "Mark Watkin",
    url => "http://",
    load => "plugin_load",
    unload => "plugin_unload"
);

sub plugin_init {
    return %PLUGIN_INFO;
}
sub plugin_load {
    my $plugin = shift;
    Purple::Debug::info("PlugIn", "plugin_load()\n");

    $data = "";
    $conversation_handle = Purple::Conversations::get_handle();
    Purple::Signal::connect($conversation_handle, "received-im-msg", $plugin, \&signal_chat_callback, $data);
}
sub plugin_unload {
    my $plugin = shift;
    Purple::Debug::info("PlugIn", "plugin_unload()\n");
}

sub signal_chat_callback {
    # The signal data and the user data come in as arguments
    my ($account, $sender, $message, $conv, $flags) = @_;

    Purple::Debug::info("PlugIn", "Account Alias \"" . $account->get_alias() . "\"\n");
    if( $account->get_alias() eq "PlugIn" )
    {
        Purple::Debug::info("PlugIn", "Request: \"" . $message . "\"\n");

        if(!$conv)
        {
            Purple::Debug::info("PlugIn", "No conversation\n");
            $conv = Purple::Conversation->new(1, $account, $sender);
        }
        $im = $conv->get_im_data();
        $im->send( "One moment please..." );

        my $query = "";
#       eval {
#           $query = capture("\"D:\\SourceCode\\PlugInNET\\bin\\Debug\\PlugInNET.exe\" \"" . $message . "\"");
#           #$query = capture("\"D:\\SourceCode\\PlugInNET\\bin\\Debug\\PlugInNET.exe\"", "\"" . $message . "\"");
#           #my $query = capture("D:\\SourceCode\\PlugInNET\\bin\\Debug\\PlugInNET.exe");
#           #my $query = `\"D:\\SourceCode\\PlugInNET\\bin\\Debug\\PlugInNET.exe\" \"$message\"`;
#           #my $query = `dir /b`;
#       };
#       if( $@ )
#       {
#           Purple::Debug::info("PlugIn", "Error: " . $@ . "\n");
#       }

        Purple::Debug::info("PlugIn", "Query: " . $query . "\n");
        open ( my $fh, "-|", "D:\\SourceCode\\PlugInNET\\bin\\Debug\\PlugInNET.exe \"$message\"" ) or die "Cannot run free, $ERRNO";
        while (<$fh>)
        {
             Purple::Debug::info("PlugIn", "Read: Line " . $_ . "\n");
             $query = $query . $_ . "\n";
        }
        close $fh;
        Purple::Debug::info("PlugIn", "Query: " . $query . "\n");

        if( $query eq "" )
        {
            $im->send( "I'm sorry, my brain doesn't seem to be functioning at the moment" );
        } else {
            @msgs = split(/-----------\n/, $query);
            foreach( @msgs )
            {
                Purple::Debug::info("PlugIn", "Result Msg: \"" . $_ . "\"\n");
                $im->send( "<BODY>" . $_ . "</BODY>" );
            }
        }
    }
}

计划是在我正确运行后修复路径

1 个答案:

答案 0 :(得分:0)

请考虑使用文件句柄而不是反引号从其他来源捕获stdout。您将能够收集错误。

#!/usr/bin/perl

use strict;
use warnings;
use English;

# No taint protection in this example
open ( my $fh, '-|', '/usr/bin/free' ) or die "Cannot run free, $ERRNO";
while (<$fh>)
{
   print;
}
close $fh;