| 1 | # Copyright (c) 2024-2025 Philipp Schafft <lion@cpan.org> | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: generic module for extracting information from filesystems | ||
| 6 | |||
| 7 | |||
| 8 | package File::Information::Lock; | ||
| 9 | |||
| 10 | use v5.10; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | |||
| 16 | our $VERSION = v0.16; | ||
| 17 | |||
| 18 | |||
| 19 | sub new { | ||
| 20 | my ($pkg, %opts) = @_; | ||
| 21 | my $self = bless \%opts, $pkg; | ||
| 22 | |||
| 23 | $self->{instance} = $self->{parent}->instance unless defined $self->{instance}; | ||
| 24 | |||
| 25 | croak 'No instance is given' unless defined $self->{instance}; | ||
| 26 | croak 'No parent is given' unless defined $self->{parent}; | ||
| 27 | croak 'No on_unlock is given' unless defined $self->{on_unlock}; | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | sub instance { | ||
| 35 | my ($self) = @_; | ||
| 36 | return $self->{instance}; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub parent { | ||
| 41 | my ($self) = @_; | ||
| 42 | return $self->{parent}; | ||
| 43 | } | ||
| 44 | |||
| 45 | # ---------------- | ||
| 46 | |||
| 47 | sub DESTROY { | ||
| 48 | my ($self) = @_; | ||
| 49 | my $func = $self->{on_unlock}; | ||
| 50 | $self->parent->$func($self); | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | __END__ | ||
| 56 | |||
| 57 | =pod | ||
| 58 | |||
| 59 | =encoding UTF-8 | ||
| 60 | |||
| 61 | =head1 NAME | ||
| 62 | |||
| 63 | File::Information::Lock - generic module for extracting information from filesystems | ||
| 64 | |||
| 65 | =head1 VERSION | ||
| 66 | |||
| 67 | version v0.16 | ||
| 68 | |||
| 69 | =head1 SYNOPSIS | ||
| 70 | |||
| 71 | use File::Information; | ||
| 72 | |||
| 73 | my File::Information::Lock $lock = $obj->lock; | ||
| 74 | |||
| 75 | This package is used to represent locks on resources and objects. | ||
| 76 | |||
| 77 | The lock is hold until the last reference to the object is gone. | ||
| 78 | |||
| 79 | =head1 METHODS | ||
| 80 | |||
| 81 | =head2 new | ||
| 82 | |||
| 83 | my File::Information::Lock $lock = File::Information::Lock->new([ instance => $instance, ] parent => $parent, on_unlock => \&unlock_sub); | ||
| 84 | |||
| 85 | Returns a new lock object. Requires an instance (L<File::Information>), a parent object (what is locked), and an unlock function. | ||
| 86 | If no instance is given C<$parent-E<gt>instance> is called to obtain one. | ||
| 87 | |||
| 88 | Once this lock is gone the unlock function is called. It is normally a private method of the object that is locked. | ||
| 89 | The unlock function is responsible of handling the case with multiple lock objects being alive at the same time. So any unlock function must check | ||
| 90 | if all locks are gone before performing an actual unlock. The unlock function is called on C<$parent> and passing the lock as first argument. | ||
| 91 | |||
| 92 | =head2 instance | ||
| 93 | |||
| 94 | my File::Information $instance = $lock->instance; | ||
| 95 | |||
| 96 | Returns the instance that was used to create this object. | ||
| 97 | |||
| 98 | =head2 parent | ||
| 99 | |||
| 100 | my $parent = $lock->parent; | ||
| 101 | |||
| 102 | Returns the parent that was used to create this object. | ||
| 103 | |||
| 104 | =head1 AUTHOR | ||
| 105 | |||
| 106 | Philipp Schafft <lion@cpan.org> | ||
| 107 | |||
| 108 | =head1 COPYRIGHT AND LICENSE | ||
| 109 | |||
| 110 | This software is Copyright (c) 2024-2025 by Philipp Schafft <lion@cpan.org>. | ||
| 111 | |||
| 112 | This is free software, licensed under: | ||
| 113 | |||
| 114 | The Artistic License 2.0 (GPL Compatible) | ||
| 115 | |||
| 116 | =cut |