Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: encoding for $!

by remiah (Hermit)
on Jun 29, 2014 at 05:40 UTC ( #1091605=note: print w/replies, xml ) Need Help??


in reply to encoding for $!

Hello powerman, I also would like to know the solution for this...

$_ would be used with interpolated string, like die "open $file failed $!", So, I would like to catch the timing for decode with tie variable or commandline option or hopefully, similar functionality like IO layer.

I tried with tie. This seems working. So, I guess changing every $! variable will become adding 2 lines to the head of script.

use DecodedMsg; tie $! , "DecodedMsg";
Below is my tests. But I have to confess, I am not familiar with theses "tie" things. Hope comments of glues.
use strict; use warnings; use Encode qw(decode encode); sub show{ printf "is_utf8=%d, len=%d, msg=%s\n", utf8::is_utf8($_), length($ +_), $_; } #byte string utf8 hiragana my $byte_error_msg=encode('UTF-8', join('', map{pack('U',$_)} 0x3042 . +. 0x3052)); print "#test1: with byets\n"; $_ = $byte_error_msg ; show; print "#test2: with decoded char\n"; $_ = decode("UTF-8", $byte_error_msg); show; print "#test3 with tied variable ... seems working\n"; {package DecodedMsg; use Encode qw(decode); sub TIESCALAR { my $class=shift; print "class=$class\n"; return bless( {} , $class ); } sub STORE{ my $self=shift; print "###in store arg=$_[0]\n"; $self->{val}= $_[0]; } sub FETCH { my $self=shift; if( utf8::is_utf8( $self->{val} ) ){ return $self->{val}; } else { print "#in fetch ... return decoded\n"; return decode('UTF-8', $self->{val}) } } 1; } tie $_, "DecodedMsg"; #tie $_ to DecodedMsg $_ = $byte_error_msg; show; print "#test4 with tied variable ... with system error\n"; tie $!, "DecodedMsg"; open(my $fh, "<", " /the/path/to/nowhere"); $_ =$!; show;

Replies are listed 'Best First'.
Re^2: encoding for $!
by powerman (Friar) on Jun 29, 2014 at 09:13 UTC

    My point was to find a solution which doesn't require fixing $! everywhere we get it. With tied $_ we should copy $! into $_ everywhere we get it, which doesn't really differ from doing decode('UTF-8',$!) inplace.

    Looks like best solution for now is force "en_US.UTF-8" (at least this can be done once at beginning of script) or wait until perl-5.22 (which may fix this issue).

    use POSIX qw(locale_h); BEGIN { setlocale(LC_MESSAGES,"en_US.UTF-8") }

      sorry, mistyped.

      use DecodedMsg; tie $! , "DecodedMsg";
      see test4 example. But I also think setting LANG to en_US.UTF-8 is better solution. regards.

        Isn't tie $! will break existing $! magic?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1091605]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2016-10-01 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (8 votes). Check out past polls.