Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Utility modules written in Perl, for Perl.

- Author: Ethan Kletschke
- Version: `0.0.1`
- Version: `0.0.5`
- Developed on: Ubuntu (WSL)
- License: MIT
- Project metadata file: [`project.yaml`](./meta/project.yaml)
9 changes: 3 additions & 6 deletions meta/BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
- [ ] `TextFiles` Module
- [ ] Log Parsing
- [x] Log type counter
- [ ] Log type filter (works like UNIX `grep` but more features(?))
- [x] Log type filter (works like UNIX `grep` but more features(?))
- [ ] CSV parsing
- [ ]
- [ ] General File Handling
- [ ] `can_open()` subroutine
- [ ] `cant_open()` subroutine
- [ ] `file_exists()` subroutine
- [ ] `file_not_exists()` subroutine
- [x] `can_open()` subroutine
- [x] `cant_open()` subroutine
4 changes: 2 additions & 2 deletions meta/project.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
project-meta:
project-name: PerlUtils
date-started: 2026-10-20
last-updated: 2026-10-28
last-updated: 2026-10-31
languages:
- Perl
status: Experimental
license: MIT
version: 0.0.1
version: 0.0.5

build:
dev-environments:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Utils/TextFiles/LogFileParsing.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sub count_logs_by_type {
# If the filename is not provided as an argument
unless (defined($log_type) && defined($filename)) {
# Abort the app (Temporary Code)
die "File name not provided";
die "Missing Parameter(s)";
}

# Open the specified log file for reading ('<')
Expand Down
92 changes: 92 additions & 0 deletions src/lib/Utils/TextFiles/Simple.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package Utils::TextFiles::Simple;

use strict;
use warnings;
use Exporter 'import';

our @EXPORT_OK = qw(
can_open
cant_open
);

sub can_open {
my ($filename) = @_;

open my $fh, '<', $filename or return 0;
close $fh or return 0;

return 1;
}

sub cant_open {
my ($filename) = @_;

open my $fh, '<', $filename or return 1;
close $fh or return 1;

return 0;
}

1;

__END__

=pod

=head1 NAME

C<TextFiles::Simple> - Simple utilities for handling text files.

=head1 SYNPOSIS

use Utils::TextFiles::Simple qw(can_open);

unless (can_open("dev.log")) {
die "Can't open dev.log: $!";
}

=head1 DESCRIPTION

The C<TextFiles::Simple> module provides utilities for
simple file handling, such as testing if a file can be opened successfully.

=head1 METHODS

=head2 can_open

Tests if a file can be opened successfully.

=head3 PARAMETERS

=over 4

=item C<$filename>

The name of the file to try open.

=back

=head3 RETURN VALUE

Returns C<1> if it can, or C<0> if it cannot be opened successfully.

=head2 cant_open

Tests if a file B<cannot> be opened successfully.

=head3 PARAMETERS

=over 4

=item C<$filename>

The name of the file to try open.

=back

=head3 RETURN VALUE

Returns C<1> if it cannot be opened successfully, or
C<0> if it can open successfully.

=cut
63 changes: 56 additions & 7 deletions src/t/TextFiles.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,68 @@ use lib 'lib';
use strict;
use warnings;
use Test::More;

use Utils::TextFiles::Simple qw(
can_open
cant_open
);
use Utils::TextFiles::LineCounter qw(
linecount
non_empty_linecount
);

use Utils::TextFiles::LogFileParsing qw(
count_logs_by_type
filter_logs
);

# Tests for Simple.pm
subtest "Simple" => sub {
subtest "can_open()" => sub {
open my $fh, '>', "Test.tmp"
or die "Could not create Test.tmp: $!";
print $fh "I can be opened!\n";
close $fh;

is(
can_open("Test.tmp"),
1,
"File can be opened and sub returns 0"
);

isnt(
can_open("Doesnt-Exist.tmp"),
1,
"Non-existent file returns 0"
);

unlink "Test.tmp";
};

subtest "cant_open()" => sub {
open my $fh, '>', "Exists.tmp"
or die "Could not open Exists.tmp: $!";
close $fh;

is(
cant_open("Exists.tmp"),
0,
"File does open, and sub returns 0"
);

is(
cant_open("Non-Existent-File.tmp"),
1,
"File cannot open, and sub returns 1"
);

unlink "Exists.tmp";
};
};

# Tests for LineCounter.pm
subtest "LineCounter" => sub {
subtest "linecount()" => sub {
# Write a test file to test the function results against
open my $fh, ">", "LineCounter.tmp" or die "Could not create LineCounter.tmp: $!\n";
open my $fh, ">", "LineCounter.tmp" or die "Could not create LineCounter.tmp: $!";
print $fh <<~EOF
line 1
line 2
Expand All @@ -29,7 +76,7 @@ subtest "LineCounter" => sub {
close $fh;

# Write an empty test file
open $fh, '>', "empty.txt" or die "Could not create empty.txt: $!\n";
open $fh, '>', "empty.txt" or die "Could not create empty.txt: $!";
close $fh;

# Test that the linecount subroutine correctly returns a count of 4
Expand Down Expand Up @@ -63,7 +110,7 @@ subtest "LineCounter" => sub {

subtest "non_empty_linecount()" => sub {
# Write a test file to test the function results against
open my $fh, ">", "LineCounter.tmp" or die "Could not create LineCounter.tmp: $!\n";
open my $fh, ">", "LineCounter.tmp" or die "Could not create LineCounter.tmp: $!";
print $fh <<~EOF
line 1
line 2
Expand All @@ -90,12 +137,13 @@ subtest "LineCounter" => sub {
"Doesn't count empty lines"
);

# Delete the testing file
# Delete the testing files
unlink "LineCounter.tmp";
unlink "empty.txt";
};
};

# Tests for LogFileParsing.pm
subtest "LogFileParsing" => sub {
subtest "count_logs_by_type()" => sub {
# Write a test file to test the function results against
Expand Down Expand Up @@ -165,4 +213,5 @@ subtest "LogFileParsing" => sub {
};
};

done_testing();
# Check that the number of executed tests is correct
done_testing(3);
Loading