Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I don't know if this is too parochial to be of greater interest, but it's a trick for getting perl to use the clipboard in Mac OSX which I've found very useful. It has the advantage over a temp file in that the resulting data is accessible to all apps. Basically by opening pipes to the pasteboard tools pbpaste and pbcopy, the clipboard seamlessly appears as file handles. See the typically useful-in-a-real-world-situation example script below - copy a word to the clipboard, run the script and it will place a backwards version on the clipboard to be pasted where ever. CAVEAT: Due to limitations with the current incarnations of pbpaste and pbcopy, this won't work with non ASCII data.

#!/usr/bin/perl -w use strict; my($data,$word); my(@letters); open (FROM_CLIPBOARD, "pbpaste|"); open (TO_CLIPBOARD, "|pbcopy"); $data=<FROM_CLIPBOARD>; if ($data){ $word=$data ;} else { $word = "forwards"; } @letters=split(//,$word); print TO_CLIPBOARD reverse(@letters),"\n"; close (FROM_CLIPBOARD); close (TO_CLIPBOARD);
The above is a trivial example, below I've included a script which incorporates this technique for polling http://thesaurus.reference.com for a word entry in Roget's Thesaurus, filtering (brutally I'll admit) out all the ads and pop ups associated.
#!/usr/bin/perl -w #=========== declare includes ============= use strict; use diagnostics-verbose; #========== declare variables ============= my($URL,$content,$test,$word); my(@content,@filtered); #============= script body ================ open (FROM_CLIPBOARD, "pbpaste|"); open (TO_CLIPBOARD, "|pbcopy"); $test=<FROM_CLIPBOARD>; $test ? $word=$test : print "what word are you looking for?\t"; $wo +rd=<>; chomp($word); $URL= "http://thesaurus.reference.com/search?q=$word&db=roget"; print "getting the data for the word \"",$word,"\"....\n"; use LWP::Simple; unless (defined ($content = get $URL)) { die "could not get $URL\n"; } print "filtering the data ....\n"; (undef,$content)=split (/<!-- Content -->/,$content); ($content,undef)=split (/<!-- End content -->/,$content); @content= split (/<tr>/,$content); for (@content){ next unless /<[^>]td*>/; next unless /:|,/; next if /Source:|Entry/; last if /Try your search for/; s/&nbsp;//gs; s/<[^>]*>//gs; push (@filtered,$_,"\n"); } print "directing output to clipboard...\n"; print TO_CLIPBOARD @filtered; print "done.\n"; close(FROM_CLIPBOARD); close(TO_CLIPBOARD);

In reply to OSX perl2clipboard by Sol-Invictus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found