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;

In reply to Re: encoding for $! by remiah
in thread encoding for $! by powerman

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.