[SOLVED] Difficulties running commands in shells from Perl
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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
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 ?
Last edited by Sergei Steshenko; 06-23-2010 at 10:18 PM.
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.
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.
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
- 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.