Show quoted text> Config::Tiny does not provide an encoding setting as part of its API,
> and I can't seem to find any hacks to force it. Any chance of
> recognizing this as a feature request?
I quickly worked something up which does what I need:
my $conf = Config::Tiny->read('file', 'utf-8')
# hack hack hack
$conf->write('file', 'utf-8')
It relies on the PerlIO layer so may or may not be portable for all
Perls that have ever been written.
The patch below will not apply to the latest release (I cleaned up my
local copy of the code using perltidy and podtidy before hand) but
shows what I'm suggesting. If you want the full patch set see:
git clone git@github.com:mlawren/Config-Tiny.git
or via browser at
https://github.com/mlawren/Config-Tiny
Mark.
---
lib/Config/Tiny.pm | 32 ++++++++++++++++++++------------
1 files changed, 20 insertions(+), 12 deletions(-)
diff --git a/lib/Config/Tiny.pm b/lib/Config/Tiny.pm
index 25b9b7c..05aaf8b 100644
--- a/lib/Config/Tiny.pm
+++ b/lib/Config/Tiny.pm
@@ -25,9 +25,13 @@ sub read {
return $class->_error("Insufficient permissions to read '$file'")
unless -r _;
+ my $encoding = @_ ? '<:encoding(' . (shift) . ')' : '<';
+
# Slurp in the file
local $/ = undef;
- open CFG, $file or return $class->_error("Failed to open file '$file': $!");
+
+ open CFG, $encoding, $file
+ or return $class->_error("Failed to open file '$file': $!");
my $contents = <CFG>;
close CFG;
@@ -76,12 +80,13 @@ sub read_string {
# Save an object to a file
sub write {
- my $self = shift;
- my $file = shift or return $self->_error( 'No file name provided' );
+ my $self = shift;
+ my $file = shift or return $self->_error('No file name provided');
+ my $encoding = @_ ? '>:' . (shift) : '>';
# Write it to the file
- open( CFG, '>' . $file )
- or return $self->_error( "Failed to open file '$file' for writing: $!" );
+ open CFG, $encoding, $file
+ or return $self->_error("Failed to open file '$file' for writing: $!");
print CFG $self->write_string;
close CFG;
}
@@ -93,7 +98,8 @@ sub write_string {
my $contents = '';
foreach my $section (
sort { ( ( $b eq '_' ) <=> ( $a eq '_' ) ) || ( $a cmp $b ) }
- keys %$self )
+ keys %$self
+ )
{
my $block = $self->{$section};
$contents .= "\n" if length $contents;
@@ -139,7 +145,7 @@ possible
my $Config = Config::Tiny->new();
# Open the config
- $Config = Config::Tiny->read( 'file.conf' );
+ $Config = Config::Tiny->read( 'file.conf', 'utf-8' );
# Reading properties
my $rootproperty = $Config->{_}->{rootproperty};
@@ -152,7 +158,7 @@ possible
delete $Config->{_}; # Delete a value or section
# Save a config
- $Config->write( 'file.conf' );
+ $Config->write( 'file.conf', 'utf-8' );
=head1 DESCRIPTION
@@ -197,10 +203,11 @@ something better, this module is not for you.
The constructor C<new> creates and returns an empty C<Config::Tiny>
object.
-=head2 read $filename
+=head2 read $filename [$encoding]
The C<read> constructor reads a config file, and returns a new
-C<Config::Tiny> object containing the properties in the file.
+C<Config::Tiny> object containing the properties in the file. An
+optional argument specifies the encoding.
Returns the object on success, or C<undef> on error.
@@ -215,10 +222,11 @@ C<$!> variable.
The C<read_string> method takes as argument the contents of a config
file as a string and returns the C<Config::Tiny> object for it.
-=head2 write $filename
+=head2 write $filename [$encoding]
The C<write> method generates the file content for the properties, and
-writes it to disk to the filename specified.
+writes it to disk to the filename specified. The encoding can be
+optionally specified as well.
Returns true on success or C<undef> on error.
--
Mark Lawrence