Discussion:
[Xmltv-devel] Proposed code change to configure_stage
h***@gmail.com
2013-12-27 15:51:37 UTC
Permalink
Currently there's not a way to add "automatic" data to the config file during --configure.

A simplistic (but not necessarily realistic) example would be adding a version number to the config file: "version=123".


Each stage during --configure will only write values which *it* has collected (i.e. via "ask").

It's not even possible to write values manually during the stage_sub (either using Configure::LoadConfig/SaveConfig or via a manual open/print/close) since the $opt hash is not available so you don't know the name of the config-file to work with!

One way to handle this, is to pass the config-file name to the stage_sub. I.e. modify configure.pm L176
my $stage = &$stagesub( $nextstage, LoadConfig( $conffile ) );
to also include $conffile as a param.


Another way (I think better) is to allow a new key - "constant" - for write_string().

E.g.
$writer->write_string( {
id => 'version',
title => [ [ 'Version number', 'en' ] ],
description => [ [ 'Automatically added version number - no user input', 'en' ] ],
constant => '123',
} );

(Note the 'title' and 'description' are purely comments and not used anywhere in this case).

This would write
version=123
into the config-file.

Multiple constants could be written in the same 'stage' by simply repeating the $writer->write_string() call.


It only needs 6 lines (+POD) to be changed (in Configure.pm and Writer.pm) to support this:


--- S:/CVS/xmltv/lib/Configure/Writer.pm Sat Apr 24 12:28:22 2010
+++ S:/CVS/!temp/Writer.pm Fri Dec 27 14:44:18 2013
@@ -186,6 +186,9 @@
my $default = delete $ch{default};

$h{default} = $default if defined $default;
+
+ my $constant = delete $ch{constant};
+ $h{constant} = $constant if defined $constant;

$self->startTag( $tag, %h );

--- S:/CVS/xmltv/lib/Configure.pm Tue Apr 10 08:44:21 2012
+++ S:/CVS/!temp/Configure.pm Fri Dec 27 14:52:36 2013
@@ -216,16 +216,18 @@
my $title = getvalue( $p, 'title', $lang );
my $description = getvalue( $p, 'description', $lang );
my $default = $p->findvalue( '@default' );
+ my $constant = $p->findvalue( '@constant' );

my $value;

my $q = defined( $default ) ? "$title: [$default]" :
"$title:";

- say( "$description" );
+ say( "$description" ) if $constant eq '';
if( $tag eq 'string' )
{
- $value = ask( "$q" );
+ $value = $constant if $constant ne '';
+ $value = ask( "$q" ) if $constant eq '';
$value = $default if $value eq "";
print OUT "$id=$value\n";
}



Incidentally I believe the code

my $q = defined( $default ) ? "$title: [$default]" :
"$title:";

is wrong, since $q will not be undefined (XML::LibXML:Node->find does not return 'undefined') . This is why we get all those spurious [] when a config option has no default! It should be:

my $q = $default ne '' ? "$title: [$default]" :
"$title:";


What do you think?

Geoff

Loading...