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

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..

Replies are listed 'Best First'.
Re^5: Unicode issues with emc uemcli
by haukex (Archbishop) on Sep 08, 2020 at 07:01 UTC

    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?

        Glad to help!

        How did it occur to you to use Data::Dumper and then use IPC::Run3? Why not anything else?

        Because I've done a fair amount of research on the topic of calling external commands (see e.g. here), I knew that IPC::Run3 was likely to work best for you on Windows. Then, when debugging Unicode issues, one needs to see the data that Perl is storing (see also my post here) with nonprintable characters visualized, for which one needs to use a dumper module like Data::Dump, Data::Dumper with $Data::Dumper::Useqq=1;, or for difficult cases Devel::Peek.

        how do I get better at understanding the CPAN docs?

        I think this will come along with learning more about Perl. A lot of CPAN authors (including myself) will use terminology and idioms that they assume people know, so as you get better at Perl you'll recognize more and more of these. And in the meantime, you're always welcome to ask here!

        What book would you suggest for someone like me who knows perl but wants to get much better acquainted with perl?

        See my post here and threads like So what is your Perl book "Trilogy" anyway?

Re^5: Unicode issues with emc uemcli
by ikegami (Patriarch) on Sep 07, 2020 at 23:12 UTC

    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.