commit 618ba1715bb12777f4afd3312d02c3c4501b43f2 from: Izzy Blacklock date: Fri May 26 17:02:22 2023 UTC Moved basic io functions from botnow to lib/IRCNOW/IO.pm Started moving code from botnow into IRCNOW:: library commit - 7deb630f2352fb316c67d518f8d2795b059e92b9 commit + 618ba1715bb12777f4afd3312d02c3c4501b43f2 blob - /dev/null blob + bb95f971cfac3a52b38c9e9a460a6a42cfe933be (mode 644) --- /dev/null +++ lib/IRCNOW/IO.pm @@ -0,0 +1,42 @@ +package IRCNOW::IO + +use File::Copy qw(copy); +use File::Basename; + + +# Read from filename and return array of lines without trailing newlines +sub readarray { + my ($filename) = @_; + open(my $fh, '<', $filename) or die "Could not read file '$filename' $!"; + chomp(my @lines = <$fh>); + close $fh; + return @lines; +} + +# Read from filename and return as string +sub readstr { + my ($filename) = @_; + open(my $fh, '<', $filename) or die "Could not read file '$filename' $!"; + my $str = do { local $/; <$fh> }; + close $fh; + return $str; +} + +# Write str to filename +sub writefile { + my ($filename, $str) = @_; + my $date = date(); + copy($filename, $backupspath.basename($filename).".".date()) or die "Could not make backup of $filename"; + open(my $fh, '>', "$filename") or die "Could not write to $filename"; + print $fh $str; + close $fh; +} + +# Append str to filename +sub appendfile { + my ($filename, $str) = @_; + open(my $fh, '>>', "$filename") or die "Could not append to $filename"; + print $fh $str; + close $fh; +} +