Skip Menu |

This queue is for tickets about the Config-General CPAN distribution.

Report information
The Basics
Id: 27622
Status: resolved
Priority: 0/
Queue: Config-General

People
Owner: tlinden [...] cpan.org
Requestors: gm_post [...] br-online.de
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: (no value)
Fixed in: (no value)



Subject: reusability from includes
If you use an include more than one time, it's on possible include one time. The problem is in sub _open { ... if (-d $configfile and $this->{IncludeDirectories}) { ... } elsif (-e _) { if (exists $this->{files}->{$configfile} ) { # do not read the same file twice, just return # FIXME: should we croak here, when some "debugging" is enabled? return; } else { ... } } This part prevent the reusability from include files. I think it's an important feature. Please can you fix this, i.e. if (exists $this->{files}->{$configfile} and !$this->{ReusableInclude}) { # do not read the same file twice, just return # FIXME: should we croak here, when some "debugging" is enabled? return; } ReusableInclude parameter has default value 0. Thank you Gerd!
From: perl [...] pied.nu
I agree. The "Including only once" feature was added in 2.28 without a reason, really. I made use of this feature and spent a some time tracking down why my code was broken. Ideally, the param should have been -IncludeOnce (defaults to 0) to avoid breaking existing code. I'll work on a patch. However, I'm wondering why someone would want to avoid including twice the same file. If you don't want it twice, don't use 2 includes.
Here is a patch for -IncludeAgain. I called it thusly so it could be like -IncludePath, -IncludeGlob, etc.
diff -rubN Config-General-2.33/General.pm Config-General-2.33-PG/General.pm --- Config-General-2.33/General.pm 2007-04-17 14:50:43.000000000 -0400 +++ Config-General-2.33-PG/General.pm 2007-08-21 20:38:35.000000000 -0400 @@ -57,6 +57,7 @@ IncludeRelative => 0, IncludeDirectories => 0, IncludeGlob => 0, + IncludeAgain => 0, AutoLaunder => 0, AutoTrue => 0, AutoTrueFlags => { @@ -448,9 +449,10 @@ } } elsif (-e _) { - if (exists $this->{files}->{$configfile} ) { + if (exists $this->{files}->{$configfile} and not $this->{IncludeAgain} ) { # do not read the same file twice, just return # FIXME: should we croak here, when some "debugging" is enabled? + warn "File $configfile already loaded. Use -IncludeAgain to load it again.\n"; return; } else { @@ -1378,6 +1380,18 @@ is often more desirable than including a directory with B<-IncludeDirectories>. +=item B<-IncludeAgain> + +If set to a true value, you will be able to include a sub-configfile +multiple times. With the default, false, you will get a warning about +duplicate includes and only the first include will succeed. + +Reincluding a configfile can be useful if it contains data that you want to +be present in multiple places in the data tree. See the example under +L</INCLUDES>. + +Note, however, that there is currently no check for include recursion. + =item B<-ConfigPath> As mentioned above, you can use this variable to specify a search path for relative @@ -2164,7 +2178,32 @@ Include statements will be ignored within C-Comments and here-documents. +By default, a config file will only be included the first time it is +referenced. If you wish to include a file in multiple places, set +</-IncludeAgain> to true. + +Example: + + # main.cfg + <object billy> + class=Some::Class + <printers> + include printers.cfg + </printers> + # ... + </object> + <object bob> + class=Another::Class + <printers> + include printers.cfg + </printers> + # ... + </object> + +Now C<printers.cfg> will be include in both the C<billy> and C<bob> objects. +You will have to be careful to not recursively include a file. Behaviour +in this case is undefined. =head1 COMMENTS diff -rubN Config-General-2.33/MANIFEST Config-General-2.33-PG/MANIFEST --- Config-General-2.33/MANIFEST 2007-02-23 17:12:30.000000000 -0500 +++ Config-General-2.33-PG/MANIFEST 2007-08-21 20:25:47.000000000 -0400 @@ -26,8 +26,11 @@ t/cfg.20.b t/cfg.20.c t/run.t -t/test.rc.out t/cfg.34 +t/apache-include.conf +t/dual-include.conf +t/include.t +t/included.conf MANIFEST example.cfg Makefile.PL diff -rubN Config-General-2.33/t/apache-include.conf Config-General-2.33-PG/t/apache-include.conf --- Config-General-2.33/t/apache-include.conf 1969-12-31 19:00:00.000000000 -0500 +++ Config-General-2.33-PG/t/apache-include.conf 2007-08-21 20:14:12.000000000 -0400 @@ -0,0 +1,6 @@ +<bit one> + include t/included.conf +</bit> +<bit two> + include t/included.conf +</bit> diff -rubN Config-General-2.33/t/dual-include.conf Config-General-2.33-PG/t/dual-include.conf --- Config-General-2.33/t/dual-include.conf 1969-12-31 19:00:00.000000000 -0500 +++ Config-General-2.33-PG/t/dual-include.conf 2007-08-21 20:08:23.000000000 -0400 @@ -0,0 +1,6 @@ +<bit one> + <<include t/included.conf>> +</bit> +<bit two> + <<include t/included.conf>> +</bit> diff -rubN Config-General-2.33/t/include.t Config-General-2.33-PG/t/include.t --- Config-General-2.33/t/include.t 1969-12-31 19:00:00.000000000 -0500 +++ Config-General-2.33-PG/t/include.t 2007-08-21 20:15:26.000000000 -0400 @@ -0,0 +1,52 @@ +# -*-perl-*- +# testscript for Config::General's -IncludeAgain +# +# needs to be invoked using the command "make test" from +# the Config::General source directory. +# +# Under normal circumstances every test should succeed. + + +use Data::Dumper; +use Test::More tests => 8; +#use Test::More qw(no_plan); + +### 1 +BEGIN { use_ok "Config::General"}; +require_ok( 'Config::General' ); + + +### Include both +my $conf = Config::General->new( -ConfigFile => "t/dual-include.conf", + -IncludeAgain => 1 ); +ok( $conf, "Loaded the config file" ); + +my %C = $conf->getall; +is_deeply( \%C, { bit => { one => { honk=>'bonk' }, + two => { honk=>'bonk' } + } }, "Included twice" ); + + + +### Include once +diag "\nPlease ignore the following message about IncludeAgain"; +$conf = Config::General->new( "t/dual-include.conf" ); +ok( $conf, "Loaded the config file" ); + +%C = $conf->getall; + +is_deeply( \%C, { bit => { one => { honk=>'bonk' }, + two => {} + } }, "Included once-only" ); + +### apache-style Include +$conf = Config::General->new( -ConfigFile => "t/apache-include.conf", + -IncludeAgain => 1, + -UseApacheInclude => 1 ); +ok( $conf, "Loaded the config file" ); + +%C = $conf->getall; + +is_deeply( \%C, { bit => { one => { honk=>'bonk' }, + two => { honk=>'bonk' } + } }, "Apache-style include" ); diff -rubN Config-General-2.33/t/included.conf Config-General-2.33-PG/t/included.conf --- Config-General-2.33/t/included.conf 1969-12-31 19:00:00.000000000 -0500 +++ Config-General-2.33-PG/t/included.conf 2007-08-21 20:10:39.000000000 -0400 @@ -0,0 +1 @@ +honk=bonk
Sorry for the delay, I were not notified about the ticket (as always, they seem to be too stupid to get some mail outa here...). For now - there is of course a reason. It was implemented to avoid include loops. Your patch doesn't avoid loops either. However - I'll take a look at it. In addition I cannot apply the patch, because I already changed the code.
So far I've applied the patch. However, there's still no loop detection. But there are some more issues left, so I'll try to implement it.
Subject: Re: [rt.cpan.org #27622] Resolved: reusability from includes
Date: Tue, 16 Oct 2007 11:13:17 +0200
To: bug-Config-General [...] rt.cpan.org
From: gm_post [...] br-online.de
Thomas Linden via RT wrote: Show quoted text
> <URL: http://rt.cpan.org/Ticket/Display.html?id=27622 > > > According to our records, your request has been resolved. If you have any > further questions or concerns, please respond to this message. > >
Thank you. I will test your fix next time. Than I can give you any comments. Gerd -- ----------------------------------------------------- Gerd Mucha BR-Online Rundfunkplatz 1 80300 München Tel: 089/5900 16024 Fax: 089/5900 16000 gm_post@br-online.de -----------------------------------------------------
RT re-opened the case for whatever reason. Re-closing it.