perl - Scalar::Util::blessed() appears to return octets - Stack Overflow

When an object whose type is a non-ASCII name is passed to Scalar::Util::blessed(), it appears to retur

When an object whose type is a non-ASCII name is passed to Scalar::Util::blessed(), it appears to return octets instead of a Perl string. I'm running ActiveState Perl v5.20.2 on Windows. Can someone please check whether this happens on any other platforms/versions? (Note that confess() displays the class name correctly for the check_self() call.)

use strict; use warnings;
use utf8;
use English;
use feature qw(unicode_strings);
use open qw(:std :utf8);

package MyClass {
    use strict; use warnings;
    use utf8;
    use feature qw(unicode_strings);
    use Carp ();
    use Scalar::Util ();

    sub new {
        my $class = $_[0];
        return bless( {}, $class );
    }
    sub check_class {
        my $class = $_[0];
        STDERR->print( "$class\n" );
        unless ( defined($class) && ref($class) eq '' 
               && $class->isa(__PACKAGE__) ) {
          Carp::confess( sprintf( "bad call to %s()", ( caller(1) )[3] ) );
        }
        return;
    }
    sub check_self {
        my $self = $_[0];
        check_class( Scalar::Util::blessed($self) );
        return;
    }
}

package MüClass {
    use strict; use warnings;
    use utf8;
    use Carp ();
    use Scalar::Util ();

    sub new {
        my $class = $_[0];
        return bless( {}, $class );
    }
    sub check_class {
        my $class = $_[0];
        STDERR->print( "$class\n" );
        Carp::confess( sprintf( "bad call to %s()", ( caller(1) )[3] ) )
            unless defined($class) && ref($class) eq '' 
                && $class->isa(__PACKAGE__);
        return $class;
    }
    sub check_self_1 {
        my $self = $_[0];
        return check_class( ref($self) );
    }
    sub check_self_2 {
        my $self = $_[0];
        my $class;
        $class = check_class( Scalar::Util::blessed($self) );
        return $class;
    }
    sub check_self_3 {
        my $self = $_[0];
        my $class = Scalar::Util::blessed($self);
        utf8::decode($class) unless utf8::is_utf8($class);
        return check_class($class);
    }
}

my $myclass = MyClass->new;
MyClass->check_class;
$myclass->check_self;
my $müclass = MüClass->new;
MüClass->check_class;
$müclass->check_self_1;
eval{ $müclass->check_self_2; };
STDERR->print($EVAL_ERROR) if $EVAL_ERROR;
$müclass->check_self_3;

Output:

MyClass  
MyClass  
MüClass  
MüClass  
MüClass  
bad call to MüClass::check_self_2() at D:\Batch/test-blessed.pl line 47.
        MüClass::check_class("M\x{c3}\x{bc}Class") called at D:\Batch/test-blessed.pl line 59  
        MüClass::check_self_2(MüClass=HASH(0x256d560)) called at D:\Batch/test-blessed.pl line 76  
        eval {...} called at D:\Batch/test-blessed.pl line 76  
MüClass

When an object whose type is a non-ASCII name is passed to Scalar::Util::blessed(), it appears to return octets instead of a Perl string. I'm running ActiveState Perl v5.20.2 on Windows. Can someone please check whether this happens on any other platforms/versions? (Note that confess() displays the class name correctly for the check_self() call.)

use strict; use warnings;
use utf8;
use English;
use feature qw(unicode_strings);
use open qw(:std :utf8);

package MyClass {
    use strict; use warnings;
    use utf8;
    use feature qw(unicode_strings);
    use Carp ();
    use Scalar::Util ();

    sub new {
        my $class = $_[0];
        return bless( {}, $class );
    }
    sub check_class {
        my $class = $_[0];
        STDERR->print( "$class\n" );
        unless ( defined($class) && ref($class) eq '' 
               && $class->isa(__PACKAGE__) ) {
          Carp::confess( sprintf( "bad call to %s()", ( caller(1) )[3] ) );
        }
        return;
    }
    sub check_self {
        my $self = $_[0];
        check_class( Scalar::Util::blessed($self) );
        return;
    }
}

package MüClass {
    use strict; use warnings;
    use utf8;
    use Carp ();
    use Scalar::Util ();

    sub new {
        my $class = $_[0];
        return bless( {}, $class );
    }
    sub check_class {
        my $class = $_[0];
        STDERR->print( "$class\n" );
        Carp::confess( sprintf( "bad call to %s()", ( caller(1) )[3] ) )
            unless defined($class) && ref($class) eq '' 
                && $class->isa(__PACKAGE__);
        return $class;
    }
    sub check_self_1 {
        my $self = $_[0];
        return check_class( ref($self) );
    }
    sub check_self_2 {
        my $self = $_[0];
        my $class;
        $class = check_class( Scalar::Util::blessed($self) );
        return $class;
    }
    sub check_self_3 {
        my $self = $_[0];
        my $class = Scalar::Util::blessed($self);
        utf8::decode($class) unless utf8::is_utf8($class);
        return check_class($class);
    }
}

my $myclass = MyClass->new;
MyClass->check_class;
$myclass->check_self;
my $müclass = MüClass->new;
MüClass->check_class;
$müclass->check_self_1;
eval{ $müclass->check_self_2; };
STDERR->print($EVAL_ERROR) if $EVAL_ERROR;
$müclass->check_self_3;

Output:

MyClass  
MyClass  
MüClass  
MüClass  
MüClass  
bad call to MüClass::check_self_2() at D:\Batch/test-blessed.pl line 47.
        MüClass::check_class("M\x{c3}\x{bc}Class") called at D:\Batch/test-blessed.pl line 59  
        MüClass::check_self_2(MüClass=HASH(0x256d560)) called at D:\Batch/test-blessed.pl line 76  
        eval {...} called at D:\Batch/test-blessed.pl line 76  
MüClass
Share edited Mar 9 at 4:49 user1462402 asked Mar 8 at 23:45 user1462402user1462402 591 silver badge6 bronze badges 1
  • Thakks, ysth. For some reason, that page isn't found by a Google search for "perl scalar::util blessed unicode", but this page is. Is there a better search engine for technical information? – user1462402 Commented Mar 9 at 20:09
Add a comment  | 

1 Answer 1

Reset to default 4

Looks to me like a bug in versions of Scalar::Util before 1.53.

https://metacpan./release/PEVANS/Scalar-List-Utils-1.68/source/Changes#L115:

1.53 -- 2019-10-24 10:41:12 [BUGFIXES] * Handle Unicode package names in Scalar::Util::blessed (GH #81)

Try upgrading your Scalar-List-Utils or upgrade perl to 5.31.6 or better.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744882696a4598933.html

相关推荐

  • perl - Scalar::Util::blessed() appears to return octets - Stack Overflow

    When an object whose type is a non-ASCII name is passed to Scalar::Util::blessed(), it appears to retur

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信