Module-Generic
view release on metacpan or search on metacpan
lib/Module/Generic/Finfo.pm view on Meta::CPAN
my $file = shift( @_ );
my $p = scalar( @_ ) ? { @_ } : {};
return( $self->new( $file, $p ) );
}
sub uid
{
my $self = shift( @_ );
my $data = $self->{_data};
return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
return( $self->new_number( $data->[ FINFO_UID ] ) );
}
sub user
{
my $self = shift( @_ );
my $data = $self->{_data};
return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
# perlport: "getpwuid: (Win32) Not implemented. (RISC OS) Not useful.
return( $self->uid ) if( $^O =~ /^(win32|riscok)/i );
return( $self->new_scalar( scalar( getpwuid( $data->[ FINFO_UID ] ) ) ) );
}
sub FREEZE
{
my $self = CORE::shift( @_ );
my $serialiser = CORE::shift( @_ ) // '';
my $class = CORE::ref( $self );
my %hash = %$self;
# 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
CORE::return( [$class, \%hash] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
# 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' ) || return( $self->pass_error );
$self->_load_class( 'DateTime::Format::Strptime' ) || 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->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->from_epoch( epoch => $t, time_zone => 'UTC' );
}
$repo->set( $has_local_tz );
# unlocks automatically at end of scope.
}
}
else
{
# try-catch
local $@;
eval
{
$dt = DateTime->from_epoch( epoch => $t, time_zone => ( $has_local_tz ? 'local' : 'UTC' ) );
};
if( $@ )
{
warn( "Error trying to set a DateTime object using ", ( $has_local_tz ? 'local' : 'UTC' ), " time zone\n" );
$dt = DateTime->from_epoch( epoch => $t, time_zone => 'UTC' );
}
}
( run in 0.368 second using v1.01-cache-2.11-cpan-0d8aa00de5b )