Module-Generic

 view release on metacpan or  search on metacpan

lib/Module/Generic/Finfo.pm  view on Meta::CPAN

    my $self = CORE::shift( @_ );
    CORE::return if( !CORE::defined( $self ) );
    my $repo = Module::Generic::Global->new( 'local_tz' => 'system' );
    $repo->cleanup;
};

sub FREEZE
{
    my $self = CORE::shift( @_ );
    my $serialiser = CORE::shift( @_ ) // '';
    my $class = CORE::ref( $self );
    my @props = qw( filepath _data );
    my $hash  = {};
    foreach my $prop ( @props )
    {
        if( CORE::exists( $self->{ $prop } ) )
        {
            $hash->{ $prop } = $self->{ $prop };
        }
    }
    # Return an array reference rather than a list so this works with Sereal and CBOR
    # On or before Sereal version 4.023, Sereal did not support multiple values returned
    if( $serialiser eq 'Sereal' )
    {
        require Sereal::Encoder;
        require version;
    
        if( version->parse( Sereal::Encoder->VERSION ) <= version->parse( '4.023' ) )
        {
            CORE::return( [$class, $hash] );
        }
    }
    # But Storable want a list with the first element being the serialised element
    CORE::return( $class, $hash );
}

sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }

sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }

# NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
# NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
sub THAW
{
    my( $self, undef, @args ) = @_;
    my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
    my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
    my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
    my $new;
    # Storable pattern requires to modify the object it created rather than returning a new one
    if( CORE::ref( $self ) )
    {
        foreach( CORE::keys( %$hash ) )
        {
            $self->{ $_ } = CORE::delete( $hash->{ $_ } );
        }
        $new = $self;
    }
    else
    {
        $new = CORE::bless( $hash => $class );
    }
    CORE::return( $new );
}

sub TO_JSON { CORE::return( CORE::shift->filepath ); }

sub _datetime
{
    my $self = shift( @_ );
    my $t = shift( @_ );
    return( $self->error( "No epoch time was provided." ) ) if( !length( $t ) );
    return( $self->error( "Invalid epoch time provided \"$t\"." ) ) if( $t !~ /^\d+$/ );
    my $class = ref( $self ) || $self;
    $self->_load_class( 'DateTime::Lite' ) || return( $self->pass_error );
    $self->_load_class( 'DateTime::Format::Lite' ) || return( $self->pass_error );
    $self->_load_class( 'Module::Generic::DateTime' ) || return( $self->pass_error );
    # my $err_key = $class;
    # Previous approach
    # my $repo = Module::Generic::Global->new( 'local_tz' => $class, key => $err_key );
    # New system-wide shared approach
    my $repo = Module::Generic::Global->new( 'local_tz' => 'system' );
    my $has_local_tz = $repo->get;
    my $dt;
    if( !defined( $has_local_tz ) )
    {
        $repo->lock;
        $has_local_tz = $repo->get; # Double-check
        if( !defined( $has_local_tz ) )
        {
            # try-catch
            local $@;
            eval
            {
                $dt = DateTime::Lite->from_epoch( epoch => $t, time_zone => 'local' );
                $has_local_tz = 1;
            };
            if( $@ )
            {
                $has_local_tz = 0;
                warn( "Your system is missing key timezone components. ${class}::_datetime is reverting to UTC instead of local time zone.\n" );
                $dt = DateTime::Lite->from_epoch( epoch => $t, time_zone => 'UTC' );
            }
            $repo->set( $has_local_tz );
            # unlocks automatically at end of scope.
        }
        # Not strictly necessary
        $repo->unlock;
    }
    else
    {
        # try-catch
        local $@;
        eval
        {
            $dt = DateTime::Lite->from_epoch( epoch => $t, time_zone => ( $has_local_tz ? 'local' : 'UTC' ) );
        };
        if( $@ )
        {
            warn( "Error trying to set a DateTime::Lite object using ", ( $has_local_tz ? 'local' : 'UTC' ), " time zone\n" );
            $dt = DateTime::Lite->from_epoch( epoch => $t, time_zone => 'UTC' );



( run in 0.857 second using v1.01-cache-2.11-cpan-39bf76dae61 )