LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Difficulties running commands in shells from Perl (http://www.linuxquestions.org/questions/programming-9/difficulties-running-commands-in-shells-from-perl-815959/)

catkin 06-23-2010 01:35 PM

Difficulties running commands in shells from Perl
 
Hello :)

I'm getting something(s) wrong, trying to run commands (both simple and piped) in shells from Perl programs. The ultimate objective is to set up "copy X selection to clipboard" from urxvt but apparently simple debugging statements are not working.

Here's the Perl, taken from here and modified to use xclip instead of xsel and with debugging added, shown in green
Code:

#! /usr/bin/perl

sub on_sel_grab {
    open(my $logfh, '>>/tmp/on_sel_grab.log');
    print $logfh "on_sel_grab running\n";

    my $query = $_[0]->selection;
    print $logfh "query is $query\n";
    open (my $pipe,'|-','/usr/bin/xclip -in -selection clipboard') or die;
    print $pipe $query;
    close $pipe;
    close $logfh
}

After selecting text in a urxvt terminal, /tmp/on_sel_grab.log shows that $query holds the selected text.

The xclip command is OK as shown by running
Code:

echo foo | /usr/bin/xclip -in -selection clipboard
and then using Ctrl+V in a GUI text editor; "foo" is pasted.

Perhaps there is something wrong with the open() call. Not knowing how to investigate further I switched to the other sample Perl script, here shown with added debug statements
Code:

#! /usr/bin/perl

sub on_sel_grab {
    open(my $logfh, '>>/tmp/on_sel_grab.log');
    print $logfh "on_sel_grab running\n";

    my $query=quotemeta $_[0]->selection;
    $query=~ s/\n/\\n/g;
    $query=~ s/\r/\\r/g;
    print $logfh "query is $query\n";
    system( "echo -en " . $query . " | /usr/bin/xclip -in -selection clipboard" );
    system( "echo -en " . $query . " > /tmp/on_sel_grab.2.log" );
    system( "echo foo >> /tmp/on_sel_grab.2.log" );
    close $logfh;
}

Again /tmp/on_sel_grab.log shows $query with the expected value but /tmp/on_sel_grab.2.log does not exist.

What is wrong with the two system() calls that are intended to create /tmp/on_sel_grab.2.log?

Best

Charles

Sergei Steshenko 06-23-2010 05:16 PM

Quote:

Originally Posted by catkin (Post 4012899)
Hello :)

I'm getting something(s) wrong, trying to run commands (both simple and piped) in shells from Perl programs. The ultimate objective is to set up "copy X selection to clipboard" from urxvt but apparently simple debugging statements are not working.

Here's the Perl, taken from here and modified to use xclip instead of xsel and with debugging added, shown in green
Code:

#! /usr/bin/perl

sub on_sel_grab {
    open(my $logfh, '>>/tmp/on_sel_grab.log');
    print $logfh "on_sel_grab running\n";

    my $query = $_[0]->selection;
    print $logfh "query is $query\n";
    open (my $pipe,'|-','/usr/bin/xclip -in -selection clipboard') or die;
    print $pipe $query;
    close $pipe;
    close $logfh
}

...

I don't understand one basic thing. The quoted code appears to be a Perl script (because of '#! /usr/bin/perl' line in it). OTOH, the code contains just a subroutine, but no executable code. I.e. neither the subroutine is called, nor there are lines outside the subroutine which can be executed.

So, how is it supposed to work ?

catkin 06-23-2010 10:12 PM

Thanks for replying Sergei :)

The file is in the urxvt terminal emulator's default Perl library directory /usr/lib/urxvt/perl. It is called "clipboard" and is named in ~/.Xresources in the line URxvt.perl-ext-common: default,matcher,clipboard

clipboard's on_sel_grab() function is called when text is selected in the urxvt terminal emulator.

After that my understanding is weak. Paraphrasing the urxvtperl man page ...

urxvt has a built in Perl interpreter. When a terminal is created the urxvt Perl extensions named as above or on the command line are loaded.

Sergei Steshenko 06-23-2010 10:43 PM

Quote:

Originally Posted by catkin (Post 4013277)
Thanks for replying Sergei :)

The file is in the urxvt terminal emulator's default Perl library directory /usr/lib/urxvt/perl. It is called "clipboard" and is named in ~/.Xresources in the line URxvt.perl-ext-common: default,matcher,clipboard

clipboard's on_sel_grab() function is called when text is selected in the urxvt terminal emulator.

After that my understanding is weak. Paraphrasing the urxvtperl man page ...

urxvt has a built in Perl interpreter. When a terminal is created the urxvt Perl extensions named as above or on the command line are loaded.

Thanks for pointing to the manpage - now it makes more sense; I think you can safely remove '#! /usr/bin/perl' line - the manpage doen't mention it at all, and it makes sense to me.

Now regarding the piece of code:


Code:

    open (my $pipe,'|-','/usr/bin/xclip -in -selection clipboard') or die;
- try just

Code:

    open (my $pipe,'| /usr/bin/xclip -in -selection clipboard') or die;
, though your code, according to the manual, is correct.

If it doesn't work, I'd suggest to temporarily replace '/usr/bin/xclip -in -selection clipboard' with a script/program which, say, echoes its STDIN to STDERR - for diagnostic purposes, to make sure that $pipe indeed gets data and passes it to the forked process.

catkin 06-23-2010 11:15 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4013293)
I think you can safely remove '#! /usr/bin/perl' line - the manpage doen't mention it at all, and it makes sense to me.

Tried removing it and the on_sel-grab() function still ran on selecting text. Strange thing is, all the as-distributed scripts in the library have first line
Code:

#! perl
Quote:

Originally Posted by Sergei Steshenko (Post 4013293)
- try just

Code:

    open (my $pipe,'| /usr/bin/xclip -in -selection clipboard') or die;

That worked! Strange that the form published in the beautifully-written ArchWiki page on urxvt didn't work. Note about the fix added to the WIKI.

Sergei Steshenko 06-24-2010 12:30 AM

Quote:

Originally Posted by catkin (Post 4013308)
Tried removing it and the on_sel-grab() function still ran on selecting text. Strange thing is, all the as-distributed scripts in the library have first line
Code:

#! perl
That worked! Strange that the form published in the beautifully-written ArchWiki page on urxvt didn't work. Note about the fix added to the WIKI.

Well, since my suggestion worked, you possibly found a bug in Perl. As I said, my suggestion was an unsubstantiated one, was just following "there is more than one way to do it" paradigm.


All times are GMT -5. The time now is 01:55 AM.