lib/javonet-ruby-sdk/Binaries/Perl/Linux/X64/deps/lib/perl5/Nice/Try.pm in javonet-ruby-sdk-2.4.8 vs lib/javonet-ruby-sdk/Binaries/Perl/Linux/X64/deps/lib/perl5/Nice/Try.pm in javonet-ruby-sdk-2.4.9

- old
+ new

@@ -1,12 +1,12 @@ ##---------------------------------------------------------------------------- ## A real Try Catch Block Implementation Using Perl Filter - ~/lib/Nice/Try.pm -## Version v1.3.11 +## Version v1.3.13 ## Copyright(c) 2024 DEGUEST Pte. Ltd. ## Author: Jacques Deguest <jack@deguest.jp> ## Created 2020/05/17 -## Modified 2024/08/11 +## Modified 2024/09/06 ## All rights reserved ## ## This program is free software; you can redistribute it and/or modify it ## under the same terms as Perl itself. ##---------------------------------------------------------------------------- @@ -24,11 +24,11 @@ use PPI 1.277; use Filter::Util::Call; use Scalar::Util (); use List::Util (); use Want (); - our $VERSION = 'v1.3.11'; + our $VERSION = 'v1.3.13'; our $ERROR; our( $CATCH, $DIED, $EXCEPTION, $FINALLY, $HAS_CATCH, @RETVAL, $SENTINEL, $TRY, $WANTARRAY ); } use strict; @@ -768,19 +768,17 @@ if( \$INC{'threads.pm'} && !CORE::exists( \$INC{'forks.pm'} ) ) { \$Nice::Try::THREADED = threads->tid; } CORE::local \$Nice::Try::WANT; -CORE::local ( \$Nice::Try::EXCEPTION, \$Nice::Try::DIED, \$Nice::Try::CATCH_DIED, \@Nice::Try::RETVAL, \@Nice::Try::VOID ); +CORE::local ( \$Nice::Try::EXCEPTION, \$Nice::Try::DIED, \$Nice::Try::CATCH_DIED, \@Nice::Try::RETVAL, \@Nice::Try::VOID, \$Nice::Try::RETURN ); CORE::local \$Nice::Try::WANTARRAY = CORE::wantarray; -CORE::local \$Nice::Try::TRY = CORE::sub +CORE::local \$Nice::Try::RETURN = sub { - \@Nice::Try::LAST_VAL = CORE::do __TRY_OPEN_NL__{ __BLOCK_PLACEHOLDER__ };__TRY__CLOSE_NL__ - CORE::return( \@Nice::Try::LAST_VAL ) if( !CORE::defined( \$Nice::Try::WANTARRAY ) && CORE::scalar( \@Nice::Try::LAST_VAL ) ); - CORE::return( \$Nice::Try::VOID[0] = \$Nice::Try::SENTINEL ); + \$Nice::Try::NEED_TO_RETURN++; + return( wantarray() ? \@_ : \$_[0] ); }; -__FINALLY_BLOCK__ CORE::local \$Nice::Try::HAS_CATCH = $has_catch_clause; EOT if( !$self->{is_tied} && !$self->{dont_want} && !$self->{is_overloaded} ) { $try_sub .= <<EOT; CORE::local \$Nice::Try::NOOP = sub @@ -814,11 +812,78 @@ undef( \$Nice::Try::WANT ) if( \$\@ ); } EOT } $try_sub .= <<EOT; +CORE::local \$Nice::Try::TRY = CORE::sub { + \@Nice::Try::LAST_VAL = CORE::do __TRY_OPEN_NL__{ __BLOCK_PLACEHOLDER__ };__TRY__CLOSE_NL__ + CORE::return( \@Nice::Try::LAST_VAL ) if( !CORE::defined( \$Nice::Try::WANTARRAY ) && CORE::scalar( \@Nice::Try::LAST_VAL ) ); + \$Nice::Try::VOID[0] = \$Nice::Try::SENTINEL; + if( CORE::defined( \$Nice::Try::WANT ) && CORE::length( \$Nice::Try::WANT ) ) + { + if( \$Nice::Try::WANT eq 'OBJECT' ) + { + CORE::return( Nice::Try::ObjectContext->new( sub{ \$Nice::Try::VOID[0] } )->callback() ); + } + elsif( \$Nice::Try::WANT eq 'CODE' ) + { + CORE::return( sub{ \$Nice::Try::VOID[0] } ); + } + elsif( \$Nice::Try::WANT eq 'HASH' ) + { + CORE::return( { dummy => \$Nice::Try::VOID[0] } ); + } + elsif( \$Nice::Try::WANT eq 'ARRAY' ) + { + CORE::return( [ \$Nice::Try::VOID[0] ] ); + } + elsif( \$Nice::Try::WANT eq 'REFSCALAR' ) + { + CORE::return( \\\$Nice::Try::VOID[0] ); + } + elsif( \$Nice::Try::WANT eq 'GLOB' ) + { + CORE::return( \*{ \$Nice::Try::VOID[0] } ); + } + elsif( \$Nice::Try::WANT eq 'LIST' ) + { + CORE::return( \$Nice::Try::VOID[0] ); + } + elsif( \$Nice::Try::WANT eq 'BOOLEAN' ) + { + CORE::return( \$Nice::Try::VOID[0] ); + } + elsif( \$Nice::Try::WANT eq 'VOID' ) + { + CORE::return( \$Nice::Try::VOID[0] ); + } + elsif( \$Nice::Try::WANT eq 'SCALAR' ) + { + CORE::return( \$Nice::Try::VOID[0] ); + } + } + else + { + if( \$Nice::Try::WANTARRAY ) + { + CORE::return( \$Nice::Try::VOID[0] ); + } + elsif( defined( \$Nice::Try::WANTARRAY ) ) + { + CORE::return( \$Nice::Try::VOID[0] ); + } + else + { + CORE::return( \$Nice::Try::VOID[0] ); + } + } +}; +__FINALLY_BLOCK__ CORE::local \$Nice::Try::HAS_CATCH = $has_catch_clause; +EOT + $try_sub .= <<EOT; +{ CORE::local \$\@; CORE::eval { EOT if( $] >= 5.036000 ) @@ -1274,10 +1339,12 @@ ( !Scalar::Util::blessed( \$Nice::Try::RETVAL[0] ) or ( Scalar::Util::blessed( \$Nice::Try::RETVAL[0] ) && !\$Nice::Try::RETVAL[0]->isa( 'Nice::Try::SENTINEL' ) ) ) ) { + \$Nice::Try::NEED_TO_RETURN++ if( defined( \$Nice::Try::BREAK ) && \$Nice::Try::BREAK eq 'return' ); + no warnings 'void'; EOT if( CORE::scalar( CORE::keys( %warnings:: ) ) && CORE::exists( $warnings::Bits{args_array_with_signatures} ) ) { $last_return_block .= <<EOT; @@ -1289,11 +1356,11 @@ { if( CORE::defined( \$Nice::Try::WANT ) && CORE::length( \$Nice::Try::WANT ) ) { if( \$Nice::Try::WANT eq 'LIST' ) { - CORE::return( \@Nice::Try::RETVAL ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \@Nice::Try::RETVAL ) : \@Nice::Try::RETVAL; } elsif( \$Nice::Try::WANT eq 'VOID' ) { if( CORE::defined( \$Nice::Try::RETVAL[0] ) && \$Nice::Try::RETVAL[0] eq '__NEXT__' ) { @@ -1307,64 +1374,65 @@ { \$Nice::Try::BREAK = 'redo'; } elsif( defined( \$Nice::Try::BREAK ) && \$Nice::Try::BREAK eq 'return' ) { - CORE::return( \$Nice::Try::RETVAL[0] ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \$Nice::Try::RETVAL[0] ) : \$Nice::Try::RETVAL[0]; } } elsif( \$Nice::Try::WANT eq 'OBJECT' ) { - CORE::return( \$Nice::Try::RETVAL[0] ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \$Nice::Try::RETVAL[0] ) : \$Nice::Try::RETVAL[0]; } elsif( \$Nice::Try::WANT eq 'REFSCALAR' ) { - CORE::return( \\\$Nice::Try::RETVAL[0] ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \\\$Nice::Try::RETVAL[0] ) : \\\$Nice::Try::RETVAL[0]; } elsif( \$Nice::Try::WANT eq 'SCALAR' ) { - CORE::return( \$Nice::Try::RETVAL[0] ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \$Nice::Try::RETVAL[0] ) : \$Nice::Try::RETVAL[0]; } elsif( \$Nice::Try::WANT eq 'BOOLEAN' ) { - CORE::return( \$Nice::Try::RETVAL[0] ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \$Nice::Try::RETVAL[0] ) : \$Nice::Try::RETVAL[0]; } elsif( \$Nice::Try::WANT eq 'CODE' ) { - CORE::return( \$Nice::Try::RETVAL[0] ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \$Nice::Try::RETVAL[0] ) : \$Nice::Try::RETVAL[0]; } elsif( \$Nice::Try::WANT eq 'HASH' ) { - CORE::return( { \@Nice::Try::RETVAL } ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( { \@Nice::Try::RETVAL } ) : { \@Nice::Try::RETVAL }; } elsif( \$Nice::Try::WANT eq 'ARRAY' ) { - CORE::return( \\\@Nice::Try::RETVAL ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \\\@Nice::Try::RETVAL ) : \\\@Nice::Try::RETVAL; } elsif( \$Nice::Try::WANT eq 'GLOB' ) { - CORE::return( \$Nice::Try::RETVAL[0] ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \$Nice::Try::RETVAL[0] ) : \$Nice::Try::RETVAL[0]; } } else { - CORE::return( \$Nice::Try::WANTARRAY ? \@Nice::Try::RETVAL : \$Nice::Try::RETVAL[0] ); + \$Nice::Try::NEED_TO_RETURN ? CORE::return( \$Nice::Try::WANTARRAY ? \@Nice::Try::RETVAL : \$Nice::Try::RETVAL[0] ) : \$Nice::Try::WANTARRAY ? \@Nice::Try::RETVAL : \$Nice::Try::RETVAL[0]; } } } elsif( scalar( \@Nice::Try::VOID ) && ( !Scalar::Util::blessed( \$Nice::Try::VOID[0] ) || ( Scalar::Util::blessed( \$Nice::Try::VOID[0] ) && !\$Nice::Try::VOID[0]->isa( 'Nice::Try::SENTINEL' ) ) ) ) { - CORE::return( scalar( \@Nice::Try::VOID ) > 1 ? \@Nice::Try::VOID : \$Nice::Try::VOID[0] ); + no warnings 'void'; + scalar( \@Nice::Try::VOID ) > 1 ? \@Nice::Try::VOID : \$Nice::Try::VOID[0]; } EOT $last_return_block =~ s/\n/ /gs unless( $self->{debug_code} ); push( @$repl, $last_return_block ); my $try_catch_code = join( '', @$repl ); # my $token = PPI::Token->new( "; \{ $try_catch_code \}" ) || die( "Unable to create token" ); # NOTE: 2021-05-11 (Jacques): Need to remove blocks so that next or last statements can be effective. my $envelop = <<EOT; -; CORE::local( \$Nice::Try::BREAK, \@Nice::Try::LAST_VAL ); +; CORE::local( \$Nice::Try::BREAK, \@Nice::Try::LAST_VAL ); local \$Nice::Try::NEED_TO_RETURN = 0 unless( defined( \$Nice::Try::NEED_TO_RETURN ) ); \{ __TRY_CATCH_CODE__ \} if( CORE::defined( \$Nice::Try::BREAK ) ) { @@ -1535,10 +1603,65 @@ # if the 'return' word is 'CORE::return' so, we add it without and change it after # $new_elem->first_element->set_content( 'CORE::return' ); } next; } + elsif( $class eq 'PPI::Token::Word' && + ( $e->content // '' ) eq 'return' && + $e->sprevious_sibling && + # Should be enough + $e->sprevious_sibling->class eq 'PPI::Token::Operator' ) + # $e->sprevious_sibling->class eq 'PPI::Token::Operator' && + # ( $e->sprevious_sibling->content // '' ) =~ /^$/ ) + { + my $break_code; + my @to_remove; + # return( # something ); + if( $e->snext_sibling && + $e->snext_sibling->class eq 'PPI::Structure::List' ) + { + my $list = $e->snext_sibling; + push( @to_remove, $list ); + $break_code = "return( \$Nice::Try::RETURN->${list} )"; + } + # return( "" ) or return( '' ) + elsif( $e->snext_sibling && + $e->snext_sibling->isa( 'PPI::Token::Quote' ) ) + { + my $list = $e->snext_sibling; + push( @to_remove, $list ); + $break_code = "return( \$Nice::Try::RETURN->(${list}) );"; + } + # return; + elsif( $e->snext_sibling && + $e->snext_sibling->class eq 'PPI::Token::Structure' && + $e->snext_sibling->content eq ';' ) + { + $break_code = "return( \$Nice::Try::RETURN->() );"; + } + else + { + my $list = ''; + my $next_elem; + my $prev_elem = $e; + while( $next_elem = $prev_elem->snext_sibling ) + { + last if( $next_elem->content eq ';' ); + $list .= $next_elem->content; + push( @to_remove, $next_elem ); + $prev_elem = $next_elem; + } + $break_code = "return( \$Nice::Try::RETURN->(${list}) );"; + } + my $break_doc = PPI::Document->new( \$break_code, readonly => 1 ); + my $new_elem = $break_doc->first_element; + $new_elem->remove; + $self->_message( 5, "New element is object '", sub{ overload::StrVal( $new_elem ) }, "' -> $new_elem" ) if( $self->{debug} >= 5 ); + # Not yet implemented as of 2021-05-11 dixit PPI, so we use a hack to make it available anyhow + $e->replace( $new_elem ); + $_->remove for( @to_remove ); + } if( $e->can('elements') && $e->elements ) { $self->_process_loop_breaks( $e ); } @@ -2037,10 +2160,10 @@ print( "Unknown error: $default\n" ); } =head1 VERSION - v1.3.11 + v1.3.13 =head1 DESCRIPTION L<Nice::Try> is a lightweight implementation of Try-Catch exception trapping block using L<perl filter|perlfilter>. It behaves like you would expect.