Win32::Clipboard won't place Unicode text on Clipboard by design:

CF_TEXT ... is the only format you can use to set clipboard data

This is what works for me (written based on that):

use strict; use warnings; use Win32::API 'WriteMemory'; use Encode 'encode'; Win32::API-> Import( 'kernel32', 'HGLOBAL GlobalAlloc( UINT uFlags, S +IZE_T dwBytes )' ) or die; Win32::API-> Import( 'kernel32', 'UINT_PTR GlobalLock( HGLOBAL hMem )' + ) or die; Win32::API-> Import( 'kernel32', 'BOOL GlobalUnlock( HGLOBAL hMem +)' ) or die; Win32::API-> Import( 'kernel32', 'HGLOBAL GlobalFree( HGLOBAL hMem )' + ) or die; Win32::API-> Import( 'user32', 'BOOL OpenClipboard( HWND hWndNew +Owner )' ) or die; Win32::API-> Import( 'user32', 'HANDLE SetClipboardData( UINT uFor +mat, HANDLE hMem )' ) or die; Win32::API-> Import( 'user32', 'BOOL CloseClipboard( )' ) + or die; my $str = "\x{05d0}\x{05d1}\x{05d2}"; # Alef Bet Gimel my $buf = encode( 'UTF16LE', $str ) . "\0\0"; my $h = GlobalAlloc( 2, length $buf ) or die; # GMEM_MOVEABLE my $p = GlobalLock( $h ) or die; WriteMemory( $p, $buf, length $buf ); GlobalUnlock( $h ); OpenClipboard( 0 ) or die; SetClipboardData( 13, $h ) or die; # CF_UNICODETEXT CloseClipboard() or die; GlobalFree( $h );

P.S. If your ANSI CP is 1255 (mine isn't, so I tested with another esoteric CP), and you don't mind appended newline, you could simply do this:

use Encode 'encode'; my $str = encode 'CP1255', "\x{05d0}\x{05d1}\x{05d2}"; system "echo $str| clip";

P.P.S. BTW, can anyone enlighten me, how Perl encodes argument to system, on Windows? Can this encoding be changed?

use strict; use warnings; use Encode 'encode'; my $str = encode 'CP1255', "\x{05d0}\x{05d1}\x{05d2}"; system "echo $str";

Output from Perl script still corresponds to my original CP1251.


C:\>mode con cp select=1255

Status for device CON:
----------------------
    Lines:          9000
    Columns:        80
    Keyboard rate:  31
    Keyboard delay: 1
    Code page:      1255


C:\>echo אבג
אבג

C:\>perl 171116.pl
абв


In reply to Re: clipboard and unicode text (+ question in postscript) by vr
in thread clipboard and unicode text by Animor

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.