| 1 | package Daje::Tools::Filechanged; | ||
| 2 | 0 | ▶ | use Mojo::Base -base, -signatures; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | |||
| 4 | use Digest::SHA qw(sha256_hex); | ||
| 5 | use Mojo::File; | ||
| 6 | |||
| 7 | # Daje::Tools::Filechanged - It's new $module | ||
| 8 | # | ||
| 9 | # SYNOPSIS | ||
| 10 | # ======== | ||
| 11 | # | ||
| 12 | # use Daje::Tools::Filechanged; | ||
| 13 | # | ||
| 14 | # my $changes = Daje::Tools::Filechanged->new( | ||
| 15 | # | ||
| 16 | # )->is_file_changed( | ||
| 17 | # | ||
| 18 | # $file_path_name, $old_hash | ||
| 19 | # ): | ||
| 20 | # | ||
| 21 | # DESCRIPTION | ||
| 22 | # =========== | ||
| 23 | # | ||
| 24 | # Daje::Tools::Filechanged is a tool to check if two hashes are equal | ||
| 25 | # | ||
| 26 | # | ||
| 27 | # METHODS | ||
| 28 | # ======= | ||
| 29 | # | ||
| 30 | # my $changed = $self->is_file_changed($file_path_name, $old_hash); | ||
| 31 | # | ||
| 32 | # Is the hashes different ? | ||
| 33 | # LICENSE | ||
| 34 | # ======= | ||
| 35 | # | ||
| 36 | # Copyright (C) janeskil1525. | ||
| 37 | # | ||
| 38 | # This library is free software; you can redistribute it and/or modify | ||
| 39 | # it under the same terms as Perl itself. | ||
| 40 | # | ||
| 41 | # AUTHOR | ||
| 42 | # ====== | ||
| 43 | # | ||
| 44 | # janeskil1525 janeskil1525@gmail.com | ||
| 45 | # | ||
| 46 | |||
| 47 | our $VERSION = "0.02"; | ||
| 48 | |||
| 49 | # Is the hashes different ? | ||
| 50 | sub is_file_changed($self, $file_path_name, $old_hash) { | ||
| 51 | my $result = 0; | ||
| 52 | my $path = Mojo::File->new($file_path_name); | ||
| 53 | my $new_hash = $self->load_new_hash($path); | ||
| 54 | $old_hash = "" unless defined $old_hash; | ||
| 55 | if ($new_hash ne $old_hash) { | ||
| 56 | $result = 1; | ||
| 57 | } | ||
| 58 | return $result; | ||
| 59 | } | ||
| 60 | |||
| 61 | # Load new hash | ||
| 62 | sub load_new_hash($self, $path) { | ||
| 63 | my $file_content = $path->slurp; | ||
| 64 | my $hash = sha256_hex($file_content); | ||
| 65 | return $hash; | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | __END__ | ||