in reply to Re^2: Unicode issues with emc uemcli
in thread Unicode issues with emc uemcli

That data is encoded with UTF-16LE. Although I'm not sure if there's a more elegant way to solve this because I'm not familiar with the "uemcli" command, by adding the following code to the example I posted above, you will get the properly decoded Perl string in $str.

use Encode qw/decode/; my $str = decode('UTF-16', $out, Encode::FB_CROAK);

Replies are listed 'Best First'.
Re^4: Unicode issues with emc uemcli
by pritesh_ugrankar (Monk) on Sep 07, 2020 at 21:55 UTC

    Hi Haukex,

    All thanks to your clever code, I am finally able to capture the output to text files too!!. Here's the addition to your code.

    my $aref_cmd1= ['uemcli','-d',$vnxe_ip,'-u',$username,'-p',$password, +'/sys/general','show','-detail']; my $aref_cmd2 = ['uemcli', '-d', $vnxe_ip, '-u', $username, '-p', $pas +sword, '/env/bat', 'show', '-detail']; my $aref_cmd3 = ['uemcli', '-d', $vnxe_ip, '-u', $username, '-p', $pas +sword, '/env/ps', 'show', '-detail']; run3 $aref_cmd1, undef, \my $out1; run3 $aref_cmd2, undef, \my $out2; run3 $aref_cmd3, undef, \my $out3; use Encode qw/decode/; my $str1 = decode('UTF-16', $out1, Encode::FB_CROAK); print "$str1\n"; my $textfile2 = "textfile2.txt"; open (my $fh2, '+>', $textfile2) or die "Cannot open file.$!"; my $str2 = decode('UTF-16', $out2, Encode::FB_CROAK); print $fh2 $str2;

    Here, the cmd1 prints to screen, cmd2 can be output to a file. cmd3 I am yet to work on. Thank you once again..

      Of course, a loop would be a bit more elegant, here's just one example.

      use warnings; use strict; use IPC::Run3; use Encode qw/decode/; my @outputs; for my $loc ('/sys/general', '/env/bat', '/env/ps') { run3 ['uemcli','-d',$vnxe_ip,'-u',$username,'-p',$password, $loc,'show','-detail'], undef, \my $out; my $str = decode('UTF-16', $out, Encode::FB_CROAK); push @outputs, $str; } print $outputs[0], "\n"; open my $fh, '>', $textfile or die "$textfile: $!"; print $fh $outputs[1]; close $fh;

      Update: Note that both prints in this example assume you don't have any Unicode (non-ASCII) characters in the strings (as your sample data shows). If you did, you'd need to set an encoding on the filehandles you're writing the data to - that's STDOUT, with for example use open qw/:std :utf8/; or several variations on that, and for filehandles, for example open my $fh, '>:encoding(UTF-8)', $filename or die "$filename: $!";.

        Hi Haukex,

        Thank you once again. Your solution helped me automate an important task at work. While I am truly obliged to you, I am still not able to understand a 100% of everything that the IPC::Run3 module does even after reading the documentation, because my perl skill is pretty rudimentary. Considering this, I request you to please answer the following questions.

        How did it occur to you to use Data::Dumper and then use IPC::Run3? Why not anything else? Is it instinct or based on some form output that you saw?

        I have extremely basic understanding of IPC, perl references and such, which I am sure is clearly revealed in my questions, so how do I get better at understanding the CPAN docs? Clearly, they are good enough for most folks, but sometimes I find the content tough to understand. May be a book would help, which leads to the next question...

        What book would you suggest for someone like me who knows perl but wants to get much better acquainted with perl? Yes, ofcourse, nothing would come close to writing code and asking questions here, but I truly want to get good with Perl hence the question on books...Edit - I currently have "Beginning Perl" by Ovid and its a fantastic book, but just wanted to know if I should go for some other books too?

      Simpler:

      open (my $fh2, '>:encoding(UTF-16)', $textfile2)
        open (my $fh2, '+>', $textfile2) or die "Cannot open file.$!"; my $str2 = decode('UTF-16', $out2, Encode::FB_CROAK); print $fh2 $str2;
        Simpler: open (my $fh2, '>:encoding(UTF-16)', $textfile2)

        No, it seems you misread decode as encode.

Re^4: Unicode issues with emc uemcli
by pritesh_ugrankar (Monk) on Sep 07, 2020 at 20:57 UTC

    Hi Haukex,

    Yes, it works and perfectly shows the output!! Thank you so much.

    All I now how to figure out is how to put this my script so that I get the desired output. Quite frankly I'm not able to make much out of the clever stuff you wrote. You've wrapped the command in what I think is a anonymous aref along with undef, \my $out. Wish there were an easier way to do this, but , that is something for me to figure out. Thank you once again.

      You've wrapped the command in what I think is a anonymous aref along with undef, \my $out.

      Yes, all of this is part of the API of IPC::Run3, its documentation explains it. I used an arrayref because this will cause the module to do its best to avoid the shell (which is a slightly complicated topic on Windows, but at least newer versions of the module will use Win32::ShellQuote to help with some of the issues). The undef causes the child process to inherit STDIN from the parent, and \my $out causes the command's STDOUT to be stored in that variable. Then, I use Encode to decode the data from the command, which is encoded in UTF-16LE; note that I specify the encoding to simply be UTF-16 because the module is smart enough to use the BOM that is part of the output you showed.