LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-23-2010, 01:35 PM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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
 
Old 06-23-2010, 05:16 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by catkin View Post
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 ?

Last edited by Sergei Steshenko; 06-23-2010 at 10:18 PM.
 
Old 06-23-2010, 10:12 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Original Poster
Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
Old 06-23-2010, 10:43 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by catkin View Post
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.
 
1 members found this post helpful.
Old 06-23-2010, 11:15 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Original Poster
Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by Sergei Steshenko View Post
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 View Post
- 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.
 
Old 06-24-2010, 12:30 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by catkin View Post
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.
 
  


Reply

Tags
perl, pipe


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] is there any way to have multiple x severs running in different shells? elerek Linux - Newbie 2 05-24-2010 09:29 PM
emacs: running multiple M-x shells? genmaicha Linux - Software 2 04-17-2009 12:41 PM
Executing unix commands from perl script running on Windows. harishkrishnan Programming 1 03-04-2008 11:48 PM
perl and linux commands Ammad Programming 3 02-11-2006 01:38 PM
difficulties running snes9x deathsythe210 Linux - Games 3 11-24-2004 02:15 PM


All times are GMT -5. The time now is 06:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration