diff --git a/Build.PL b/Build.PL new file mode 100644 index 0000000..e687642 --- /dev/null +++ b/Build.PL @@ -0,0 +1,81 @@ + +# This file was automatically generated by Dist::Zilla::Plugin::ModuleBuild v5.047. +use strict; +use warnings; + +use Module::Build 0.28; + + +my %module_build_args = ( + "build_requires" => { + "Module::Build" => "0.28" + }, + "configure_requires" => { + "ExtUtils::MakeMaker" => 0, + "Module::Build" => "0.28" + }, + "dist_abstract" => "A module for reading .ini-style configuration files.", + "dist_author" => [ + "Shlomi Fish " + ], + "dist_name" => "Config-IniFiles", + "dist_version" => "2.94", + "license" => "perl", + "module_name" => "Config::IniFiles", + "recursive_test_files" => 1, + "requires" => { + "Carp" => 0, + "Fcntl" => 0, + "File::Basename" => 0, + "File::Temp" => 0, + "IO::Scalar" => 0, + "List::Util" => "1.33", + "Symbol" => 0, + "perl" => "5.008", + "strict" => 0, + "vars" => 0, + "warnings" => 0 + }, + "test_requires" => { + "English" => 0, + "Exporter" => 0, + "File::Spec" => 0, + "File::Temp" => 0, + "IO::File" => 0, + "IO::Handle" => 0, + "IPC::Open3" => 0, + "Scalar::Util" => 0, + "Test::More" => 0, + "base" => 0, + "blib" => "1.01", + "lib" => 0 + } +); + + +my %fallback_build_requires = ( + "English" => 0, + "Exporter" => 0, + "File::Spec" => 0, + "File::Temp" => 0, + "IO::File" => 0, + "IO::Handle" => 0, + "IPC::Open3" => 0, + "Module::Build" => "0.28", + "Scalar::Util" => 0, + "Test::More" => 0, + "base" => 0, + "blib" => "1.01", + "lib" => 0 +); + + +unless ( eval { Module::Build->VERSION(0.4004) } ) { + delete $module_build_args{test_requires}; + $module_build_args{build_requires} = \%fallback_build_requires; +} + +my $build = Module::Build->new(%module_build_args); + + +$build->create_build_script; diff --git a/Changes b/Changes new file mode 100644 index 0000000..e2f0ec6 --- /dev/null +++ b/Changes @@ -0,0 +1,465 @@ +2.94 2016-11-29 + * Move to GitHub. + - for better visibility, collaboration, and CI options. + * Apply a patch/pull-request from Peter from Debian with some corrections + for typos: + - https://github.com/shlomif/perl-Config-IniFiles/pull/1 + - https://rt.cpan.org/Ticket/Display.html?id=119001 + - Thanks! + +2.93 2016-07-24 + * Add [MetaJSON] and META.json to the distribution. + - Pull-request by Kent Fredric - thanks! + - https://bitbucket.org/shlomif/perl-config-inifiles/pull-requests/5/ship-metajson-with-metajson/diff + +2.92 2016-06-17 + * Hopefully fix tests with read/write test01.ini: + - http://www.cpantesters.org/cpan/report/121523be-341c-11e6-9fc5-14bfbd15da07 + * Some cleanups and modernisations to the tests. + +2.91 2016-06-03 + * Set minimal version of List::Util in the requires and in the code. + - Reported by some CPAN testers. + +2.90 2016-06-02 + * Convert to Dist-Zilla with some induced enhancements. + +2.89 2016-05-03 + * Abort if print returns false. + - See https://rt.cpan.org/Ticket/Display.html?id=114140 + - "WriteConfig nullifies the file in case of no space left on disk" + +2.88 2015-07-10 + * Apply speedup patch from James Rouzier. + - https://bitbucket.org/shlomif/perl-config-inifiles/pull-request/4/avoid-searching-through-all-the-sections/commits + - Avoids it being O(n^2). + +2.87 2015-06-16 + * Replace List::MoreUtils with List::Util-1.33-or-above. + - We only used any() and none() which exist in List::Util too now. + - https://rt.cpan.org/Public/Bug/Display.html?id=104764 + - Thanks to DBOOK for the report. + * Fix handling of trailing comments which contain their delimiters twice. + - E.g: "key=val ; comment ; further comment". + - https://rt.cpan.org/Ticket/Display.html?id=105255 + - Thanks to Rohan Carly for the report and a test case. + +2.86 2015-04-14 + * Hopefully fix tests in perls less than 5.14 + - Failed on CPAN Tester reports such as: + - http://www.cpantesters.org/cpan/report/88707ece-e22d-11e4-a1d1-8536eb4f9f07 + - Thanks to Slaven Rezic for pointing it to me. + +2.85 2015-04-13 + * Convert "Changes" (= this file) to CPAN::Changes. + - Added t/cpan-changes.t in the process. + * Add OLD-Changes.txt for the old changes' log. + +2.84 2015-04-13 + + * Fix https://rt.cpan.org/Public/Bug/Display.html?id=103496 + - Improperly update while using _write_config_with_a_made_fh + - Thanks to JORISD for the report, a proposed patch and a test case. + +2.83 2014-01-27 + + * Add a LICENSE file and section. + - Kwalitee. + + * Minimum version is now perl-5.8.0. + - CPANTS Kwalitee. + +2.82 2013-05-21 + + * Add t/style-trailing-space.t . + - Remove trailing space. + +2.81 2013-05-16 + + * Add the CopySection method to copy a section. + - Thanks to James Rouzier. + +2.80 2013-05-14 + + * Add the RenameSection method to rename a section. + - Thanks to James Rouzier. + +2.79 2013-05-6 + + * Fix test failures with Pod-Simple-3.28: + - http://www.cpantesters.org/cpan/report/98f9d3a8-b557-11e2-9adc-3d5fc1508286 + +2.78 2012-10-21 + + * Fix https://rt.cpan.org/Public/Bug/Display.html?id=80259: + - Warnings on undefined value in length in perl-5.10.x. + +2.77 2012-06-21 + + * Bump the List::MoreUtils dependency to '0.33'. + - https://rt.cpan.org/Ticket/Display.html?id=77930 + - Thanks to TJENNESS for the report. + +2.76 2012-06-15 + + * Correct the documentation regarding the syntax of calling ->WriteConfig(), + which always accepts a mandatory filename and not just + "->WriteConfig(-delta => 1)". + - Thanks to the Mageia Linux QA team at: + - https://bugs.mageia.org/show_bug.cgi?id=6024#c17 + +2.75 2012-05-25 + + * Add "=encoding utf8" to the POD of lib/Config/IniFiles.pm + - The POD contained wide characters and it is now an error in + with recent Test::Pod and Pod::Simple. + - See for example: http://www.cpantesters.org/cpan/report/89000ebe-a5bc-11e1-a439-fa78ab6c2661 + +2.74 2012-05-24 + + * Made sure .ini files are output on Win32 with CR-LF pairs. + - Thanks to Ben Johnson for reporting the bug and for a preliminary + test script. + - Add t/32mswin-outputs-contain-crs.t + +2.73 2012-05-14 + + * Regression bug fix: temporary file creation error should generate a warning + and return undef() instead of throwing an exception. + - https://rt.cpan.org/Ticket/Display.html?id=77039 + - Thanks to Adam D. Barrat for noticing that, and Gregor Herrmann + from Debian for reporting this issue. + +2.72 2012-05-5 + + * More refactoring. + + * Now requiring Fcntl (which is a core module) for SEEK_SET() and SEEK_CUR(). + + * Update the contact information on the POD. + + * Maintainer’s special birthday (5-May) release. + +2.71 2012-05-2 + + * SECURITY BUG FIX: Config::IniFiles used to write to a temporary filename + with a predictable name ("${filename}-new") which opens the door for potential + exploits. + + * Now requiring List::MoreUtils (for any() and other functions), File::Temp + and File::Basename . + + * Add "use warnings;" to lib/Config/IniFiles.pm . + + * More refactoring. + +2.70 2012-04-6 + + * Fix the test dependency (in the new t/31comments_with_spaces.t test) + on IO::stringy (now writing it to a file). + - This caused some CPAN Testers failures such as: + - http://www.cpantesters.org/cpan/report/c16b9978-7f40-11e1-9d6f-f6dbfa7543f5 + + * Modernized some of the open() calls in the test files. + - three args open, lexcial file handles, etc. + +2.69 2012-04-5 + + * Fix https://sourceforge.net/tracker/?func=detail&aid=3388382&group_id=6926&atid=106926 + - "Trailing comments with whitespace are not recognised" + - Thanks to briconaut for the report and to rbowen for the patch and test. + + * Add the method ->OutputConfigToFileHandle which is a more explicit and + safer version of ->OutputConfig. + - ->OutputConfig now uses it. + - all existing tests pass, and a new test was added to + t/15store-and-retrieve-here-doc-terminator.t . + + * Started refactoring the code. + - Expand tabs to spaces. + - many other small changes. + +2.68 2011-06-21 + + * Typo correction: https://rt.cpan.org/Ticket/Display.html?id=69003 + +2.67 2011-06-21 + + * Made sure that parameters with empty values will be written to the file. + - Fixes https://rt.cpan.org/Public/Bug/Display.html?id=68554 . + +2.66 2011-01-29 + + * Add support for the handle_trailing_comment option to support trailing + comments at the end of a param=value line. + - Fixes https://rt.cpan.org/Public/Bug/Display.html?id=43843 . + - Original patch by Peter Xu. + - Some comments and corrections of the patch by Shlomi Fish (the current + Config-IniFiles maintainer.) + +2.65 2010-11-25 + + * Replaced calls to File::Temp's tempfile() with those to its tempdir() + followed by File::Spec->catfile() - hopefully fixes the CPAN smoking failures + on Win32. + +2.64 2010-11-20 + + * Fixed https://rt.cpan.org/Ticket/Display.html?id=63177 + - tag-release.pl was installed into lib/Config by EU-MM. Converted the + Makefile.PL to a wrapper around Build.PL by using Module-Build's + create_makefile_pl='small' instead of create_makefile_pl='traditional' . + +2.63 2010-11-19 + + * Fixed https://rt.cpan.org/Ticket/Display.html?id=63079 by + refactoring/bug-fixing the main module into the _OutputParam method, which + handles the output of multi-line and single-line parameters with all the edge + cases. + - The reported problem was that EOT was encountered again in certain cases + in the output string, and now we make sure the program always avoid it. The + reappearance of the bug was caused due to duplicate code, which was now + eliminated. + +2.62 2010-11-19 + + * Applied a modified version of Steven Haryanto's commit + ac6ace8bf09fabba4e6ea3b2826ff13cd30507bc : + - When writing config with fallback section, also omit the fallback + section header, like the original file. Also, change + t/19param-found-outside-section.t a bit to not require IO::Scalar and use + File::Temp instead (we are testing WriteConfig anyway). + - Shlomi Fish fixed unless'es to if's and converted a do { ... } if call to + a block conditional, as well as made the open statements on t/19param + more secure. + + * Add -nomultiline option. + -Applied a modified version of Steven Haryanto's commit + 2181f438d40b54c290a2dda5627a244c6e467a7b + +2.61 2010-11-14 + + * Correct the opening comment on t/26scalar-filehandle.t to point to the right + bug URL and to include the title of the bug + - https://rt.cpan.org/Ticket/Display.html?id=54997 - this is the right + URL. + + * Correct the check in t/26scalar-filehandle.t - it should check that Perl + is above 5.8.x - not that IO::Scalar is available, because we use + open my $fh, "<", \$buffer; + +2.60 2010-11-13 + + * Made t/19param-found-outside-section.t pass in case IO::Scalar is not + installed. + - We got many failing test reports due to that, like: + - http://www.cpantesters.org/cpan/report/eb3df0f4-eeb7-11df-b934-9430e12b63f3 + +2.59 2010-11-12 + + * Applied a patch from "sharyanto" about enabling the -fallback parameter + to new for items outside any section. + - https://rt.cpan.org/Public/Bug/Display.html?id=62944 + + * Add the tag-release.pl file to tag a release. + +2.58 2010-05-17 + + * Incorporated a fix for some broken POD. + - https://rt.cpan.org/Ticket/Display.html?id=51617 + - Thanks to Frank Wiegand for reporting it. + + * Got rid of \r/carriage-returns's in lib/Config/IniFiles.pm . + - It interfered with applying a patch. + + * Applied a patch to get rid of indirect object notation in the documentation + and code ("new Config::IniFiles") and replace it with Config::IniFiles->new(). + - https://rt.cpan.org/Ticket/Display.html?id=57536 + - Thanks to Tom Molesworth for the patch. + +2.57 2010-03-1 + + * Fixed failure to read the ini file contents from a filehandle made out of a + scalar + - https://rt.cpan.org/Ticket/Display.html?id=45997 + - Applied a patch by Nicolas Mendoza - thanks! + +2.56 2009-12-31 + + * Hopefully fixed the skip() in t/07misc.t + - https://rt.cpan.org/Public/Bug/Display.html?id=53238 + - It was skip($num, $label) instead of skip($label, $num) and it + lacked a "SKIP:" label. + +2.55 2009-12-22 + + * Refactoring the tests: + - Converted t/05hash.t to Test::More and modernised it. + - Converted t/06oo.t to Test::More and modernised it. + - Testing: extracted t/lib/Config/IniFiles/TestPaths.pm. + - Converted t/10delta.t to Test::More and modernised it. + - Add the t_unlink method. + +2.54 2009-11-18 + + * Adapted t/25line-endings.t to perl-5.6.x . + - http://www.nntp.perl.org/group/perl.cpan.testers/2009/11/msg6026936.html + - Failure there. + - did that by replacing open ">:raw" with "binmode()". + +2.53 2009-11-13 + + * Converted t/01basic.t to Test::More and modernised it. + * Converted t/02weird.t to Test::More and modernised it. + * Converted t/04import.t to Test::More and modernised it. + * Applied a patch from Steffen Heinrich (SHE) for: + - https://rt.cpan.org/Ticket/Display.html?id=51445 + - included t/25line-endings.t + - modified a little. + +2.52 2009-06-28 + + * Added more keys to Build.PL (author, etc.) + * Added resources and keywords to META.yml. + * Converted t/00load.t to Test-More. + * No longer chdir()-ing into "t" in t/00load.t + * Got rid of the "new Config::IniFiles" syntax in the tests. + - replaced with Config::IniFiles->new(). + - see "Perl Best Practices", "Programming Perl", 3rd Edition + +2.51 2009-06-8 + + * Fixed "#46721: $config->exists() does not pay attention to -nocase => 1" + - https://rt.cpan.org/Ticket/Display.html?id=46721 + +2.50 2009-05-31 + + * Fixed the POD for SetParameterEOT: + - https://rt.cpan.org/Ticket/Display.html?id=28375 + * Added t/pod.t and t/pod-coverage.t and fixed the problems they pointed: + - Removed some trailing pod. + - http://rt.cpan.org/Ticket/Display.html?id=46549 + - Documented the ->OutputConfig() method. + +2.49 2009-05-2 + + * Fixed bug https://rt.cpan.org/Public/Bug/Display.html?id=45209 : + - Makefile.PL lacked the empty "PL_FILES" directive. + + * Fixed bug https://rt.cpan.org/Ticket/Display.html?id=45588 : + - Failure to read ini file contents from a scalar (via "IO::Scalar"). + - added t/23scalar-ref.t + +2.48 2009-04-7 + + * Fixed bug https://rt.cpan.org/Ticket/Display.html?id=30402 : + - Trailing comments at the end of the file are not written. + - added a test (t/22trailing-comment-lines.t ) and a test file. + + * Reverted the fix to bug #34067 ( "Multiline values returned as array + references in tied hash interface"), as it could potentially break a lot + of old code, and was not such a good idea, and was less preferable from + interface-sanity reasons. + + * Documented the old API in regards to bug #34067 - see above. + +2.47 2009-01-21 + + * Fixed bug https://rt.cpan.org/Ticket/Display.html?id=34067 : + - Multiline values returned as array references in tied hash interface + - This was as opposed to the documentation. + - added t/21multiline-values-in-tied.t and fixed some older tests. + + * Moved the eval'ed debugging code (_assert_invariants) to its own module, + without the eval so the perl-5.6.2 will be happy. Now explicitly use'ing it + from t/lib where appropriate. See for example: + - http://www.nntp.perl.org/group/perl.cpan.testers/2009/01/msg3101788.html + +2.46 2009-01-17 + + * Added regression tests for bug #1720915 + - https://sourceforge.net/tracker/index.php?func=detail&aid=1720915&group_id=6926&atid=106926 + - the bug seems to be fixed in trunk. + + * Added a regression test for RT #36584 + - https://rt.cpan.org/Ticket/Display.html?id=36584 + - the bug seems to be fixed in trunk. + + * Added a regression test for RT #36309 + - https://rt.cpan.org/Ticket/Display.html?id=36309 + - Testing for allowedcommentchars. + - the bug seems to be fixed in trunk. + + * Converted t/03comments.t to Test::More and did other cleanups in preparation + of solving a few bugs. + + * Now GetSectionComment does the documented behaviour in scalar context and + returns the string joined by newlines: + - https://rt.cpan.org/Ticket/Display.html?id=8612 + + * Now GetParameterComment returns the right thing in scalar context (joined + by newlines). It was documented and tested. + - https://rt.cpan.org/Ticket/Display.html?id=8612 + +2.45 2008-12-27 + + * Fixed bug #1230339: + - https://sourceforge.net/tracker/index.php?func=detail&aid=1230339&group_id=6926&atid=106926 + - Can't store and retrive "\nEOT\n" + - Fixed by checking for the existence of the here-doc terminator in the + string, and if so appending more characters. + + * Fixed bug #1565180 + - https://sourceforge.net/tracker/index.php?func=detail&aid=1565180&group_id=6926&atid=106926 + - -nocase breaks -default. + - If a default-values section is specified, then -nocase did not look it up + if it's not lowercase. + + * Fixed bug #1831654 + - https://sourceforge.net/tracker/index.php?func=detail&aid=1831654&group_id=6926&atid=106926 + - multiline-values are tainted. + - Added a test as t/17untainted-multiline-values.t + +2.44 2008-12-25 + + * Eliminated an assignment of $VERSION into itself to "get rid of warnings". + Only caused warnings at Build.PL, and is unnecessary now that we + "use vars". + +2.43 2008-12-4 + + * Placed IniFiles.pm under lib/Config/IniFiles.pm. + + * Added a Build.PL file for the distribution. + + * Moved the ChangeLog to its own file - Changes. + + * Made the README contain the useful information and nothing else - previously + it was a copy of the module's entire POD. + + * Added a regression test for: + + https://sourceforge.net/tracker/index.php?func=detail&aid=778593&group_id=6926&atid=106926 + + When copying tied-hashes representing sections with multiline values as + keys, one used to get a reference instead of a copy. This was fixed in SVN + but not in CPAN. + + * Added a test for opening an empty file, inspired by: + + https://sourceforge.net/tracker/index.php?func=detail&aid=927111&group_id=6926&atid=106926 + + * Added a fix for this bug: + + https://sourceforge.net/tracker/index.php?func=detail&aid=767913&group_id=6926&atid=106926 + + With a test that I wrote myself: in list context, ->val() returned the list + containing (undef()) if there was no such key. Now returning the empty + list which evaluates to false. + + * Added a test for bug + + "[ 2030786 ] Brackets within values are treated as section headers" + + It seems to be fixed on SVN. + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0ba8d2b --- /dev/null +++ b/LICENSE @@ -0,0 +1,396 @@ +Terms of Perl itself + +a) the GNU General Public License as published by the Free + Software Foundation; either version 1, or (at your option) any + later version, or +b) the "Artistic License" + +---------------------------------------------------------------------------- + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + +---------------------------------------------------------------------------- + +The Artistic License + +Preamble + +The intent of this document is to state the conditions under which a Package +may be copied, such that the Copyright Holder maintains some semblance of +artistic control over the development of the package, while giving the users of the +package the right to use and distribute the Package in a more-or-less customary +fashion, plus the right to make reasonable modifications. + +Definitions: + +- "Package" refers to the collection of files distributed by the Copyright + Holder, and derivatives of that collection of files created through textual + modification. +- "Standard Version" refers to such a Package if it has not been modified, + or has been modified in accordance with the wishes of the Copyright + Holder. +- "Copyright Holder" is whoever is named in the copyright or copyrights for + the package. +- "You" is you, if you're thinking about copying or distributing this Package. +- "Reasonable copying fee" is whatever you can justify on the basis of + media cost, duplication charges, time of people involved, and so on. (You + will not be required to justify it to the Copyright Holder, but only to the + computing community at large as a market that must bear the fee.) +- "Freely Available" means that no fee is charged for the item itself, though + there may be fees involved in handling the item. It also means that + recipients of the item may redistribute it under the same conditions they + received it. + +1. You may make and give away verbatim copies of the source form of the +Standard Version of this Package without restriction, provided that you duplicate +all of the original copyright notices and associated disclaimers. + +2. You may apply bug fixes, portability fixes and other modifications derived from +the Public Domain or from the Copyright Holder. A Package modified in such a +way shall still be considered the Standard Version. + +3. You may otherwise modify your copy of this Package in any way, provided +that you insert a prominent notice in each changed file stating how and when +you changed that file, and provided that you do at least ONE of the following: + + a) place your modifications in the Public Domain or otherwise + make them Freely Available, such as by posting said modifications + to Usenet or an equivalent medium, or placing the modifications on + a major archive site such as ftp.uu.net, or by allowing the + Copyright Holder to include your modifications in the Standard + Version of the Package. + + b) use the modified Package only within your corporation or + organization. + + c) rename any non-standard executables so the names do not + conflict with standard executables, which must also be provided, + and provide a separate manual page for each non-standard + executable that clearly documents how it differs from the Standard + Version. + + d) make other distribution arrangements with the Copyright Holder. + +4. You may distribute the programs of this Package in object code or executable +form, provided that you do at least ONE of the following: + + a) distribute a Standard Version of the executables and library + files, together with instructions (in the manual page or equivalent) + on where to get the Standard Version. + + b) accompany the distribution with the machine-readable source of + the Package with your modifications. + + c) accompany any non-standard executables with their + corresponding Standard Version executables, giving the + non-standard executables non-standard names, and clearly + documenting the differences in manual pages (or equivalent), + together with instructions on where to get the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +5. You may charge a reasonable copying fee for any distribution of this Package. +You may charge any fee you choose for support of this Package. You may not +charge a fee for this Package itself. However, you may distribute this Package in +aggregate with other (possibly commercial) programs as part of a larger +(possibly commercial) software distribution provided that you do not advertise +this Package as a product of your own. + +6. The scripts and library files supplied as input to or produced as output from +the programs of this Package do not automatically fall under the copyright of this +Package, but belong to whomever generated them, and may be sold +commercially, and may be aggregated with this Package. + +7. C or perl subroutines supplied by you and linked into this Package shall not +be considered part of this Package. + +8. The name of the Copyright Holder may not be used to endorse or promote +products derived from this software without specific prior written permission. + +9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED +WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR +PURPOSE. + +The End + diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..02ad948 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,78 @@ +# This file was automatically generated by Dist::Zilla::Plugin::Manifest v5.047. +Build.PL +Changes +LICENSE +MANIFEST +MANIFEST.SKIP +META.json +META.yml +Makefile.PL +OLD-Changes.txt +README +dist.ini +inc/Test/Run/Builder.pm +lib/Config/IniFiles.pm +scripts/tag-release.pl +t/00-compile.t +t/00load.t +t/01basic.t +t/02weird.t +t/03comments.t +t/04import.t +t/05hash.t +t/06oo.t +t/07misc.t +t/08group.t +t/09case.t +t/10delta.t +t/11copy-ties-with-array-vals.t +t/12open-empty-file.t +t/13val-in-list-context.t +t/14brackets-within-values.t +t/15store-and-retrieve-here-doc-terminator.t +t/16case-sensitive-default.t +t/17untainted-multiline-values.t +t/18non-contiguous-groups.t +t/19param-found-outside-section.t +t/20allowedcommentchars.t +t/22trailing-comment-lines.t +t/23scalar-ref.t +t/24case-sensitive-exists.t +t/25line-endings.t +t/26scalar-filehandle.t +t/27empty-ini.t +t/28nomultiline.t +t/29end-of-line-comment.t +t/30parameters-with-empty-values.t +t/31comments_with_spaces.t +t/32mswin-outputs-contain-crs.t +t/33update-using-tied-fh-w-shorter-names.t +t/34trailing-comments-double-delimeter.t +t/allowed-comment-chars.ini +t/array.ini +t/author-pod-coverage.t +t/author-pod-syntax.t +t/bad.ini +t/blank.ini +t/brackets-in-values.ini +t/ca.ini +t/case-sensitive-default.ini +t/case-sensitive.ini +t/cmt.ini +t/cpan-changes.t +t/en.ini +t/end-of-line-comment.ini +t/es.ini +t/for-27-empty.ini +t/lib/Config/IniFiles/Debug.pm +t/lib/Config/IniFiles/Slurp.pm +t/lib/Config/IniFiles/TestPaths.pm +t/non-contiguous-groups.ini +t/pod-coverage.t +t/pod.t +t/release-cpan-changes.t +t/release-kwalitee.t +t/style-trailing-space.t +t/test.ini +t/trailing-comments.ini +weaver.ini diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP new file mode 100644 index 0000000..2e58f07 --- /dev/null +++ b/MANIFEST.SKIP @@ -0,0 +1 @@ +~$ diff --git a/META.json b/META.json new file mode 100644 index 0000000..8ddf5cc --- /dev/null +++ b/META.json @@ -0,0 +1,100 @@ +{ + "abstract" : "A module for reading .ini-style configuration files.", + "author" : [ + "Shlomi Fish " + ], + "dynamic_config" : 0, + "generated_by" : "Dist::Zilla version 5.047, CPAN::Meta::Converter version 2.150005", + "keywords" : [ + "config", + "configuration", + "files", + "ini", + "ini", + "files", + "pure-perl", + "tested", + "tie", + "windows" + ], + "license" : [ + "perl_5" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : 2 + }, + "name" : "Config-IniFiles", + "prereqs" : { + "build" : { + "requires" : { + "Module::Build" : "0.28" + } + }, + "configure" : { + "requires" : { + "ExtUtils::MakeMaker" : "0", + "Module::Build" : "0.28" + } + }, + "develop" : { + "requires" : { + "Pod::Coverage::TrustPod" : "0", + "Test::CPAN::Changes" : "0.19", + "Test::Pod" : "1.41", + "Test::Pod::Coverage" : "1.08" + } + }, + "runtime" : { + "requires" : { + "Carp" : "0", + "Fcntl" : "0", + "File::Basename" : "0", + "File::Temp" : "0", + "IO::Scalar" : "0", + "List::Util" : "1.33", + "Symbol" : "0", + "perl" : "5.008", + "strict" : "0", + "vars" : "0", + "warnings" : "0" + } + }, + "test" : { + "requires" : { + "English" : "0", + "Exporter" : "0", + "File::Spec" : "0", + "File::Temp" : "0", + "IO::File" : "0", + "IO::Handle" : "0", + "IPC::Open3" : "0", + "Scalar::Util" : "0", + "Test::More" : "0", + "base" : "0", + "blib" : "1.01", + "lib" : "0" + } + } + }, + "provides" : { + "Config::IniFiles" : { + "file" : "lib/Config/IniFiles.pm", + "version" : "2.94" + } + }, + "release_status" : "stable", + "resources" : { + "bugtracker" : { + "mailto" : "bug-config-inifiles@rt.cpan.org", + "web" : "http://rt.cpan.org/NoAuth/Bugs.html?Dist=Config-IniFiles" + }, + "repository" : { + "type" : "git", + "url" : "ssh://git@github.com:shlomif/perl-Config-IniFiles.git", + "web" : "https://github.com/shlomif/perl-Config-IniFiles" + } + }, + "version" : "2.94" +} + diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..18133d7 --- /dev/null +++ b/META.yml @@ -0,0 +1,59 @@ +--- +abstract: 'A module for reading .ini-style configuration files.' +author: + - 'Shlomi Fish ' +build_requires: + English: '0' + Exporter: '0' + File::Spec: '0' + File::Temp: '0' + IO::File: '0' + IO::Handle: '0' + IPC::Open3: '0' + Module::Build: '0.28' + Scalar::Util: '0' + Test::More: '0' + base: '0' + blib: '1.01' + lib: '0' +configure_requires: + ExtUtils::MakeMaker: '0' + Module::Build: '0.28' +dynamic_config: 0 +generated_by: 'Dist::Zilla version 5.047, CPAN::Meta::Converter version 2.150005' +keywords: + - config + - configuration + - files + - ini + - ini + - files + - pure-perl + - tested + - tie + - windows +license: perl +meta-spec: + url: http://module-build.sourceforge.net/META-spec-v1.4.html + version: '1.4' +name: Config-IniFiles +provides: + Config::IniFiles: + file: lib/Config/IniFiles.pm + version: '2.94' +requires: + Carp: '0' + Fcntl: '0' + File::Basename: '0' + File::Temp: '0' + IO::Scalar: '0' + List::Util: '1.33' + Symbol: '0' + perl: '5.008' + strict: '0' + vars: '0' + warnings: '0' +resources: + bugtracker: http://rt.cpan.org/NoAuth/Bugs.html?Dist=Config-IniFiles + repository: ssh://git@github.com:shlomif/perl-Config-IniFiles.git +version: '2.94' diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..9daf08a --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,91 @@ +# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.047. +use strict; +use warnings; + +use 5.008; + +use ExtUtils::MakeMaker; + +my %WriteMakefileArgs = ( + "ABSTRACT" => "A module for reading .ini-style configuration files.", + "AUTHOR" => "Shlomi Fish ", + "BUILD_REQUIRES" => { + "Module::Build" => "0.28" + }, + "CONFIGURE_REQUIRES" => { + "ExtUtils::MakeMaker" => 0, + "Module::Build" => "0.28" + }, + "DISTNAME" => "Config-IniFiles", + "LICENSE" => "perl", + "MIN_PERL_VERSION" => "5.008", + "NAME" => "Config::IniFiles", + "PREREQ_PM" => { + "Carp" => 0, + "Fcntl" => 0, + "File::Basename" => 0, + "File::Temp" => 0, + "IO::Scalar" => 0, + "List::Util" => "1.33", + "Symbol" => 0, + "strict" => 0, + "vars" => 0, + "warnings" => 0 + }, + "TEST_REQUIRES" => { + "English" => 0, + "Exporter" => 0, + "File::Spec" => 0, + "File::Temp" => 0, + "IO::File" => 0, + "IO::Handle" => 0, + "IPC::Open3" => 0, + "Scalar::Util" => 0, + "Test::More" => 0, + "base" => 0, + "blib" => "1.01", + "lib" => 0 + }, + "VERSION" => "2.94", + "test" => { + "TESTS" => "t/*.t" + } +); + + +my %FallbackPrereqs = ( + "Carp" => 0, + "English" => 0, + "Exporter" => 0, + "Fcntl" => 0, + "File::Basename" => 0, + "File::Spec" => 0, + "File::Temp" => 0, + "IO::File" => 0, + "IO::Handle" => 0, + "IO::Scalar" => 0, + "IPC::Open3" => 0, + "List::Util" => "1.33", + "Module::Build" => "0.28", + "Scalar::Util" => 0, + "Symbol" => 0, + "Test::More" => 0, + "base" => 0, + "blib" => "1.01", + "lib" => 0, + "strict" => 0, + "vars" => 0, + "warnings" => 0 +); + + +unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { + delete $WriteMakefileArgs{TEST_REQUIRES}; + delete $WriteMakefileArgs{BUILD_REQUIRES}; + $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; +} + +delete $WriteMakefileArgs{CONFIGURE_REQUIRES} + unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; + +WriteMakefile(%WriteMakefileArgs); diff --git a/OLD-Changes.txt b/OLD-Changes.txt new file mode 100644 index 0000000..1c9b115 --- /dev/null +++ b/OLD-Changes.txt @@ -0,0 +1,276 @@ +Revision 2.40 2003/12/08 10:33:13 domq +Documentation tidyup + +Revision 2.39 2003/12/06 07:54:21 wadg +[By Proxy for domq] +* Encapsulation of internal data structures even for use within the + class: e.g. ReadConfig() and the TIEHASH interface operate using accessor + methods on $self. Causes massive simplification of the code. + +* TIEHASH interface made load-on-demand. Order of each() in TIEHASH + same as configuration file's order (i.e. $self->Sections() and + $self->Parameters()). + +* push() and exists() methods + +* -allowempty and -deltas parameters to new() + +* support for loading config from a SCALAR reference. Unfortunately + this also requires a small patch to IO::Scalar, whose author has yet + to respond to my email + +* ->{myparms} and ->{mysects} fields, supporting -delta=>1 option to + WriteConfig() + +* _assert_invariants() method (used only in t/01basic.t and + t/02weird.t, maybe should be packaged otherwise) + +Revision 2.39 2003/11/10 15:37:48 dom +* Encapsulation of internal data structures even for use within the + class: e.g. ReadConfig() and the TIEHASH interface operate using accessor + methods on $self. Causes massive simplification of the code. + +* TIEHASH interface made load-on-demand. Order of each() in TIEHASH + same as configuration file's order (i.e. $self->Sections() and + $self->Parameters()). + +* push() and exists() methods + +* -allowempty and -deltas parameters to new() + +* support for loading config from a SCALAR reference. Unfortunately + this also requires a small patch to IO::Scalar, whose author has yet + to respond to my email + +* ->{myparms} and ->{mysects} fields, supporting -delta=>1 option to + WriteConfig() + +* _assert_invariants() method (used only in t/01basic.t and + t/02weird.t, maybe should be packaged otherwise) + + +Revision 2.38 2003/05/14 01:30:32 wadg +- fixed RewriteConfig and ReadConfig to work with open file handles +- added a test to ensure that blank files throw no warnings +- added a test for error messages from malformed lines + +Revision 2.37 2003/01/31 23:00:35 wadg +Updated t/07misc test 4 to remove warning + +Revision 2.36 2002/12/18 01:43:11 wadg +- Improved error message when an invalid line is encountered in INI file +- Fixed bug 649220; importing a non-file-based object into a file one + no longer destroys the original object + +Revision 2.33 2002/11/12 14:48:16 grail +Addresses feature request - [ 403496 ] A simple change will allow support on more platforms + +Revision 2.32 2002/11/12 14:15:44 grail +Addresses bug - [225971] Respect Read-Only Permissions of File System + +Revision 2.31 2002/10/29 01:45:47 grail +[ 540867 ] Add GetFileName method + +Revision 2.30 2002/10/15 18:51:07 wadg +Patched to stopwarnings about utf8 usage. + +Revision 2.29 2002/08/15 21:33:58 wadg +- Support for UTF Byte-Order-Mark (Raviraj Murdeshwar) +- Made tests portable to Mac (p. kent) +- Made file parsing portable for s390/EBCDIC, etc. (Adam Fischler) +- Fixed import bug with Perl 5.8.0 (Marek Rouchal) +- Fixed precedence bug in WriteConfig (Luc St Louis) +- Fixed broken group detection in SetGroupMember and RemoveGroupMember (Kay Röpke) +- Added line continuation character (/) support (Marek Rouchal) +- Added configurable comment character support (Marek Rouchal) + +Revision 2.28 2002/07/04 03:56:05 grail +Changes for resolving bug 447532 - _section::FETCH should return array ref for multiline values. + +Revision 2.27 2001/12/20 16:03:49 wadg +- Fixed bug introduced in new valid file check where ';' comments in first lines were not considered valid +- Rearranged some tests to put them in the proper files (case and -default) +- Added more comment test to cover more cases +- Fixed first two comments tests which weren't doing anything + +Revision 2.26 2001/12/19 22:20:50 wadg +#481513 Recognize badly formatted files + +Revision 2.25 2001/12/12 20:44:48 wadg +Update to bring CVS version in synch + +Revision 2.24 2001/12/07 10:03:06 wadg +222444 Ability to load from arbitrary source + +Revision 2.23 2001/12/07 09:35:06 wadg +Forgot to include updates t/test.ini + +Revision 2.22 2001/12/06 16:52:39 wadg +Fixed bugs 482353,233372. Updated doc for new mgr. + +Revision 2.21 2001/08/14 01:49:06 wadg +Bug fix: multiple blank lines counted as one +Patched README change log to include recent updates + +Revision 2.20 2001/06/07 02:49:52 grail + - Added checks for method parameters being defined + - fixed some regexes to make them stricter + - Fixed greps to make them consistent through the code (also a vain + attempt to help my editors do syntax colouring properly) + - Added AddSection method, replaced chunk of ReadConfig with AddSection + - Added case handling stuff to more methods + - Added RemoveGroupMember + - Made variable names more consistent through OO methods + - Restored Unix EOLs + +Revision 2.19 2001/04/04 23:33:40 wadg +Fixed case sensitivity bug + +Revision 2.18 2001/03/30 04:41:08 rbowen +Small documentation change in IniFiles.pm - pod2* was choking on misplaces +=item tags. And I regenerated the README +The main reason for this release is that the MANIFEST in the 2.17 version was +missing one of the new test suite files, and that is included in this +re-release. + +Revision 2.17 2001/03/21 21:05:12 wadg +Documentation edits + +Revision 2.16 2001/03/21 19:59:09 wadg +410327 -default not in original; 233255 substring parameters + +Revision 2.15 2001/01/30 11:46:48 rbowen +Very minor documentation bug fixed. + +Revision 2.14 2001/01/08 18:02:32 wadg +[Bug #127325] Fixed proken import; changelog; moved + +Revision 2.13 2000/12/18 07:14:41 wadg +[Bugs# 122441,122437] Alien EOLs and OO delete method + +Revision 2.12 2000/12/18 04:59:37 wadg +[Bug #125524] Writing multiline of 2 with tied hash + +Revision 2.11 2000/12/16 12:53:13 grail +[BUG #122455] Problem with File Permissions + +Revision 2.10 2000/12/13 17:40:18 rbowen +Updated version number so that CPAN will stop being angry with us. + +Revision 1.18 2000/12/08 00:45:35 grail +Change as requested by Jeremy Wadsack, for Bug 123146 + +Revision 1.17 2000/12/07 15:32:36 grail +Further patch to duplicate sections bug, and replacement of repeated values handling code. + +Revision 1.14 2000/11/29 11:26:03 grail +Updates for task 22401 (no more reloadsig) and 22402 (Group and GroupMember doco) + +Revision 1.13 2000/11/28 12:41:42 grail +Added test for being able to add sections with wierd names like section|version2 + +Revision 1.11 2000/11/24 21:20:11 rbowen +Resolved SourceForge bug #122445 - a parameter should be split from its value on the first = sign encountered, not on the last one. Added test suite to test this, and put test case in test.ini + +Revision 1.10 2000/11/24 20:40:58 rbowen +Updated MANIFEST to have file list of new files in t/ +Updated IniFiles.pm to have mention of sourceforge addresses, rather than rcbowen.com addresses +Regenerated README from IniFiles.pm + +Revision 1.9 2000/11/23 05:08:08 grail +Fixed documentation for bug 122443 - Check that INI files can be created from scratch. + +Revision 1.1.1.1 2000/11/10 03:04:01 rbowen +Initial checkin of the Config::IniFiles source to SourceForge + +Revision 1.8 2000/10/17 01:52:55 rbowen +Patch from Jeremy. Fixed "defined" warnings. + +Revision 1.7 2000/09/21 11:19:17 rbowen +Mostly documentation changes. I moved the change log into the POD rather +than having it in a separate Changes file. This allows people to see the +changes in the Readme before they download the module. Now I just +need to make sure I remember to regenerate the Readme every time I do +a commit. + + +1.6 September 19, 2000 by JW, AS +* Applied several patches submitted to me by Jeremy and Alex. +* Changed version number to the CVS version number, so that I won't +have to think about changing it ever again. Big version change +should not be taken as a huge leap forward. + +0.12 September 13, 2000 by JW/WADG +* Added documentation to clarify autovivification issues when +creating new sections +* Fixed version number (Oops!) + +0.11 September 13, 2000 by JW/WADG +* Applied patch to Group and GroupMembers functions to return empty +list when no groups are present (submitted by John Bass, Sep 13) + +0.10 September 13, 2000 by JW/WADG +* Fixed reference in POD to ReWriteFile. changes to RewriteConfig +* Applied patch for failed open bug submitted by Mordechai T. Abzug Aug 18 +* Doc'd behavior of failed open +* Removed planned SIG testing from test.pl as SIGs have been removed +* Applied patch from Thibault Deflers to fix bug in parameter list +when a parameter value is undef + +0.09 +Hey! Where's the change log for 0.09? + +0.08 +2000-07-30 Adrian Phillips + +* test.pl: Fixed some tests which use $\, and made those that try +to check a non existant val check against ! defined. + +* IniFiles.pm: hopefully fixed use of $\ when this is unset +(problems found when running tests with -w). Similar problem with +$/ which can be undefined and trying to return a val which does +not exist. Modified val docs section to indicate a undef return +when this occurs. + +0.07 +Looks like we missed a change log for 0.07. Bummer. + +0.06 Sun Jun 25, 2000 by Daniel Winkelmann +* Patch for uninitialized value bug in newval and setval + +0.05 Sun Jun 18, 2000 by RBOW +* Added something to shut up -w on VERSIONS +* Removed unused variables + +0.04 Thu Jun 15 - Fri Jun 16, 2000 by JW/WADG +* Added support for -import option on ->new +* Added support for tying a hash +* Edited POD for grammer, clarity and updates +* Updated test.pl file +* Fixed bug in multiline/single line output +* Fixed bug in default handling with tie interface +* Added bugs to test.pl for regression +* Fixed bug in {group} vs. {groups} property (first is valid) +* Fixed return value for empty {sects} or {parms}{$sect} in +Sections and Parameters methods + +0.03 Thu Jun 15, 2000 by RBOW +* Modifications to permit 'use strict', and to get 'make test' working +again. + +0.02 Tue Jun 13, 2000 by RBOW +* Fixed bug reported by Bernie Cosell - Sections, Parameters, +and GroupMembers return undef if there are no sections, +parameters, or group members. These functions now return +() if the particular value is undefined. +* Added some contributed documentation, from Alex Satrapa, explaining +how the internal data structure works. +* Set up a project on SourceForge. (Not a change, but worth +noting). +* Added Groups method to return a list of section groups. + +0.01 Mon Jun 12, 2000 by RBOW +Some general code cleanup, in preparation for changes to +come. Put up Majordomo mailing list and sent invitation to +various people to join it. + diff --git a/README b/README new file mode 100644 index 0000000..25779d0 --- /dev/null +++ b/README @@ -0,0 +1,35 @@ +Config::IniFiles - A module for reading .ini-style configuration files. + +DESCRIPTION + +Config::IniFiles provides a way to have readable configuration files +outside your Perl script. Configurations can be imported (inherited, +stacked,...), sections can be grouped, and settings can be accessed from +a tied hash. + +INSTALLATION + +To install this module type the following: + + perl Build.PL + ./Build + ./Build test + ./Build install + +Alternatively use the CPAN.pm module: + + # perl -MCPAN -e 'install Config::IniFiles' + +Or the newer CPANPLUS.pm module + + # perl -MCPANPLUS -e 'install Config::IniFiles' + +DEPENDENCIES + +The module's only dependency is a recent-enough version of perl5. + +LICENSE + +This program is free software; you can redistribute it and/or +modify it under the same terms as Perl itself. + diff --git a/dist.ini b/dist.ini new file mode 100644 index 0000000..70bd7c0 --- /dev/null +++ b/dist.ini @@ -0,0 +1,41 @@ +name = Config-IniFiles +author = Shlomi Fish +license = Perl_5 +copyright_holder = RBOW and others +copyright_year = 2000 + +[@Filter] +-bundle = @Basic +-remove = License +-remove = Readme +[AutoPrereqs] +[Keywords] +keyword = config +keyword = configuration +keyword = files +keyword = ini +keyword = ini files +keyword = pure-perl +keyword = tested +keyword = tie +keyword = windows +[MetaProvides::Package] +[MetaResources] +bugtracker.web = http://rt.cpan.org/NoAuth/Bugs.html?Dist=Config-IniFiles +bugtracker.mailto = bug-config-inifiles@rt.cpan.org +repository.url = ssh://git@github.com:shlomif/perl-Config-IniFiles.git +repository.web = https://github.com/shlomif/perl-Config-IniFiles +repository.type = git +[ModuleBuild] +[MetaJSON] +[PodSyntaxTests] +[PodCoverageTests] +[PodWeaver] +[PruneFiles] +match = ^rejects/ +[RewriteVersion] +[Test::Compile] +fake_home = 1 +skip = bump-ver|tag-release|run_agg_tests +[Test::CPAN::Changes] +[Test::Kwalitee::Extra] diff --git a/inc/Test/Run/Builder.pm b/inc/Test/Run/Builder.pm new file mode 100644 index 0000000..5703d01 --- /dev/null +++ b/inc/Test/Run/Builder.pm @@ -0,0 +1,79 @@ +package Test::Run::Builder; + +use strict; +use warnings; + +use Module::Build; + +use vars qw(@ISA); + +@ISA = (qw(Module::Build)); + +sub ACTION_runtest +{ + my ($self) = @_; + my $p = $self->{properties}; + + $self->depends_on('code'); + + local @INC = @INC; + + # Make sure we test the module in blib/ + unshift @INC, (File::Spec->catdir($p->{base_dir}, $self->blib, 'lib'), + File::Spec->catdir($p->{base_dir}, $self->blib, 'arch')); + + $self->do_test_run_tests; +} + +sub ACTION_distruntest { + my ($self) = @_; + + $self->depends_on('distdir'); + + my $start_dir = $self->cwd; + my $dist_dir = $self->dist_dir; + chdir $dist_dir or die "Cannot chdir to $dist_dir: $!"; + # XXX could be different names for scripts + + $self->run_perl_script('Build.PL') # XXX Should this be run w/ --nouse-rcfile + or die "Error executing 'Build.PL' in dist directory: $!"; + $self->run_perl_script('Build') + or die "Error executing 'Build' in dist directory: $!"; + $self->run_perl_script('Build', [], ['runtest']) + or die "Error executing 'Build test' in dist directory"; + chdir $start_dir; +} + +sub do_test_run_tests +{ + my $self = shift; + + require Test::Run::CmdLine::Iface; + + my $test_run = + Test::Run::CmdLine::Iface->new( + { + 'test_files' => [glob("t/*.t")], + } + # 'backend_params' => $self->_get_backend_params(), + ); + + return $test_run->run(); +} + +sub ACTION_tags +{ + my $self = shift; + return + $self->do_system( + "ctags", + qw(-f tags --recurse --totals + --exclude=blib/** --exclude=t/lib/** + --exclude=**/.svn/** --exclude='*~'), + "--exclude=".$self->dist_name()."-*/**", + qw(--languages=Perl --langmap=Perl:+.t) + ); +} + +1; + diff --git a/lib/Config/IniFiles.pm b/lib/Config/IniFiles.pm new file mode 100644 index 0000000..85296e8 --- /dev/null +++ b/lib/Config/IniFiles.pm @@ -0,0 +1,3284 @@ +package Config::IniFiles; + +require 5.008; +use strict; +use warnings; + +our $VERSION = '2.94'; +use Carp; +use Symbol 'gensym','qualify_to_ref'; # For the 'any data type' hack +use Fcntl qw( SEEK_SET SEEK_CUR ); + +use List::Util 1.33 qw(any none); + +use File::Basename qw( dirname ); +use File::Temp qw/ tempfile /; + +@Config::IniFiles::errors = ( ); + +# $Header: /home/shlomi/progs/perl/cpan/Config/IniFiles/config-inifiles-cvsbackup/config-inifiles/IniFiles.pm,v 2.41 2003-12-08 10:50:56 domq Exp $ + + +sub _nocase +{ + my $self = shift; + + if (@_) + { + $self->{nocase} = (shift(@_) ? 1 : 0); + } + + return $self->{nocase}; +} + +sub _is_parm_in_sect +{ + my ($self, $sect, $parm) = @_; + + return any { $_ eq $parm } @{$self->{myparms}{$sect}}; +} + +sub new { + my $class = shift; + my %parms = @_; + + my $errs = 0; + my @groups = ( ); + + my $self = bless { + default => '', + fallback =>undef, + fallback_used => 0, + imported =>undef, + v =>{}, + cf => undef, + firstload => 1, + nomultiline => 0, + handle_trailing_comment => 0, + }, $class; + + if( ref($parms{-import}) && ($parms{-import}->isa('Config::IniFiles')) ) { + $self->{imported}=$parms{-import}; # ReadConfig will load the data + $self->{negativedeltas}=1; + } elsif( defined $parms{-import} ) { + carp "Invalid -import value \"$parms{-import}\" was ignored."; + } # end if + delete $parms{-import}; + + # Copy the original parameters so we + # can use them when we build new sections + %{$self->{startup_settings}} = %parms; + + # Parse options + my($k, $v); + local $_; + $self->_nocase(0); + + # Handle known parameters first in this order, + # because each() could return parameters in any order + if (defined ($v = delete $parms{'-file'})) { + # Should we be pedantic and check that the file exists? + # .. no, because now it could be a handle, IO:: object or something else + $self->{cf} = $v; + } + if (defined ($v = delete $parms{'-nocase'})) { + $self->_nocase($v); + } + if (defined ($v = delete $parms{'-default'})) { + $self->{default} = $self->_nocase ? lc($v) : $v; + } + if (defined ($v = delete $parms{'-fallback'})) { + $self->{fallback} = $self->_nocase ? lc($v) : $v; + } + if (defined ($v = delete $parms{'-reloadwarn'})) { + $self->{reloadwarn} = $v ? 1 : 0; + } + if (defined ($v = delete $parms{'-nomultiline'})) { + $self->{nomultiline} = $v ? 1 : 0; + } + if (defined ($v = delete $parms{'-allowcontinue'})) { + $self->{allowcontinue} = $v ? 1 : 0; + } + if (defined ($v = delete $parms{'-allowempty'})) { + $self->{allowempty} = $v ? 1 : 0; + } + if (defined ($v = delete $parms{'-negativedeltas'})) { + $self->{negativedeltas} = $v ? 1 : 0; + } + if (defined ($v = delete $parms{'-commentchar'})) { + if(!defined $v || length($v) != 1) { + carp "Comment character must be unique."; + $errs++; + } + elsif($v =~ /[\[\]=\w]/) { + # must not be square bracket, equal sign or alphanumeric + carp "Illegal comment character."; + $errs++; + } + else { + $self->{comment_char} = $v; + } + } + if (defined ($v = delete $parms{'-allowedcommentchars'})) { + # must not be square bracket, equal sign or alphanumeric + if(!defined $v || $v =~ /[\[\]=\w]/) { + carp "Illegal value for -allowedcommentchars."; + $errs++; + } + else { + $self->{allowed_comment_char} = $v; + } + } + + if (defined ($v = delete $parms{'-handle_trailing_comment'})) { + $self->{handle_trailing_comment} = $v ? 1 : 0; + } + + $self->{comment_char} = '#' unless exists $self->{comment_char}; + $self->{allowed_comment_char} = ';' unless exists $self->{allowed_comment_char}; + # make sure that comment character is always allowed + $self->{allowed_comment_char} .= $self->{comment_char}; + + $self->{_comments_at_end_of_file} = []; + + # Any other parameters are unknown + while (($k, $v) = each %parms) { + carp "Unknown named parameter $k=>$v"; + $errs++; + } + + return undef if $errs; + + if ($self->ReadConfig) { + return $self; + } else { + return undef; + } +} + + + +sub _caseify { + my ($self, @refs) = @_; + + if (not $self->_nocase) + { + return; + } + + foreach my $ref (@refs) { + ${$ref} = lc(${$ref}) + } + + return; +} + +sub val +{ + my ($self, $sect, $parm, $def) = @_; + + # Always return undef on bad parameters + if (not (defined($sect) && defined($parm))) + { + return; + } + + $self->_caseify(\$sect, \$parm); + + my $val_sect = + defined($self->{v}{$sect}{$parm}) + ? $sect + : $self->{default} + ; + + my $val = $self->{v}{$val_sect}{$parm}; + + # If the value is undef, make it $def instead (which could just be undef) + if (!defined ($val)) + { + $val = $def; + } + + # Return the value in the desired context + if (wantarray) + { + if (ref($val) eq "ARRAY") + { + return @$val; + } + elsif (defined($val)) + { + return $val; + } + else + { + return; + } + } + elsif (ref($val) eq "ARRAY") + { + return join( (defined($/) ? $/ : "\n"), @$val); + } + else + { + return $val; + } +} + + +sub exists { + my ($self, $sect, $parm) = @_; + + $self->_caseify(\$sect, \$parm); + + return (exists $self->{v}{$sect}{$parm}); +} + + + + +sub push { + my ($self, $sect, $parm, @vals) = @_; + + return undef if not defined $sect; + return undef if not defined $parm; + + $self->_caseify(\$sect, \$parm); + + return undef if (! defined($self->{v}{$sect}{$parm})); + + return 1 if (! @vals); + + $self->_touch_parameter($sect, $parm); + + $self->{EOT}{$sect}{$parm} = 'EOT' if + (!defined $self->{EOT}{$sect}{$parm}); + + $self->{v}{$sect}{$parm} = [$self->{v}{$sect}{$parm}] unless + (ref($self->{v}{$sect}{$parm}) eq "ARRAY"); + + CORE::push @{$self->{v}{$sect}{$parm}}, @vals; + return 1; +} + + +sub setval { + my $self = shift; + my $sect = shift; + my $parm = shift; + my @val = @_; + + return undef if not defined $sect; + return undef if not defined $parm; + + $self->_caseify(\$sect, \$parm); + + if (defined($self->{v}{$sect}{$parm})) { + $self->_touch_parameter($sect, $parm); + if (@val > 1) { + $self->{v}{$sect}{$parm} = \@val; + $self->{EOT}{$sect}{$parm} = 'EOT'; + } else { + $self->{v}{$sect}{$parm} = shift @val; + } + return 1; + } else { + return undef; + } +} + + +sub newval { + my $self = shift; + my $sect = shift; + my $parm = shift; + my @val = @_; + + return undef if not defined $sect; + return undef if not defined $parm; + + $self->_caseify(\$sect, \$parm); + + $self->AddSection($sect); + + if (none { $_ eq $parm } @{$self->{parms}{$sect}}) + { + CORE::push(@{$self->{parms}{$sect}}, $parm) + } + + $self->_touch_parameter($sect, $parm); + if (@val > 1) { + $self->{v}{$sect}{$parm} = \@val; + if (!defined $self->{EOT}{$sect}{$parm}) + { + $self->{EOT}{$sect}{$parm} = 'EOT'; + } + } else { + $self->{v}{$sect}{$parm} = shift @val; + } + return 1 +} + + +sub delval { + my $self = shift; + my $sect = shift; + my $parm = shift; + + return undef if not defined $sect; + return undef if not defined $parm; + + $self->_caseify(\$sect, \$parm); + + $self->{parms}{$sect} = [grep {$_ ne $parm} @{$self->{parms}{$sect}}]; + $self->_touch_parameter($sect, $parm); + delete $self->{v}{$sect}{$parm}; + + return 1; +} + + +# Auxiliary function to make deep (aliasing-free) copies of data +# structures. Ignores blessed objects in tree (could be taught not +# to, if needed) +sub _deepcopy { + my $ref = shift; + + if (! ref($ref)) { + return $ref; + } + + if (UNIVERSAL::isa($ref, "ARRAY")) { + return [map {_deepcopy($_)} @$ref]; + } + + if (UNIVERSAL::isa($ref, "HASH")) { + my $return = {}; + foreach my $k (keys %$ref) { + $return->{$k} = _deepcopy($ref->{$k}); + } + return $return; + } + + carp "Unhandled data structure in $ref, cannot _deepcopy()"; +} + +# Internal method, gets the next line, taking proper care of line endings. +sub _nextline { + my ($self, $fh) = @_; + local $_; + if (!exists $self->{line_ends}) { + # no $self->{line_ends} is a hint set by caller that we are at + # the first line (kludge kludge). + { + local $/=\1; + my $nextchar; + do { + $nextchar=<$fh>; + return undef if (!defined $nextchar); + $_ .= $nextchar; + } until (m/((\015|\012|\025|\n)$)/s); + $self->{line_ends}=$1; + if ($nextchar eq "\x0d") { + # peek at the next char + $nextchar = <$fh>; + if ($nextchar eq "\x0a") { + $self->{line_ends} .= "\x0a"; + } else { + seek $fh, -1, SEEK_CUR(); + } + } + } + + # If there's a UTF BOM (Byte-Order-Mark) in the first + # character of the first line then remove it before processing + # ( http://www.unicode.org/unicode/faq/utf_bom.html#22 ) + s/^//; + + return $_; + } else { + local $/=$self->{line_ends}; + return scalar <$fh>; + } +} + +# Internal method, closes or resets the file handle. To be called +# whenever ReadConfig() returns. +sub _rollback { + my ($self, $fh) = @_; + # Only close if this is a filename, if it's + # an open handle, then just roll back to the start + if( !ref($self->{cf}) ) { + close($fh); + } else { + # Attempt to rollback to beginning, no problem if this fails (e.g. STDIN) + seek( $fh, 0, SEEK_SET() ); + } # end if +} + +sub _no_filename +{ + my $self = shift; + + my $fn = $self->{cf}; + + return (not (defined($fn) && length($fn))); +} + +sub _read_line_num +{ + my $self = shift; + + if (@_) + { + $self->{_read_line_num} = shift; + } + + return $self->{_read_line_num}; +} + +# Reads the next line and removes the end of line from it. +sub _read_next_line +{ + my ($self, $fh) = @_; + + my $line = $self->_nextline($fh); + + if (! defined($line)) + { + return undef; + } + + $self->_read_line_num( $self->_read_line_num() + 1); + + # Remove line ending char(s) + $line =~ s/(\015\012?|\012|\025|\n)\z//; + + return $line; +} + +sub _add_error +{ + my ($self, $msg) = @_; + + CORE::push(@Config::IniFiles::errors, $msg); + + return; +} + +# The current section - used for parsing. +sub _curr_sect +{ + my $self = shift; + + if (@_) + { + $self->{_curr_sect} = shift; + } + + return $self->{_curr_sect}; +} + +# The current parameter - used for parsing. +sub _curr_parm +{ + my $self = shift; + + if (@_) + { + $self->{_curr_parm} = shift; + } + + return $self->{_curr_parm}; +} + +# Current location - section and parameter. +sub _curr_loc +{ + my $self = shift; + + return ($self->_curr_sect, $self->_curr_parm); +} + +# The current value - used in parsing. +sub _curr_val +{ + my $self = shift; + + if (@_) + { + $self->{_curr_val} = shift; + } + + return $self->{_curr_val}; +} + +sub _curr_cmts +{ + my $self = shift; + + if (@_) + { + $self->{_curr_cmts} = shift; + } + + return $self->{_curr_cmts}; +} + +sub _curr_end_comment +{ + my $self = shift; + + if (@_) + { + $self->{_curr_end_comment} = shift; + } + + return $self->{_curr_end_comment}; +} + +my $RET_CONTINUE = 1; +my $RET_BREAK; + +sub _ReadConfig_handle_comment +{ + my ($self, $line) = @_; + + if ($self->{negativedeltas} and + my ($to_delete) = $line =~ m/\A$self->{comment_char} (.*) is deleted\z/ + ) + { + if (my ($sect) = $to_delete =~ m/\A\[(.*)\]\z/) + { + $self->DeleteSection($sect); + } + else + { + $self->delval($self->_curr_sect, $to_delete); + } + } + else + { + CORE::push(@{$self->_curr_cmts}, $line); + } + + return $RET_CONTINUE; +} + +sub _ReadConfig_new_section +{ + my ($self, $sect) = @_; + + $self->_caseify(\$sect); + + $self->_curr_sect($sect); + $self->AddSection($self->_curr_sect); + $self->SetSectionComment($self->_curr_sect, @{$self->_curr_cmts}); + $self->_curr_cmts([]); + + return $RET_CONTINUE; +} + +sub _handle_fallback_sect +{ + my ($self) = @_; + + if ((!defined($self->_curr_sect)) and defined($self->{fallback})) + { + $self->_curr_sect($self->{fallback}); + $self->{fallback_used}++; + } + + return; +} + +sub _ReadConfig_load_value +{ + my ($self, $val_aref) = @_; + + # Now load value + if (exists $self->{v}{$self->_curr_sect}{$self->_curr_parm} && + exists $self->{myparms}{$self->_curr_sect} && + $self->_is_parm_in_sect($self->_curr_loc)) + { + $self->push($self->_curr_loc, @$val_aref); + } + else + { + # Loaded parameters shadow imported ones, instead of appending + # to them + $self->newval($self->_curr_loc, @$val_aref); + } + + return; +} + +sub _test_for_fallback_or_no_sect +{ + my ($self, $fh) = @_; + + $self->_handle_fallback_sect; + + if (!defined $self->_curr_sect) { + $self->_add_error( + sprintf('%d: %s', $self->_read_line_num(), + qq#parameter found outside a section# + ) + ); + $self->_rollback($fh); + return $RET_BREAK; + } + + return $RET_CONTINUE; +} + +sub _ReadConfig_handle_here_doc_param +{ + my ($self, $fh, $eotmark, $val_aref) = @_; + + my $foundeot = 0; + my $startline = $self->_read_line_num(); + + HERE_DOC_LOOP: + while (defined( my $line = $self->_read_next_line($fh) )) + { + if ($line eq $eotmark) + { + $foundeot = 1; + last HERE_DOC_LOOP; + } + else + { + # Untaint + my ($contents) = $line =~ /(.*)/ms; + CORE::push(@$val_aref, $contents); + } + } + + if (! $foundeot) + { + $self->_add_error(sprintf('%d: %s', $startline, + qq#no end marker ("$eotmark") found#)); + $self->_rollback(); + return $RET_BREAK; + } + + return $RET_CONTINUE; +} + +sub _ReadConfig_handle_non_here_doc_param +{ + my ($self, $fh, $val_aref) = @_; + + my $allCmt = $self->{allowed_comment_char}; + my $end_commenthandle = $self->{handle_trailing_comment}; + + # process continuation lines, if any + $self->_process_continue_val($fh); + + # we should split value and comments if there is any comment + if ($end_commenthandle and + my ($value_to_assign, $end_comment_to_assign) = $self->_curr_val =~ /(.*?)\s*[$allCmt]\s*(.*)$/) + { + $self->_curr_val($value_to_assign); + $self->_curr_end_comment($end_comment_to_assign); + } + else + { + $self->_curr_end_comment(q{}); + } + + @{$val_aref} = ($self->_curr_val); + + return; +} + + +sub _ReadConfig_populate_values +{ + my ($self, $val_aref, $eotmark) = @_; + + $self->_ReadConfig_load_value($val_aref); + + $self->SetParameterComment($self->_curr_loc, @{ $self->_curr_cmts }); + $self->_curr_cmts([]); + if (defined $eotmark) + { + $self->SetParameterEOT($self->_curr_loc, $eotmark); + } + # if handle_trailing_comment is off, this line makes no sense, since all $end_comment="" + $self->SetParameterTrailingComment($self->_curr_loc, $self->_curr_end_comment); + + return; +} + +sub _ReadConfig_param_assignment +{ + my ($self, $fh, $line, $parm, $value_to_assign) = @_; + + $self->_curr_val($value_to_assign); + $self->_curr_end_comment(undef()); + + if (!defined( $self->_test_for_fallback_or_no_sect($fh) )) + { + return $RET_BREAK; + } + + $self->_caseify(\$parm); + $self->_curr_parm($parm); + + my @val = ( ); + my $eotmark; + + if (($eotmark) = $self->_curr_val =~ /\A<<(.*)$/) + { + if (! defined($self->_ReadConfig_handle_here_doc_param( + $fh, $eotmark, \@val + )) + ) + { + return $RET_BREAK; + } + } + else + { + $self->_ReadConfig_handle_non_here_doc_param( $fh, \@val ); + } + + $self->_ReadConfig_populate_values(\@val, $eotmark); + + return $RET_CONTINUE; +} + +# Return 1 to continue - undef to terminate the loop. +sub _ReadConfig_handle_line +{ + my ($self, $fh, $line) = @_; + + my $allCmt = $self->{allowed_comment_char}; + + # ignore blank lines + if ($line =~ /\A\s*\z/) + { + return $RET_CONTINUE; + } + + # collect comments + if ($line =~/\A\s*[$allCmt]/) + { + return $self->_ReadConfig_handle_comment($line); + } + + # New Section + if (my ($sect) = $line =~ /\A\s*\[\s*(\S|\S.*\S)\s*\]\s*\z/) + { + return $self->_ReadConfig_new_section($sect); + } + + # New parameter + if (my ($parm, $value_to_assign) = $line =~ /^\s*([^=]*?[^=\s])\s*=\s*(.*)$/) + { + return $self->_ReadConfig_param_assignment($fh, $line, $parm, $value_to_assign); + } + + $self->_add_error( + sprintf("Line %d in file %s is malformed:\n\t\%s", + $self->_read_line_num(), $self->GetFileName(), $line + ) + ); + + return $RET_CONTINUE; +} + +sub _ReadConfig_lines_loop +{ + my ($self, $fh) = @_; + + $self->_curr_sect(undef()); + $self->_curr_parm(undef()); + $self->_curr_val(undef()); + $self->_curr_cmts([]); + + while ( defined(my $line = $self->_read_next_line($fh)) ) + { + if (!defined( + scalar( $self->_ReadConfig_handle_line($fh, $line) ) + ) + ) + { + return undef; + } + } + + return 1; +} + +sub ReadConfig +{ + my $self = shift; + + @Config::IniFiles::errors = ( ); + + # Initialize (and clear out) storage hashes + $self->{sects} = []; + $self->{parms} = {}; + $self->{group} = {}; + $self->{v} = {}; + $self->{sCMT} = {}; + $self->{pCMT} = {}; + $self->{EOT} = {}; + $self->{mysects} = []; # A pair of hashes to remember which params are loaded + $self->{myparms} = {}; # or set using the API vs. imported - useful for + $self->{peCMT} = {}; # this will store trailing comments at the end of single-line params + $self->{e} = {}; # If a section already exists + $self->{mye} = {}; # If a section already exists + # import shadowing, see below, and WriteConfig($fn, -delta=>1) + + if( defined $self->{imported} ) { + # Run up the import tree to the top, then reload coming + # back down, maintaining the imported file names and our + # file name. + # This is only needed on a reload though + $self->{imported}->ReadConfig() unless ($self->{firstload}); + + foreach my $field (qw(sects parms group v sCMT pCMT EOT e)) { + $self->{$field} = _deepcopy($self->{imported}->{$field}); + } + } # end if + + if ($self->_no_filename) + { + return 1; + } + + # If this is a reload and we want warnings then send one to the STDERR log + unless( $self->{firstload} || !$self->{reloadwarn} ) { + my ($ss, $mm, $hh, $DD, $MM, $YY) = (localtime(time))[0..5]; + printf STDERR + "PID %d reloading config file %s at %d.%02d.%02d %02d:%02d:%02d\n", + $$, $self->{cf}, $YY+1900, $MM+1, $DD, $hh, $mm, $ss; + } + + # Turn off. Future loads are reloads + $self->{firstload} = 0; + + # Get a filehandle, allowing almost any type of 'file' parameter + my $fh = $self->_make_filehandle( $self->{cf} ); + if (!$fh) { + carp "Failed to open $self->{cf}: $!"; + return undef; + } + + # Get mod time of file so we can retain it (if not from STDIN) + # also check if it's a real file (could have been a filehandle made from a scalar). + if (ref($fh) ne "IO::Scalar" && -e $fh) + { + my @stats = stat $fh; + $self->{file_mode} = sprintf("%04o", $stats[2]) if defined $stats[2]; + } + + + # The first lines of the file must be blank, comments or start with [ + my $first = ''; + + delete $self->{line_ends}; # Marks start of parsing for _nextline() + + $self->_read_line_num(0); + + if (!defined($self->_ReadConfig_lines_loop($fh))) + { + return undef; + } + + # Special case: return undef if file is empty. (suppress this line to + # restore the more intuitive behaviour of accepting empty files) + if (! keys %{$self->{v}} && ! $self->{allowempty}) { + $self->_add_error("Empty file treated as error"); + $self->_rollback($fh); + return undef; + } + + if ( defined (my $defaultsect=$self->{startup_settings}->{-default}) ) + { + $self->AddSection($defaultsect); + } + + $self->_SetEndComments(@{ $self->_curr_cmts }); + + $self->_rollback($fh); + return (@Config::IniFiles::errors ? undef : 1); +} + + + +sub Sections { + my $self = shift; + + return @{_aref_or_empty($self->{sects})}; +} + + +sub SectionExists { + my $self = shift; + my $sect = shift; + + return undef if not defined $sect; + + $self->_caseify(\$sect); + + return ((exists $self->{e}{$sect}) ? 1 : 0); +} + + +sub _AddSection_Helper +{ + my ($self, $sect) = @_; + $self->{e}{$sect} = 1; + CORE::push @{$self->{sects}}, $sect; + $self->_touch_section($sect); + + $self->SetGroupMember($sect); + + # Set up the parameter names and values lists + $self->{parms}{$sect} ||= []; + + if (!defined($self->{v}{$sect})) { + $self->{sCMT}{$sect} = []; + $self->{pCMT}{$sect} = {}; # Comments above parameters + $self->{parms}{$sect} = []; + $self->{v}{$sect} = {}; + } + + return; +} + +sub AddSection { + my ($self, $sect) = @_; + + return undef if not defined $sect; + + $self->_caseify(\$sect); + + if ( $self->SectionExists($sect)) + { + return; + } + + return $self->_AddSection_Helper($sect); +} + +# Marks a section as modified by us (this includes deleted by us). +sub _touch_section { + my ($self, $sect) = @_; + + $self->{mysects} ||= []; + + unless (exists $self->{mye}{$sect}) + { + CORE::push @{$self->{mysects}}, $sect; + $self->{mye}{$sect} = 1; + } + + return; +} + +# Marks a parameter as modified by us (this includes deleted by us). +sub _touch_parameter { + my ($self, $sect, $parm) = @_; + + $self->_touch_section($sect); + return if (!exists $self->{v}{$sect}); + $self->{myparms}{$sect} ||= []; + + if (! $self->_is_parm_in_sect($sect, $parm)) + { + CORE::push @{$self->{myparms}{$sect}}, $parm; + } + + return; +} + + + +sub DeleteSection { + my $self = shift; + my $sect = shift; + + return undef if not defined $sect; + + $self->_caseify(\$sect); + + # This is done the fast way, change if data structure changes!! + delete $self->{v}{$sect}; + delete $self->{sCMT}{$sect}; + delete $self->{pCMT}{$sect}; + delete $self->{EOT}{$sect}; + delete $self->{parms}{$sect}; + delete $self->{myparms}{$sect}; + delete $self->{e}{$sect}; + + $self->{sects} = [grep {$_ ne $sect} @{$self->{sects}}]; + $self->_touch_section($sect); + + $self->RemoveGroupMember($sect); + + return 1; +} # end DeleteSection + + +sub RenameSection { + my $self = shift; + my $old_sect = shift; + my $new_sect = shift; + my $include_groupmembers = shift; + return undef unless $self->CopySection($old_sect,$new_sect,$include_groupmembers); + return $self->DeleteSection($old_sect); + +} # end RenameSection + + +sub CopySection { + my $self = shift; + my $old_sect = shift; + my $new_sect = shift; + my $include_groupmembers = shift; + + if (not defined $old_sect or + not defined $new_sect or + !$self->SectionExists($old_sect) or + $self->SectionExists($new_sect)) { + return undef; + } + + $self->_caseify(\$new_sect); + $self->_AddSection_Helper($new_sect); + + # This is done the fast way, change if data structure changes!! + foreach my $key (qw(v sCMT pCMT EOT parms myparms e)) { + next unless exists $self->{$key}{$old_sect}; + $self->{$key}{$new_sect} = Config::IniFiles::_deepcopy($self->{$key}{$old_sect}); + } + + if($include_groupmembers) { + foreach my $old_groupmember ($self->GroupMembers($old_sect)) { + my $new_groupmember = $old_groupmember; + $new_groupmember =~ s/\A\Q$old_sect\E/$new_sect/; + $self->CopySection($old_groupmember,$new_groupmember); + } + } + + return 1; +} # end CopySection + + +sub _aref_or_empty +{ + my ($aref) = @_; + + return ((defined($aref) and ref($aref) eq 'ARRAY') ? $aref : []); +} + +sub Parameters { + my $self = shift; + my $sect = shift; + + return undef if not defined $sect; + + $self->_caseify(\$sect); + + return @{_aref_or_empty($self->{parms}{$sect})}; +} + + +sub Groups +{ + my $self = shift; + + if (ref($self->{group}) eq 'HASH') + { + return keys %{$self->{group}}; + } + else + { + return (); + } +} + + +sub _group_member_handling_skeleton +{ + my ($self, $sect, $method) = @_; + + return undef if not defined $sect; + + if (! (my ($group) = ($sect =~ /\A(\S+)\s+\S/))) + { + return 1; + } + else + { + return $self->$method($sect, $group); + } +} + +sub _SetGroupMember_helper +{ + my ($self, $sect, $group) = @_; + + if (not exists($self->{group}{$group})) { + $self->{group}{$group} = []; + } + + if (none {$_ eq $sect} @{$self->{group}{$group}}) { + CORE::push @{$self->{group}{$group}}, $sect; + } + + return; +} + +sub SetGroupMember { + my ($self, $sect) = @_; + + return $self->_group_member_handling_skeleton($sect, '_SetGroupMember_helper'); +} + + +sub _RemoveGroupMember_helper +{ + my ($self, $sect, $group) = @_; + + if (!exists $self->{group}{$group}) + { + return; + } + + $self->{group}{$group} = + [grep { $_ ne $sect } @{$self->{group}{$group}}]; + + return; +} + +sub RemoveGroupMember +{ + my ($self, $sect) = @_; + + return $self->_group_member_handling_skeleton($sect, '_RemoveGroupMember_helper'); +} + + +sub GroupMembers { + my ($self, $group) = @_; + + return undef if not defined $group; + + $self->_caseify(\$group); + + return @{_aref_or_empty($self->{group}{$group})}; +} + + +sub SetWriteMode +{ + my ($self, $mode) = @_; + + if (not (defined($mode) && ($mode =~ m/[0-7]{3}/))) + { + return undef; + } + + return ($self->{file_mode} = $mode); +} + + +sub GetWriteMode +{ + my $self = shift; + + return $self->{file_mode}; +} + + +sub _write_config_to_filename +{ + my ($self, $filename, %parms) = @_; + + if (-e $filename) { + if (not (-w $filename)) + { + #carp "File $filename is not writable. Refusing to write config"; + return undef; + } + my $mode = (stat $filename)[2]; + $self->{file_mode} = sprintf "%04o", ($mode & 0777); + #carp "Using mode $self->{file_mode} for file $file"; + } + + my ($fh, $new_file); + + # We need to trap the exception that tempfile() may throw and instead + # carp() and return undef() because that was the previous behaviour: + # + # See RT #77039 ( https://rt.cpan.org/Ticket/Display.html?id=77039 ) + eval { + ($fh, $new_file) = tempfile( + "temp.ini-XXXXXXXXXX", + DIR => dirname($filename) + ); + + # Convert the filehandle to a "text" filehandle suitable for use + # on Windows (and other platforms). + # + # This may break compatibility for ultra-old perls (ones before 5.6.0) + # so I say - Good Riddance! + if ($^O =~ m/\AMSWin/) + { + binmode $fh, ':crlf'; + } + }; + + if ($@) + { + carp( "Unable to write temp config file: $!" ); + return undef; + } + + $self->OutputConfigToFileHandle($fh, $parms{-delta}); + close($fh); + if (!rename( $new_file, $filename )) { + carp "Unable to rename temp config file ($new_file) to ${filename}: $!"; + return undef; + } + if (exists $self->{file_mode}) { + chmod oct($self->{file_mode}), $filename; + } + + return 1; +} + +sub _write_config_with_a_made_fh +{ + my ($self, $fh, %parms) = @_; + + # Only roll back if it's not STDIN (if it is, Carp) + if( $fh == \*STDIN ) + { + carp "Cannot write configuration file to STDIN."; + } + else + { + seek( $fh, 0, SEEK_SET() ); + # Make sure to keep the previous junk out. + # See: + # https://rt.cpan.org/Public/Bug/Display.html?id=103496 + truncate( $fh, 0 ); + $self->OutputConfigToFileHandle($fh, $parms{-delta}); + seek( $fh, 0, SEEK_SET() ); + } # end if + + return 1; +} + +sub _write_config_to_fh +{ + my ($self, $file, %parms) = @_; + + # Get a filehandle, allowing almost any type of 'file' parameter + ## NB: If this were a filename, this would fail because _make_file + ## opens a read-only handle, but we have already checked that case + ## so re-using the logic is ok [JW/WADG] + my $fh = $self->_make_filehandle( $file ); + + if (!$fh) { + carp "Could not find a filehandle for the input stream ($file): $!"; + return undef; + } + + return $self->_write_config_with_a_made_fh($fh, %parms); +} + +sub WriteConfig { + my ($self, $file, %parms) = @_; + + return undef unless defined $file; + + # If we are using a filename, then do mode checks and write to a + # temporary file to avoid a race condition + if( !ref($file) ) + { + return $self->_write_config_to_filename($file, %parms); + } + # Otherwise, reset to the start of the file and write, unless we are using + # STDIN + else + { + return $self->_write_config_to_fh($file, %parms); + } +} + + +sub RewriteConfig { + my $self = shift; + + if ($self->_no_filename) + { + return 1; + } + + return $self->WriteConfig($self->{cf}); +} + + +sub GetFileName +{ + my $self = shift; + + return $self->{cf}; +} + + +sub SetFileName { + my ($self, $new_filename) = @_; + + if ( length($new_filename) > 0 ) { + return ($self->{cf} = $new_filename); + } + else { + return undef; + } +} + + +sub _calc_eot_mark +{ + my ($self, $sect, $parm, $val) = @_; + + my $eotmark = $self->{EOT}{$sect}{$parm} || 'EOT'; + + # Make sure the $eotmark does not occur inside the string. + my @letters = ('A' .. 'Z'); + my $joined_val = join(q{ }, @$val); + while (index($joined_val, $eotmark) >= 0) + { + $eotmark .= $letters[rand(@letters)]; + } + + return $eotmark; +} + +sub _OutputParam { + my ($self, $sect, $parm, $val, $end_comment, $output_cb) = @_; + + my $line_loop = sub { + my ($mapper) = @_; + + foreach my $line (@{$val}[0 .. $#$val-1]) { + $output_cb->($mapper->($line)); + } + $output_cb->( + $mapper->($val->[-1]), + ($end_comment ? (" $self->{comment_char} $end_comment") : ()), + ); + return; + }; + + if (! @$val) { + # An empty variable - see: + # https://rt.cpan.org/Public/Bug/Display.html?id=68554 + $output_cb->("$parm="); + } + elsif ((@$val == 1) or $self->{nomultiline}) { + $line_loop->(sub { my ($line) = @_; return "$parm=$line"; }); + } + else + { + my $eotmark = $self->_calc_eot_mark($sect, $parm, $val); + + $output_cb->("$parm= <<$eotmark"); + $line_loop->(sub { my ($line) = @_; return $line; }); + $output_cb->($eotmark); + } + + return; +} + +sub OutputConfig { + my ($self, $delta) = @_; + + return $self->OutputConfigToFileHandle(select(), $delta); +} + +sub _output_comments +{ + my ($self, $print_line, $comments_aref) = @_; + + if (ref($comments_aref) eq 'ARRAY') { + foreach my $comment (@$comments_aref) { + $print_line->($comment); + } + } + + return; +} + +sub _process_continue_val +{ + my ($self, $fh) = @_; + + if (not $self->{allowcontinue}) + { + return; + } + + my $val = $self->_curr_val; + + while($val =~ s/\\\z//) { + $val .= $self->_read_next_line($fh); + } + + $self->_curr_val($val); + + return; +} + +sub _output_param_total +{ + my ($self, $sect, $parm, $print_line, $split_val, $delta) = @_; + if (!defined $self->{v}{$sect}{$parm}) { + if ($delta) { + $print_line->("$self->{comment_char} $parm is deleted"); + } + else { + warn "Weird unknown parameter $parm" if $^W; + } + return; + } + + $self->_output_comments($print_line, $self->{pCMT}{$sect}{$parm}); + + my $val = $self->{v}{$sect}{$parm}; + my $end_comment = $self->{peCMT}{$sect}{$parm}; + + return if ! defined ($val); # No parameter exists !! + + $self->_OutputParam( + $sect, + $parm, + $split_val->($val), + (defined($end_comment) ? $end_comment : ""), + $print_line, + ); + + return; +} + +sub _output_section { + my ($self, $sect, $print_line, $split_val, $delta, $position) = @_; + + if (!defined $self->{v}{$sect}) { + if ($delta) { + $print_line->("$self->{comment_char} [$sect] is deleted"); + } else { + warn "Weird unknown section $sect" if $^W; + } + return; + } + return if not defined $self->{v}{$sect}; + $print_line->() if ($position > 0); + $self->_output_comments($print_line, $self->{sCMT}{$sect}); + + if (! + ($self->{fallback_used} and $sect eq $self->{fallback}) + ) + { + $print_line->("[$sect]"); + } + return if ref($self->{v}{$sect}) ne 'HASH'; + + foreach my $parm (@{$self->{$delta ? "myparms" : "parms"}{$sect}}) { + $self->_output_param_total( + $sect, $parm, $print_line, $split_val, $delta + ); + } + + return; +} + +sub OutputConfigToFileHandle { + # We need no strict 'refs' to be able to print to $fh if it points + # to a glob filehandle. + no strict 'refs'; + my ($self, $fh, $delta) = @_; + + my $ors = $self->{line_ends} || $\ || "\n"; # $\ is normally unset, but use input by default + my $print_line = sub { + print {$fh} (@_, $ors) + or die "Config-IniFiles cannot print to filehandle (out-of-space?). Aborting!"; + return; + }; + my $split_val = sub { + my ($val) = @_; + + return ((ref($val) eq 'ARRAY') + ? $val + : [split /[$ors]/, $val, -1] + ); + }; + + my $position = 0; + + foreach my $sect (@{$self->{$delta ? "mysects" : "sects"}}) { + $self->_output_section( + $sect, $print_line, $split_val, $delta, $position++ + ); + } + + $self->_output_comments($print_line, [ $self->_GetEndComments() ] ); + + return 1; +} + + +sub SetSectionComment +{ + my ($self, $sect, @comment) = @_; + + if (not (defined($sect) && @comment)) + { + return undef; + } + + $self->_caseify(\$sect); + + $self->_touch_section($sect); + # At this point it's possible to have a comment for a section that + # doesn't exist. This comment will not get written to the INI file. + $self->{sCMT}{$sect} = $self->_markup_comments(\@comment); + + return scalar @comment; +} + + + +# this helper makes sure that each line is preceded with the correct comment +# character +sub _markup_comments +{ + my ($self, $comment_aref) = @_; + + my $allCmt = $self->{allowed_comment_char}; + my $cmtChr = $self->{comment_char}; + + my $is_comment = qr/\A\s*[$allCmt]/; + + # TODO : Maybe create a qr// out of it. + return [map { ($_ =~ $is_comment) ? $_ : "$cmtChr $_" } @$comment_aref]; +} + + + + +sub _return_comment +{ + my ($self, $comment_aref) = @_; + + my $delim = defined($/) ? $/ : "\n"; + + return wantarray() ? @$comment_aref : join($delim, @$comment_aref); +} + +sub GetSectionComment +{ + my ($self, $sect) = @_; + + return undef if not defined $sect; + + $self->_caseify(\$sect); + + if (! exists $self->{sCMT}{$sect}) { + return undef; + } + + return $self->_return_comment( $self->{sCMT}{$sect} ); +} + + +sub DeleteSectionComment +{ + my $self = shift; + my $sect = shift; + + return undef if not defined $sect; + + $self->_caseify(\$sect); + $self->_touch_section($sect); + + delete $self->{sCMT}{$sect}; + + return; +} + + +sub SetParameterComment +{ + my ($self, $sect, $parm, @comment) = @_; + + if (not (defined($sect) && defined($parm) && @comment)) + { + return undef; + } + + $self->_caseify(\$sect, \$parm); + + $self->_touch_parameter($sect, $parm); + + # Note that at this point, it's possible to have a comment for a parameter, + # without that parameter actually existing in the INI file. + $self->{pCMT}{$sect}{$parm} = $self->_markup_comments(\@comment); + + return scalar @comment; +} + +sub _SetEndComments +{ + my $self = shift; + my @comments = @_; + + $self->{_comments_at_end_of_file} = \@comments; + + return 1; +} + +sub _GetEndComments { + my $self = shift; + + return @{$self->{_comments_at_end_of_file}}; +} + + +sub GetParameterComment +{ + my ($self, $sect, $parm) = @_; + + if (not (defined($sect) && defined($parm))) + { + return undef; + } + + $self->_caseify(\$sect, \$parm); + + if (not (exists( $self->{pCMT}{$sect} ) + && exists( $self->{pCMT}{$sect}{$parm} ))) + { + return undef; + } + + return $self->_return_comment( $self->{pCMT}{$sect}{$parm} ); +} + + +sub DeleteParameterComment +{ + my ($self, $sect, $parm) = @_; + + if (not (defined($sect) && defined($parm))) + { + return undef; + } + + $self->_caseify(\$sect, \$parm); + + # If the parameter doesn't exist, our goal has already been achieved + if ( exists( $self->{pCMT}{$sect} ) + && exists( $self->{pCMT}{$sect}{$parm} )) + { + $self->_touch_parameter($sect, $parm); + delete $self->{pCMT}{$sect}{$parm}; + } + + return 1; +} + + +sub GetParameterEOT +{ + my ($self, $sect, $parm) = @_; + + if (not (defined($sect) && defined($parm))) + { + return undef; + } + + $self->_caseify(\$sect, \$parm); + + return $self->{EOT}{$sect}{$parm}; +} + + +sub SetParameterEOT +{ + my ($self, $sect, $parm, $EOT) = @_; + + if (not (defined($sect) && defined($parm) && defined($EOT))) + { + return undef; + } + + $self->_caseify(\$sect, \$parm); + + $self->_touch_parameter($sect, $parm); + + $self->{EOT}{$sect}{$parm} = $EOT; + + return; +} + + +sub DeleteParameterEOT +{ + my ($self, $sect, $parm) = @_; + + if (not (defined($sect) && defined($parm))) + { + return undef; + } + + $self->_caseify(\$sect, \$parm); + + $self->_touch_parameter($sect, $parm); + delete $self->{EOT}{$sect}{$parm}; + + return; +} + + +sub SetParameterTrailingComment +{ + my ($self, $sect, $parm, $cmt) = @_; + + if (not (defined($sect) && defined($parm) && defined($cmt))) + { + return undef; + } + + $self->_caseify(\$sect, \$parm); + + # confirm the parameter exist + return undef if not exists $self->{v}{$sect}{$parm}; + + $self->_touch_parameter($sect, $parm); + $self->{peCMT}{$sect}{$parm} = $cmt; + + return 1; +} + + +sub GetParameterTrailingComment +{ + my ($self, $sect, $parm) = @_; + + if (not (defined($sect) && defined($parm))) + { + return undef; + } + + $self->_caseify(\$sect, \$parm); + + # confirm the parameter exist + return undef if not exists $self->{v}{$sect}{$parm}; + return $self->{peCMT}{$sect}{$parm}; +} + + +sub Delete { + my $self = shift; + + foreach my $section ($self->Sections()) { + $self->DeleteSection($section); + } + + return 1; +} # end Delete + + + + +############################################################ +# +# TIEHASH Methods +# +# Description: +# These methods allow you to tie a hash to the +# Config::IniFiles object. Note that, when tied, the +# user wants to look at thinks like $ini{sec}{parm}, but the +# TIEHASH only provides one level of hash interface, so the +# root object gets asked for a $ini{sec}, which this +# implements. To further tie the {parm} hash, the internal +# class Config::IniFiles::_section, is provided, below. +# +############################################################ +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000May09 Created method JW +# ---------------------------------------------------------- +sub TIEHASH { + my $class = shift; + my %parms = @_; + + # Get a new object + my $self = $class->new( %parms ); + + return $self; +} # end TIEHASH + + +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000May09 Created method JW +# ---------------------------------------------------------- +sub FETCH { + my $self = shift; + my( $key ) = @_; + + $self->_caseify(\$key); + return if (! $self->{v}{$key}); + + my %retval; + tie %retval, 'Config::IniFiles::_section', $self, $key; + return \%retval; + +} # end FETCH + +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000Jun14 Fixed bug where wrong ref was saved JW +# 2000Oct09 Fixed possible but in %parms with defaults JW +# 2001Apr04 Fixed -nocase problem in storing JW +# ---------------------------------------------------------- +sub STORE { + my $self = shift; + my( $key, $ref ) = @_; + + return undef unless ref($ref) eq 'HASH'; + + $self->_caseify(\$key); + + $self->AddSection($key); + $self->{v}{$key} = {%$ref}; + $self->{parms}{$key} = [keys %$ref]; + $self->{myparms}{$key} = [keys %$ref]; + + return 1; +} # end STORE + + +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000May09 Created method JW +# 2000Dec17 Now removes comments, groups and EOTs too JW +# 2001Arp04 Fixed -nocase problem JW +# ---------------------------------------------------------- +sub DELETE { + my $self = shift; + my( $key ) = @_; + + my $retval=$self->FETCH($key); + $self->DeleteSection($key); + return $retval; +} # end DELETE + + +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000May09 Created method JW +# ---------------------------------------------------------- +sub CLEAR { + my $self = shift; + + return $self->Delete(); +} # end CLEAR + +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000May09 Created method JW +# ---------------------------------------------------------- +sub FIRSTKEY { + my $self = shift; + + $self->{tied_enumerator}=0; + return $self->NEXTKEY(); +} # end FIRSTKEY + + +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000May09 Created method JW +# ---------------------------------------------------------- +sub NEXTKEY { + my $self = shift; + my( $last ) = @_; + + my $i=$self->{tied_enumerator}++; + my $key=$self->{sects}[$i]; + return if (! defined $key); + return wantarray ? ($key, $self->FETCH($key)) : $key; +} # end NEXTKEY + + +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000May09 Created method JW +# 2001Apr04 Fixed -nocase bug and false true bug JW +# ---------------------------------------------------------- +sub EXISTS { + my $self = shift; + my( $key ) = @_; + return $self->SectionExists($key); +} # end EXISTS + + +# ---------------------------------------------------------- +# DESTROY is used by TIEHASH and the Perl garbage collector, +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000May09 Created method JW +# ---------------------------------------------------------- +sub DESTROY { + # my $self = shift; +} # end if + + +# ---------------------------------------------------------- +# Sub: _make_filehandle +# +# Args: $thing +# $thing An input source +# +# Description: Takes an input source - a filehandle, +# filehandle glob, reference to a filehandle glob, IO::File +# object or scalar filename - and returns a file handle to +# read from it with. +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 06Dec2001 Added to support input from any source JW +# ---------------------------------------------------------- +sub _make_filehandle { + my $self = shift; + + # + # This code is 'borrowed' from Lincoln D. Stein's GD.pm module + # with modification for this module. Thanks Lincoln! + # + + no strict 'refs'; + my $thing = shift; + + if (ref($thing) eq "SCALAR") { + if (eval { require IO::Scalar; $IO::Scalar::VERSION >= 2.109; }) { + return IO::Scalar->new($thing); + } else { + warn "SCALAR reference as file descriptor requires IO::stringy ". + "v2.109 or later" if ($^W); + return; + } + } + + return $thing if defined(fileno $thing); + + # otherwise try qualifying it into caller's package + my $fh = qualify_to_ref($thing,caller(1)); + return $fh if defined(fileno $fh); + + # otherwise treat it as a file to open + $fh = gensym; + open($fh,$thing) || return; + + return $fh; +} # end _make_filehandle + +############################################################ +# +# INTERNAL PACKAGE: Config::IniFiles::_section +# +# Description: +# This package is used to provide a single-level TIEHASH +# interface to the sections in the IniFile. When tied, the +# user wants to look at thinks like $ini{sec}{parm}, but the +# TIEHASH only provides one level of hash interface, so the +# root object gets asked for a $ini{sec} and must return a +# has reference that accurately covers the '{parm}' part. +# +# This package is only used when tied and is inter-woven +# between the sections and their parameters when the TIEHASH +# method is called by Perl. It's a very simple implementation +# of a tied hash object that simply maps onto the object API. +# +############################################################ +# Date Modification Author +# ---------------------------------------------------------- +# 2000.May.09 Created to excapsulate TIEHASH interface JW +############################################################ +package Config::IniFiles::_section; + +use strict; +use warnings; +use Carp; +use vars qw( $VERSION ); + +$Config::IniFiles::_section::VERSION = 2.16; + +# ---------------------------------------------------------- +# Sub: Config::IniFiles::_section::TIEHASH +# +# Args: $class, $config, $section +# $class The class that this is being tied to. +# $config The parent Config::IniFiles object +# $section The section this tied object refers to +# +# Description: Builds the object that implements accesses to +# the tied hash. +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# ---------------------------------------------------------- +sub TIEHASH { + my $proto = shift; + my $class = ref($proto) || $proto; + my ($config, $section) = @_; + + # Make a new object + return bless {config=>$config, section=>$section}, $class; +} # end TIEHASH + + +# ---------------------------------------------------------- +# Sub: Config::IniFiles::_section::FETCH +# +# Args: $key +# $key The name of the key whose value to get +# +# Description: Returns the value associated with $key. If +# the value is a list, returns a list reference. +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2000Jun15 Fixed bugs in -default handler JW +# 2000Dec07 Fixed another bug in -deault handler JW +# 2002Jul04 Returning scalar values (Bug:447532) AS +# ---------------------------------------------------------- +sub FETCH { + my ($self, $key) = @_; + my @retval=$self->{config}->val($self->{section}, $key); + return (@retval <= 1) ? $retval[0] : \@retval; +} # end FETCH + + +# ---------------------------------------------------------- +# Sub: Config::IniFiles::_section::STORE +# +# Args: $key, @val +# $key The key under which to store the value +# @val The value to store, either an array or a scalar +# +# Description: Sets the value for the specified $key +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2001Apr04 Fixed -nocase bug JW +# ---------------------------------------------------------- +sub STORE { + my ($self, $key, @val) = @_; + return $self->{config}->newval($self->{section}, $key, @val); +} # end STORE + + +# ---------------------------------------------------------- +# Sub: Config::IniFiles::_section::DELETE +# +# Args: $key +# $key The key to remove from the hash +# +# Description: Removes the specified key from the hash and +# returns its former value. +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2001Apr04 Fixed -nocase bug JW +# ---------------------------------------------------------- +sub DELETE { + my ($self, $key) = @_; + my $retval=$self->{config}->val($self->{section}, $key); + $self->{config}->delval($self->{section}, $key); + return $retval; +} # end DELETE + +# ---------------------------------------------------------- +# Sub: Config::IniFiles::_section::CLEAR +# +# Args: (None) +# +# Description: Empties the entire hash +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# ---------------------------------------------------------- +sub CLEAR { + my ($self) = @_; + return $self->{config}->DeleteSection($self->{section}); +} # end CLEAR + +# ---------------------------------------------------------- +# Sub: Config::IniFiles::_section::EXISTS +# +# Args: $key +# $key The key to look for +# +# Description: Returns whether the key exists +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# 2001Apr04 Fixed -nocase bug JW +# ---------------------------------------------------------- +sub EXISTS { + my ($self, $key) = @_; + return $self->{config}->exists($self->{section},$key); +} # end EXISTS + +# ---------------------------------------------------------- +# Sub: Config::IniFiles::_section::FIRSTKEY +# +# Args: (None) +# +# Description: Returns the first key in the hash +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# ---------------------------------------------------------- +sub FIRSTKEY { + my $self = shift; + + $self->{tied_enumerator}=0; + return $self->NEXTKEY(); +} # end FIRSTKEY + +# ---------------------------------------------------------- +# Sub: Config::IniFiles::_section::NEXTKEY +# +# Args: $last +# $last The last key accessed by the iterator +# +# Description: Returns the next key in line +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# ---------------------------------------------------------- +sub NEXTKEY { + my $self = shift; + my( $last ) = @_; + + my $i=$self->{tied_enumerator}++; + my @keys = $self->{config}->Parameters($self->{section}); + my $key=$keys[$i]; + return if (! defined $key); + return wantarray ? ($key, $self->FETCH($key)) : $key; +} # end NEXTKEY + + +# ---------------------------------------------------------- +# Sub: Config::IniFiles::_section::DESTROY +# +# Args: (None) +# +# Description: Called on cleanup +# ---------------------------------------------------------- +# Date Modification Author +# ---------------------------------------------------------- +# ---------------------------------------------------------- +sub DESTROY { + # my $self = shift +} # end DESTROY + +1; + + + + +1; + +# Please keep the following within the last four lines of the file +#[JW for editor]:mode=perl:tabSize=8:indentSize=2:noTabs=true:indentOnEnter=true: + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Config::IniFiles - A module for reading .ini-style configuration files. + +=head1 VERSION + +version 2.94 + +=head1 SYNOPSIS + + use Config::IniFiles; + my $cfg = Config::IniFiles->new( -file => "/path/configfile.ini" ); + print "The value is " . $cfg->val( 'Section', 'Parameter' ) . "." + if $cfg->val( 'Section', 'Parameter' ); + +=head1 DESCRIPTION + +Config::IniFiles provides a way to have readable configuration files outside +your Perl script. Configurations can be imported (inherited, stacked,...), +sections can be grouped, and settings can be accessed from a tied hash. + +=head1 FILE FORMAT + +INI files consist of a number of sections, each preceded with the +section name in square brackets, followed by parameter names and +their values. + + [a section] + Parameter=Value + + [section 2] + AnotherParameter=Some value + Setting=Something else + Parameter=Different scope than the one in the first section + +The first non-blank character of the line indicating a section must +be a left bracket and the last non-blank character of a line indicating +a section must be a right bracket. The characters making up the section +name can be any symbols at all. However section names must be unique. + +Parameters are specified in each section as Name=Value. Any spaces +around the equals sign will be ignored, and the value extends to the +end of the line (including any whitespace at the end of the line. +Parameter names are localized to the namespace of the section, but must +be unique within a section. + +Both the hash mark (#) and the semicolon (;) are comment characters. +by default (this can be changed by configuration). Lines that begin with +either of these characters will be ignored. Any amount of whitespace may +precede the comment character. + +Multi-line or multi-valued parameters may also be defined ala UNIX +"here document" syntax: + + Parameter=< method: + + $cfg = Config::IniFiles->new( -file => "/path/config_file.ini" ); + $cfg = new Config::IniFiles -file => "/path/config_file.ini"; + +Optional named parameters may be specified after the configuration +file name. See the I in the B section, below. + +Values from the config file are fetched with the val method: + + $value = $cfg->val('Section', 'Parameter'); + +If you want a multi-line/value field returned as an array, just +specify an array as the receiver: + + @values = $cfg->val('Section', 'Parameter'); + +=head1 METHODS + +=head2 new ( [-option=>value ...] ) + +Returns a new configuration object (or "undef" if the configuration +file has an error, in which case check the global C<@Config::IniFiles::errors> +array for reasons why). One Config::IniFiles object is required per configuration +file. The following named parameters are available: + +=over 10 + +=item I<-file> filename + +Specifies a file to load the parameters from. This 'file' may actually be +any of the following things: + + 1) the pathname of a file + + $cfg = Config::IniFiles->new( -file => "/path/to/config_file.ini" ); + + 2) a simple filehandle + + $cfg = Config::IniFiles->new( -file => STDIN ); + + 3) a filehandle glob + + open( CONFIG, "/path/to/config_file.ini" ); + $cfg = Config::IniFiles->new( -file => *CONFIG ); + + 4) a reference to a glob + + open( CONFIG, "/path/to/config_file.ini" ); + $cfg = Config::IniFiles->new( -file => \*CONFIG ); + + 5) an IO::File object + + $io = IO::File->new( "/path/to/config_file.ini" ); + $cfg = Config::IniFiles->new( -file => $io ); + + or + + open my $fh, '<', "/path/to/config_file.ini" or die $!; + $cfg = Config::IniFiles->new( -file => $fh ); + + 6) A reference to a scalar (requires newer versions of IO::Scalar) + + $ini_file_contents = <new( -file => \$ini_file_contents ); + +If this option is not specified, (i.e. you are creating a config file from scratch) +you must specify a target file using L in order to save the parameters. + +=item I<-default> section + +Specifies a section to be used for default values. For example, in the +following configuration file, if you look up the "permissions" parameter +in the "joe" section, there is none. + + [all] + permissions=Nothing + + [jane] + name=Jane + permissions=Open files + + [joe] + name=Joseph + +If you create your Config::IniFiles object with a default section of "all" like this: + + $cfg = Config::IniFiles->new( -file => "file.ini", -default => "all" ); + +Then requesting a value for a "permissions" in the [joe] section will +check for a value from [all] before returning undef. + + $permissions = $cfg->val( "joe", "permissions"); // returns "Nothing" + +=item I<-fallback> section + +Specifies a section to be used for parameters outside a section. Default is none. +Without -fallback specified (which is the default), reading a configuration file +which has a parameter outside a section will fail. With this set to, say, +"GENERAL", this configuration: + + wrong=wronger + + [joe] + name=Joseph + +will be assumed as: + + [GENERAL] + wrong=wronger + + [joe] + name=Joseph + +Note that Config::IniFiles will also omit the fallback section header when +outputting such configuration. + +=item I<-nocase> 0|1 + +Set -nocase => 1 to handle the config file in a case-insensitive +manner (case in values is preserved, however). By default, config +files are case-sensitive (i.e., a section named 'Test' is not the same +as a section named 'test'). Note that there is an added overhead for +turning off case sensitivity. + +=item I<-import> object + +This allows you to import or inherit existing setting from another +Config::IniFiles object. When importing settings from another object, +sections with the same name will be merged and parameters that are +defined in both the imported object and the I<-file> will take the +value of given in the I<-file>. + +If a I<-default> section is also given on this call, and it does not +coincide with the default of the imported object, the new default +section will be used instead. If no I<-default> section is given, +then the default of the imported object will be used. + +=item I<-allowcontinue> 0|1 + +Set -allowcontinue => 1 to enable continuation lines in the config file. +i.e. if a line ends with a backslash C<\>, then the following line is +appended to the parameter value, dropping the backslash and the newline +character(s). + +Default behavior is to keep a trailing backslash C<\> as a parameter +value. Note that continuation cannot be mixed with the "here" value +syntax. + +=item I<-allowempty> 0|1 + +If set to 1, then empty files are allowed at L +time. If set to 0 (the default), an empty configuration file is considered +an error. + +=item I<-negativedeltas> 0|1 + +If set to 1 (the default if importing this object from another one), +parses and honors lines of the following form in the configuration +file: + + ; [somesection] is deleted + +or + + [inthissection] + ; thisparameter is deleted + +If set to 0 (the default if not importing), these comments are treated +like ordinary ones. + +The L1)> form will output such +comments to indicate deleted sections or parameters. This way, +reloading a delta file using the same imported object produces the +same results in memory again. See L for more +details. + +=item I<-commentchar> 'char' + +The default comment character is C<#>. You may change this by specifying +this option to another character. This can be any character except +alphanumeric characters, square brackets or the "equal" sign. + +=item I<-allowedcommentchars> 'chars' + +Allowed default comment characters are C<#> and C<;>. By specifying this +option you may change the range of characters that are used to denote a +comment line to include any set of characters + +Note: that the character specified by B<-commentchar> (see above) is +I part of the allowed comment characters. + +Note 2: The given string is evaluated as a regular expression character +class, so '\' must be escaped if you wish to use it. + +=item I<-reloadwarn> 0|1 + +Set -reloadwarn => 1 to enable a warning message (output to STDERR) +whenever the config file is reloaded. The reload message is of the +form: + + PID reloading config file at YYYY.MM.DD HH:MM:SS + +Default behavior is to not warn (i.e. -reloadwarn => 0). + +This is generally only useful when using Config::IniFiles in a server +or daemon application. The application is still responsible for determining +when the object is to be reloaded. + +=item I<-nomultiline> 0|1 + +Set -nomultiline => 1 to output multi-valued parameter as: + + param=value1 + param=value2 + +instead of the default: + + param=< 0|1 + +Set -handle_trailing_comment => 1 to enable support of parameter trailing +comments. + +For example, if we have a parameter line like this: + + param1=value1;comment1 + +by default, handle_trailing_comment will be set to B<0>, and we will get +I as the value of I. If we have +-handle_trailing_comment set to B<1>, then we will get I +as the value for I, and I as the trailing comment of +I. + +Set and get methods for trailing comments are provided as +L and L. + +=back + +=head2 val ($section, $parameter [, $default] ) + +Returns the value of the specified parameter (C<$parameter>) in section +C<$section>, returns undef (or C<$default> if specified) if no section or +no parameter for the given section exists. + +If you want a multi-line/value field returned as an array, just +specify an array as the receiver: + + @values = $cfg->val('Section', 'Parameter'); + +A multi-line/value field that is returned in a scalar context will be +joined using $/ (input record separator, default is \n) if defined, +otherwise the values will be joined using \n. + +=head2 exists($section, $parameter) + +True if and only if there exists a section C<$section>, with +a parameter C<$parameter> inside, not counting default values. + +=head2 push ($section, $parameter, $value, [ $value2, ...]) + +Pushes new values at the end of existing value(s) of parameter +C<$parameter> in section C<$section>. See below for methods to write +the new configuration back out to a file. + +You may not set a parameter that didn't exist in the original +configuration file. B will return I if this is +attempted. See B below to do this. Otherwise, it returns 1. + +=head2 setval ($section, $parameter, $value, [ $value2, ... ]) + +Sets the value of parameter C<$parameter> in section C<$section> to +C<$value> (or to a set of values). See below for methods to write +the new configuration back out to a file. + +You may not set a parameter that didn't exist in the original +configuration file. B will return I if this is +attempted. See B below to do this. Otherwise, it returns 1. + +=head2 newval($section, $parameter, $value [, $value2, ...]) + +Assigns a new value, C<$value> (or set of values) to the +parameter C<$parameter> in section C<$section> in the configuration +file. + +=head2 delval($section, $parameter) + +Deletes the specified parameter from the configuration file + +=head2 ReadConfig + +Forces the configuration file to be re-read. Returns undef if the +file can not be opened, no filename was defined (with the C<-file> +option) when the object was constructed, or an error occurred while +reading. + +If an error occurs while parsing the INI file the @Config::IniFiles::errors +array will contain messages that might help you figure out where the +problem is in the file. + +=head2 Sections + +Returns an array containing section names in the configuration file. +If the I option was turned on when the config object was +created, the section names will be returned in lowercase. + +=head2 SectionExists ( $sect_name ) + +Returns 1 if the specified section exists in the INI file, 0 otherwise (undefined if section_name is not defined). + +=head2 AddSection ( $sect_name ) + +Ensures that the named section exists in the INI file. If the section already +exists, nothing is done. In this case, the "new" section will possibly contain +data already. + +If you really need to have a new section with no parameters in it, check that +the name that you're adding isn't in the list of sections already. + +=head2 DeleteSection ( $sect_name ) + +Completely removes the entire section from the configuration. + +=head2 RenameSection ( $old_section_name, $new_section_name, $include_groupmembers) + +Renames a section if it does not already exist, optionally including groupmembers + +=head2 CopySection ( $old_section_name, $new_section_name, $include_groupmembers) + +Copies one section to another optionally including groupmembers + +=head2 Parameters ($sect_name) + +Returns an array containing the parameters contained in the specified +section. + +=head2 Groups + +Returns an array containing the names of available groups. + +Groups are specified in the config file as new sections of the form + + [GroupName MemberName] + +This is useful for building up lists. Note that parameters within a +"member" section are referenced normally (i.e., the section name is +still "Groupname Membername", including the space) - the concept of +Groups is to aid people building more complex configuration files. + +=head2 SetGroupMember ( $sect ) + +Makes sure that the specified section is a member of the appropriate group. + +Only intended for use in newval. + +=head2 RemoveGroupMember ( $sect ) + +Makes sure that the specified section is no longer a member of the +appropriate group. Only intended for use in DeleteSection. + +=head2 GroupMembers ($group) + +Returns an array containing the members of specified $group. Each element +of the array is a section name. For example, given the sections + + [Group Element 1] + ... + + [Group Element 2] + ... + +GroupMembers would return ("Group Element 1", "Group Element 2"). + +=head2 SetWriteMode ($mode) + +Sets the mode (permissions) to use when writing the INI file. + +$mode must be a string representation of the octal mode. + +=head2 GetWriteMode ($mode) + +Gets the current mode (permissions) to use when writing the INI file. + +$mode is a string representation of the octal mode. + +=head2 WriteConfig ($filename [, %options]) + +Writes out a new copy of the configuration file. A temporary file +is written out and then renamed to the specified filename. Also see +B below. + +If C<-delta> is set to a true value in %options, and this object was +imported from another (see L), only the differences between this +object and the imported one will be recorded. Negative deltas will be +encoded into comments, so that a subsequent invocation of I +with the same imported object produces the same results (see the +I<-negativedeltas> option in L). + +C<%options> is not required. + +Returns true on success, C on failure. + +=head2 RewriteConfig + +Same as WriteConfig, but specifies that the original configuration +file should be rewritten. + +=head2 GetFileName + +Returns the filename associated with this INI file. + +If no filename has been specified, returns undef. + +=head2 SetFileName ($filename) + +If you created the Config::IniFiles object without initialising from +a file, or if you just want to change the name of the file to use for +ReadConfig/RewriteConfig from now on, use this method. + +Returns $filename if that was a valid name, undef otherwise. + +=head2 $ini->OutputConfigToFileHandle($fh, $delta) + +Writes OutputConfig to the $fh filehandle. $delta should be set to 1 +1 if writing only delta. This is a newer and safer version of +C and one is encouraged to use it instead. + +=head2 $ini->OutputConfig($delta) + +Writes OutputConfig to STDOUT. Use select() to redirect STDOUT to +the output target before calling this function. Optional argument +should be set to 1 if writing only a delta. Also see OutputConfigToFileHandle + +=head2 SetSectionComment($section, @comment) + +Sets the comment for section $section to the lines contained in @comment. + +Each comment line will be prepended with the comment character (default +is C<#>) if it doesn't already have a comment character (ie: if the +line does not start with whitespace followed by an allowed comment +character, default is C<#> and C<;>). + +To clear a section comment, use DeleteSectionComment ($section) + +=head2 GetSectionComment ($section) + +Returns a list of lines, being the comment attached to section $section. In +scalar context, returns a string containing the lines of the comment separated +by newlines. + +The lines are presented as-is, with whatever comment character was originally +used on that line. + +=head2 DeleteSectionComment ($section) + +Removes the comment for the specified section. + +=head2 SetParameterComment ($section, $parameter, @comment) + +Sets the comment attached to a particular parameter. + +Any line of @comment that does not have a comment character will be +prepended with one. See L above + +=head2 GetParameterComment ($section, $parameter) + +Gets the comment attached to a parameter. In list context returns all +comments - in scalar context returns them joined by newlines. + +=head2 DeleteParameterComment ($section, $parmeter) + +Deletes the comment attached to a parameter. + +=head2 GetParameterEOT ($section, $parameter) + +Accessor method for the EOT text (in fact, style) of the specified parameter. If any text is used as an EOT mark, this will be returned. If the parameter was not recorded using HERE style multiple lines, GetParameterEOT returns undef. + +=head2 $cfg->SetParameterEOT ($section, $parameter, $EOT) + +Accessor method for the EOT text for the specified parameter. Sets the HERE style marker text to the value $EOT. Once the EOT text is set, that parameter will be saved in HERE style. + +To un-set the EOT text, use DeleteParameterEOT ($section, $parameter). + +=head2 DeleteParameterEOT ($section, $parmeter) + +Removes the EOT marker for the given section and parameter. +When writing a configuration file, if no EOT marker is defined +then "EOT" is used. + +=head2 SetParameterTrailingComment ($section, $parameter, $cmt) + +Set the end trailing comment for the given section and parameter. +If there is a old comment for the parameter, it will be +overwritten by the new one. + +If there is a new parameter trailing comment to be added, the +value should be added first. + +=head2 GetParameterTrailingComment ($section, $parameter) + +An accessor method to read the trailing comment after the parameter. +The trailing comment will be returned if there is one. A null string +will be returned if the parameter exists but there is no comment for it. +otherwise, L will be returned. + +=head2 Delete + +Deletes the entire configuration file in memory. + +=head1 USAGE -- Tied Hash + +=head2 tie %ini, 'Config::IniFiles', (-file=>$filename, [-option=>value ...] ) + +Using C, you can tie a hash to a B object. This creates a new +object which you can access through your hash, so you use this instead of the +B method. This actually creates a hash of hashes to access the values in +the INI file. The options you provide through C are the same as given for +the B method, above. + +Here's an example: + + use Config::IniFiles; + + my %ini; + tie %ini, 'Config::IniFiles', ( -file => "/path/configfile.ini" ); + + print "We have $ini{Section}{Parameter}." if $ini{Section}{Parameter}; + +Accessing and using the hash works just like accessing a regular hash and +many of the object methods are made available through the hash interface. + +For those methods that do not coincide with the hash paradigm, you can use +the Perl C function to get at the underlying object tied to the hash +and call methods on that object. For example, to write the hash out to a new +ini file, you would do something like this: + + tied( %ini )->WriteConfig( "/newpath/newconfig.ini" ) || + die "Could not write settings to new file."; + +=head2 $val = $ini{$section}{$parameter} + +Returns the value of $parameter in $section. + +Multiline values accessed through a hash will be returned +as a list in list context and a concatenated value in scalar +context. + +=head2 $ini{$section}{$parameter} = $value; + +Sets the value of C<$parameter> in C<$section> to C<$value>. + +To set a multiline or multi-value parameter just assign an +array reference to the hash entry, like this: + + $ini{$section}{$parameter} = [$value1, $value2, ...]; + +If the parameter did not exist in the original file, it will +be created. However, Perl does not seem to extend autovivification +to tied hashes. That means that if you try to say + + $ini{new_section}{new_paramters} = $val; + +and the section 'new_section' does not exist, then Perl won't +properly create it. In order to work around this you will need +to create a hash reference in that section and then assign the +parameter value. Something like this should do nicely: + + $ini{new_section} = {}; + $ini{new_section}{new_paramters} = $val; + +=head2 %hash = %{$ini{$section}} + +Using the tie interface, you can copy whole sections of the +ini file into another hash. Note that this makes a copy of +the entire section. The new hash in no longer tied to the +ini file, In particular, this means -default and -nocase +settings will not apply to C<%hash>. + +=head2 $ini{$section} = {}; %{$ini{$section}} = %parameters; + +Through the hash interface, you have the ability to replace +the entire section with a new set of parameters. This call +will fail, however, if the argument passed in NOT a hash +reference. You must use both lines, as shown above so that +Perl recognizes the section as a hash reference context +before COPYing over the values from your C<%parameters> hash. + +=head2 delete $ini{$section}{$parameter} + +When tied to a hash, you can use the Perl C function +to completely remove a parameter from a section. + +=head2 delete $ini{$section} + +The tied interface also allows you to delete an entire +section from the ini file using the Perl C function. + +=head2 %ini = (); + +If you really want to delete B the items in the ini file, this +will do it. Of course, the changes won't be written to the actual +file unless you call B on the object tied to the hash. + +=head2 Parameter names + +=over 4 + +=item my @keys = keys %{$ini{$section}} + +=item while (($k, $v) = each %{$ini{$section}}) {...} + +=item if( exists %{$ini{$section}}, $parameter ) {...} + +=back + +When tied to a hash, you use the Perl C and C +functions to iteratively list the parameters (C) or +parameters and their values (C) in a given section. + +You can also use the Perl C function to see if a +parameter is defined in a given section. + +Note that none of these will return parameter names that +are part of the default section (if set), although accessing +an unknown parameter in the specified section will return a +value from the default section if there is one. + +=head2 Section names + +=over 4 + +=item foreach( keys %ini ) {...} + +=item while (($k, $v) = each %ini) {...} + +=item if( exists %ini, $section ) {...} + +=back + +When tied to a hash, you use the Perl C and C +functions to iteratively list the sections in the ini file. + +You can also use the Perl C function to see if a +section is defined in the file. + +=head1 IMPORT / DELTA FEATURES + +The I<-import> option to L allows one to stack one +I object on top of another (which might be itself +stacked in turn and so on recursively, but this is beyond the +point). The effect, as briefly explained in L, is that the +fields appearing in the composite object will be a superposition of +those coming from the ``original'' one and the lines coming from the +file, the latter taking precedence. For example, let's say that +C<$master> and C were created like this: + + my $master = Config::IniFiles->new(-file => "master.ini"); + my $overlay = Config::IniFiles->new(-file => "overlay.ini", + -import => $master); + +If the contents of C and C are respectively + + ; master.ini + [section1] + arg0=unchanged from master.ini + arg1=val1 + + [section2] + arg2=val2 + +and + + ; overlay.ini + [section1] + arg1=overriden + +Then C<< $overlay->val("section1", "arg1") >> is "overriden", while +C<< $overlay->val("section1", "arg0") >> is "unchanged from +master.ini". + +This feature may be used to ship a ``global defaults'' configuration +file for a Perl application, that can be overridden piecewise by a +much shorter, per-site configuration file. Assuming UNIX-style path +names, this would be done like this: + + my $defaultconfig = Config::IniFiles->new + (-file => "/usr/share/myapp/myapp.ini.default"); + my $config = Config::IniFiles->new + (-file => "/etc/myapp.ini", -import => $defaultconfig); + # Now use $config and forget about $defaultconfig in the rest of + # the program + +Starting with version 2.39, I also provides features +to keep the importing / per-site configuration file small, by only +saving those options that were modified by the running program. That +is, if one calls + + $overlay->setval("section1", "arg1", "anotherval"); + $overlay->newval("section3", "arg3", "val3"); + $overlay->WriteConfig('overlay.ini', -delta=>1); + +C would now contain + + ; overlay.ini + [section1] + arg1=anotherval + + [section3] + arg3=val3 + +This is called a I (see L). The untouched +[section2] and arg0 do not appear, and the config file is therefore +shorter; while of course, reloading the configuration into C<$master> +and C<$overlay>, either through C<< $overlay->ReadConfig() >> or through +the same code as above (e.g. when application restarts), would yield +exactly the same result had the overlay object been saved in whole to +the file system. + +The only problem with this delta technique is one cannot delete the +default values in the overlay configuration file, only change +them. This is solved by a file format extension, enabled by the +I<-negativedeltas> option to L: if, say, one would delete +parameters like this, + + $overlay->DeleteSection("section2"); + $overlay->delval("section1", "arg0"); + $overlay->WriteConfig('overlay.ini', -delta=>1); + +The I file would now read: + + ; overlay.ini + [section1] + ; arg0 is deleted + arg1=anotherval + + ; [section2] is deleted + + [section3] + arg3=val3 + +Assuming C<$overlay> was later re-read with C<< -negativedeltas => 1 >>, +the parser would interpret the deletion comments to yield the correct +result, that is, [section2] and arg0 would cease to exist in the +C<$overlay> object. + +=head1 DIAGNOSTICS + +=head2 @Config::IniFiles::errors + +Contains a list of errors encountered while parsing the configuration +file. If the I method returns B, check the value of this +to find out what's wrong. This value is reset each time a config file +is read. + +=head1 BUGS + +=over 3 + +=item * + +The output from [Re]WriteConfig/OutputConfig might not be as pretty as +it can be. Comments are tied to whatever was immediately below them. +And case is not preserved for Section and Parameter names if the -nocase +option was used. + +=item * + +No locking is done by [Re]WriteConfig. When writing servers, take +care that only the parent ever calls this, and consider making your +own backup. + +=back + +=head1 Data Structure + +Note that this is only a reference for the package maintainers - one of the +upcoming revisions to this package will include a total clean up of the +data structure. + + $iniconf->{cf} = "config_file_name" + ->{startup_settings} = \%orginal_object_parameters + ->{firstload} = 0 OR 1 + ->{imported} = $object WHERE $object->isa("Config::IniFiles") + ->{nocase} = 0 + ->{reloadwarn} = 0 + ->{sects} = \@sections + ->{mysects} = \@sections + ->{sCMT}{$sect} = \@comment_lines + ->{group}{$group} = \@group_members + ->{parms}{$sect} = \@section_parms + ->{myparms}{$sect} = \@section_parms + ->{EOT}{$sect}{$parm} = "end of text string" + ->{pCMT}{$sect}{$parm} = \@comment_lines + ->{v}{$sect}{$parm} = $value OR \@values + ->{e}{$sect} = 1 OR does not exist + ->{mye}{$sect} = 1 OR does not exists + +=head1 AUTHOR and ACKNOWLEDGEMENTS + +The original code was written by Scott Hutton. +Then handled for a time by Rich Bowen (thanks!), +and was later managed by Jeremy Wadsack (thanks!), +and now is managed by Shlomi Fish ( L ) +with many contributions from various other people. + +In particular, special thanks go to (in roughly chronological order): + +Bernie Cosell, Alan Young, Alex Satrapa, Mike Blazer, Wilbert van de Pieterman, +Steve Campbell, Robert Konigsberg, Scott Dellinger, R. Bernstein, +Daniel Winkelmann, Pires Claudio, Adrian Phillips, +Marek Rouchal, Luc St Louis, Adam Fischler, Kay Röpke, Matt Wilson, +Raviraj Murdeshwar and Slaven Rezic, Florian Pfaff + +Geez, that's a lot of people. And apologies to the folks who were missed. + +If you want someone to bug about this, that would be: + + Shlomi Fish + +If you want more information, or want to participate, go to: + +L + +Please submit bug reports using the Request Tracker interface at +L . + +Development discussion occurs on the mailing list +config-inifiles-dev@lists.sourceforge.net, which you can subscribe +to by going to the project web site (link above). + +=head1 LICENSE + +This software is copyright (c) 2000 by Scott Hutton and the rest of the +Config::IniFiles contributors. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=head1 AUTHOR + +Shlomi Fish + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2000 by RBOW and others. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +http://rt.cpan.org/NoAuth/Bugs.html?Dist=Config-IniFiles or by email to +bug-config-inifiles@rt.cpan.org. + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan + +=head1 SUPPORT + +=head2 Perldoc + +You can find documentation for this module with the perldoc command. + + perldoc Config::IniFiles + +=head2 Websites + +The following websites have more information about this module, and may be of help to you. As always, +in addition to those websites please use your favorite search engine to discover more resources. + +=over 4 + +=item * + +MetaCPAN + +A modern, open-source CPAN search engine, useful to view POD in HTML format. + +L + +=item * + +Search CPAN + +The default CPAN search engine, useful to view POD in HTML format. + +L + +=item * + +RT: CPAN's Bug Tracker + +The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN. + +L + +=item * + +AnnoCPAN + +The AnnoCPAN is a website that allows community annotations of Perl module documentation. + +L + +=item * + +CPAN Ratings + +The CPAN Ratings is a website that allows community ratings and reviews of Perl modules. + +L + +=item * + +CPAN Forum + +The CPAN Forum is a web forum for discussing Perl modules. + +L + +=item * + +CPANTS + +The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution. + +L + +=item * + +CPAN Testers + +The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions. + +L + +=item * + +CPAN Testers Matrix + +The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms. + +L + +=item * + +CPAN Testers Dependencies + +The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution. + +L + +=back + +=head2 Bugs / Feature Requests + +Please report any bugs or feature requests by email to C, or through +the web interface at L. You will be automatically notified of any +progress on the request by the system. + +=head2 Source Code + +The code is open to the world, and available for you to hack on. Please feel free to browse it and play +with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull +from your repository :) + +L + + git clone ssh://git@github.com:shlomif/perl-Config-IniFiles.git + +=cut diff --git a/scripts/tag-release.pl b/scripts/tag-release.pl new file mode 100644 index 0000000..f43cd16 --- /dev/null +++ b/scripts/tag-release.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use IO::All; + +my ($version) = + (map { m{\$VERSION *= *'([^']+)'} ? ($1) : () } + io->file('lib/Config/IniFiles.pm')->getlines() + ) + ; + +if (!defined ($version)) +{ + die "Version is undefined!"; +} + +my @cmd = ( + "hg", "tag", "-m", + "Tagging the Config-IniFiles release as $version", + "releases/$version", +); + +print join(" ", map { /\s/ ? qq{"$_"} : $_ } @cmd), "\n"; +exec(@cmd); + diff --git a/t/00-compile.t b/t/00-compile.t new file mode 100644 index 0000000..cde9dd7 --- /dev/null +++ b/t/00-compile.t @@ -0,0 +1,57 @@ +use 5.006; +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::Compile 2.054 + +use Test::More; + +plan tests => 1 + ($ENV{AUTHOR_TESTING} ? 1 : 0); + +my @module_files = ( + 'Config/IniFiles.pm' +); + + + +# fake home for cpan-testers +use File::Temp; +local $ENV{HOME} = File::Temp::tempdir( CLEANUP => 1 ); + + +my $inc_switch = -d 'blib' ? '-Mblib' : '-Ilib'; + +use File::Spec; +use IPC::Open3; +use IO::Handle; + +open my $stdin, '<', File::Spec->devnull or die "can't open devnull: $!"; + +my @warnings; +for my $lib (@module_files) +{ + # see L + my $stderr = IO::Handle->new; + + my $pid = open3($stdin, '>&STDERR', $stderr, $^X, $inc_switch, '-e', "require q[$lib]"); + binmode $stderr, ':crlf' if $^O eq 'MSWin32'; + my @_warnings = <$stderr>; + waitpid($pid, 0); + is($?, 0, "$lib loaded ok"); + + shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/ + and not eval { require blib; blib->VERSION('1.01') }; + + if (@_warnings) + { + warn @_warnings; + push @warnings, @_warnings; + } +} + + + +is(scalar(@warnings), 0, 'no warnings found') + or diag 'got warnings: ', ( Test::More->can('explain') ? Test::More::explain(\@warnings) : join("\n", '', @warnings) ) if $ENV{AUTHOR_TESTING}; + + diff --git a/t/00load.t b/t/00load.t new file mode 100644 index 0000000..0826707 --- /dev/null +++ b/t/00load.t @@ -0,0 +1,181 @@ +#!/usr/bin/perl +use strict; +use warnings; + +# Should be 15 +use Test::More tests => 15; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; + +use IO::File; + +BEGIN +{ + # TEST + use_ok ('Config::IniFiles'); +} + +my $ini; + +# a simple filehandle, such as STDIN +#** If anyone can come up with a test for STDIN that would be great +# The following could be run in a separate file with data piped +# in. e.g. ok( !system( "$^X stdin.pl < test.ini" ) ); +# But it's only good on platforms that support redirection. +# use strict; +# use Config::IniFiles; +# my $ini = Config::IniFiles->new(-file => STDIN); +# exit $ini ? 0; 1 + +local *CONFIG; +# TEST +# a filehandle glob, such as *CONFIG +if( open( CONFIG, "<", t_file("test.ini") ) ) { + $ini = Config::IniFiles->new(-file => *CONFIG); + ok ($ini, q{$ini was initialized}); + close CONFIG; +} else { + ok (0, "Could not open file"); +} + +# TEST +# a reference to a glob, such as \*CONFIG +if( open( CONFIG, "<", t_file("test.ini") ) ) { + $ini = Config::IniFiles->new(-file => \*CONFIG); + ok ($ini, q{$ini was initialized with a reference to a glob.}); + close CONFIG; +} else { + ok( 0, q{could not open test.ini}); +} + +# an IO::File object +# TEST +if( my $fh = IO::File->new( t_file("test.ini") )) { + $ini = Config::IniFiles->new(-file => $fh); + ok ($ini, q{$ini was initialized with an IO::File reference.}); + $fh->close; +} else { + ok ( 0, "Could not open file" ); +} # endif + + +# TEST +# Reread on an open handle +if( open( CONFIG, "<", t_file("test.ini") ) ) { + $ini = Config::IniFiles->new(-file => \*CONFIG); + ok (($ini && $ini->ReadConfig()), qq{ReadConfig() was successful}); + close CONFIG; +} else { + ok (0, "Could not open file" ); +} + + +use File::Temp qw(tempdir); +use File::Spec (); + +my $dir_name = tempdir(CLEANUP => 1); +my $test01_fn = File::Spec->catfile($dir_name, 'test01.ini'); + +# TEST +if( open( CONFIG, "<", t_file("test.ini") ) ) { + $ini = Config::IniFiles->new(-file => \*CONFIG); + $ini->SetFileName( $test01_fn ); + $ini->RewriteConfig(); + close CONFIG; + # Now test opening and re-write to the same handle + if(! open( CONFIG, "+<", $test01_fn )) { + die "Could not open " . $test01_fn . " for read/write"; + } + $ini = Config::IniFiles->new(-file => \*CONFIG); + my $badname = scalar(\*CONFIG); + # Have to use open/close because -e seems to be always true! + ok( $ini && $ini->RewriteConfig() && !(open( I, $badname ) && close(I)) , + qq{Write to a new file name and write to it}, + ); + close CONFIG; + # In case it failed, remove the file + # (old behavior was to write to a file whose filename is the scalar value of the handle!) + unlink $badname; +} else { + ok (0, "Could not open file"); +} # end if + +# the pathname of a file +$ini = Config::IniFiles->new(-file => t_file("test.ini")); +# TEST +ok ($ini, q{Opening with -file works}); + +# A non-INI file should fail, but not throw warnings +local $@ = ''; +my $ERRORS = ''; +local $SIG{__WARN__} = sub { $ERRORS .= $_[0] }; +eval { $ini = Config::IniFiles->new(-file => t_file("00load.t")) }; +# TEST +ok( + !$@ && !$ERRORS && !defined($ini), + "A non-INI file should fail, but not throw errors" +); + +$@ = ''; +eval { $ini = Config::IniFiles->new(-file => \*DATA) }; +# TEST +ok (!$@ && defined($ini), + "Read in the DATA file without errors" +); + +# Try a file with utf-8 encoding (has a Byte-Order-Mark at the start) +# TEST +$ini = Config::IniFiles->new(-file => t_file("en.ini")); +ok ($ini, + "Try a file with utf-8 encoding (has a Byte-Order-Mark at the start)" +); + + +# Create a new INI file, and set the name using SetFileName +$ini = Config::IniFiles->new(); +my $filename = $ini->GetFileName; +# TEST +ok ((! defined($filename)), + "Not defined filename on fresh Config::IniFiles" +); + +# TEST +$ini->SetFileName(t_file("test9_name.ini")); +$filename = $ini->GetFileName; +is( + $filename, + t_file("test9_name.ini"), + "Check GetFileName method", +); + +$@ = ''; +eval { $ini = Config::IniFiles->new(-file => t_file('blank.ini')); }; +# TEST +ok ((!$@ && !defined($ini)), + "Make sure that no warnings are thrown for an empty file", +); + +$@ = ''; +eval { $ini = Config::IniFiles->new(-file => t_file('blank.ini'), -allowempty=>1); }; +# TEST +ok((!$@ && defined($ini)), + "Empty files should cause no rejection when appropriate switch set", +); + +$@ = ''; +eval { $ini = Config::IniFiles->new(-file => t_file('bad.ini')); }; +# TEST +ok((!$@ && !defined($ini) && @Config::IniFiles::errors), + "A malformed file should throw an error message", +); + +__END__ +; File that has comments in the first line +; Comments are marked with ';'. +; This should not fail when checking if the file is valid +[section] +parameter=value + + diff --git a/t/01basic.t b/t/01basic.t new file mode 100644 index 0000000..2f9f691 --- /dev/null +++ b/t/01basic.t @@ -0,0 +1,97 @@ +#!/usr/bin/perl +use strict; +use warnings; + +# Should be 10. +use Test::More tests => 10; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::Debug; +use Config::IniFiles::TestPaths; + +my ($value, @value); +umask 0000; + +my $ini = Config::IniFiles->new(-file => t_file("test.ini")); +$ini->_assert_invariants(); +t_unlink("test01.ini"); +$ini->SetFileName(t_file("test01.ini")); +$ini->SetWriteMode("0666"); + +# TEST +ok($ini, "Loading from a file"); + +$value = $ini->val('test1', 'one'); +$ini->_assert_invariants(); +# TEST +is ( + $value, 'value1', + "Reading a single value in scalar context" +); + +@value = $ini->val('test1', 'one'); +$ini->_assert_invariants(); +# TEST +is ($value[0], 'value1', "Reading a single value in list context"); + +$value = $ini->val('test1', 'mult'); +# TEST +is ($value, "one$/two$/three", + "Reading a multiple value in scalar context" +); + +@value = $ini->val('test1', 'mult'); +$value = join "|", @value; +# TEST +is_deeply( + \@value, + ["one", "two", "three"], + "Reading a multiple value in list context", +); + +@value = ("one", "two", "three"); +$ini->newval('test1', 'eight', @value); +$ini->_assert_invariants(); +$value = $ini->val('test1', 'eight'); +# TEST +is ( + $value, + "one$/two$/three", + "Creating a new multiple value", +); + +$ini->newval('test1', 'seven', 'value7'); +$ini->_assert_invariants(); +$ini->RewriteConfig; +$ini->ReadConfig; +$ini->_assert_invariants(); +$value=''; +$value = $ini->val('test1', 'seven'); +$ini->_assert_invariants(); +# TEST +is ($value, 'value7', "Creating a new value",); + +$ini->delval('test1', 'seven'); +$ini->_assert_invariants(); +$ini->RewriteConfig; +$ini->ReadConfig; +$ini->_assert_invariants(); +$value=''; +$value = $ini->val('test1', 'seven'); +# TEST +ok (! defined ($value), "Deleting a value"); + +$value = $ini->val('test1', 'not a real parameter name', '12345'); +# TEST +is ($value, '12345', "Reading a default values from existing section"); + +$value = $ini->val('not a real section', 'no parameter by this name', '12345'); +# TEST +is ($value, '12345', "Reading a default values from non-existent section"); + +# Clean up when we're done +t_unlink("test01.ini"); + diff --git a/t/02weird.t b/t/02weird.t new file mode 100644 index 0000000..2023154 --- /dev/null +++ b/t/02weird.t @@ -0,0 +1,88 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# Should be 6. +use Test::More tests => 6; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::Debug; +use Config::IniFiles::TestPaths; + +my ($ini, $value); + + +$ini = Config::IniFiles->new(-file => t_file("test.ini")); +$ini->_assert_invariants(); +$ini->SetFileName(t_file("test02.ini")); +$ini->SetWriteMode("0666"); + +# print "Weird characters in section name . "; +$value = $ini->val('[w]eird characters', 'multiline'); +$ini->_assert_invariants(); +# TEST +is( + $value, + "This$/is a multi-line$/value", + "Weird characters in section name", +); + +$ini->newval("test7|anything", "exists", "yes"); +$ini->_assert_invariants(); +$ini->RewriteConfig; +$ini->ReadConfig; +$ini->_assert_invariants(); +$value = $ini->val("test7|anything", "exists"); +# TEST +is( + $value, + "yes", + "More weird chars.", +); + +# Test 3/4 +# Make sure whitespace after parameter name is not included in name +# TEST +is( $ini->val( 'test7', 'criterion' ), + 'price <= maximum' , + "Make sure whitespace after parameter name is not included in name", +); +# TEST +ok( + ! defined $ini->val( 'test7', 'criterion ' ), + "For criterion containing whitespace returns undef.", +); + +# Test 5 +# Build a file from scratch with tied interface for testing +my %test; +# TEST +ok( (tie %test, 'Config::IniFiles'), "Tying is successful" ); +tied(%test)->SetFileName(t_file('test02.ini')); + +# Test 6 +# Also with pipes when using tied interface using vlaue of 0 +$test{'2'}={}; +tied(%test)->_assert_invariants(); +$test{'2'}{'test'}="sleep"; +tied(%test)->_assert_invariants(); +my $sectionheader="0|2"; +$test{$sectionheader}={}; +tied(%test)->_assert_invariants(); +$test{$sectionheader}{'vacation'}=0; +tied(%test)->_assert_invariants(); +tied(%test)->RewriteConfig(); +tied(%test)->ReadConfig; +# TEST +ok( + scalar($test{$sectionheader}{'vacation'} == 0), + "Returned 0", +); + +# Clean up when we're done +t_unlink("test02.ini"); + diff --git a/t/03comments.t b/t/03comments.t new file mode 100644 index 0000000..c7aabe5 --- /dev/null +++ b/t/03comments.t @@ -0,0 +1,178 @@ +#!/usr/bin/perl +# +# Comment preservation +# + +use strict; +use warnings; + +use Test::More tests => 17; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; +use Config::IniFiles::Slurp qw( slurp ); + +my $ors = $\ || "\n"; +my $irs = $/ || "\n"; +my ($ini, $value); + +# Load ini file and write as new file +$ini = Config::IniFiles->new( -file => t_file("test.ini")); +$ini->SetFileName(t_file("test03.ini")); +$ini->SetWriteMode("0666"); +t_unlink("test03.ini"); +$ini->RewriteConfig; +$ini->ReadConfig; +# TEST +ok($ini, "ini is still initialised"); + +sub t_slurp +{ + return slurp(t_file(@_)); +} + +# Section comments preserved +my $contents = t_slurp("test03.ini"); +# TEST +ok ( + scalar($contents =~ /\# This is a section comment[$ors]\[test1\]/), + "Found section comment." +); + +# Parameter comments preserved +# TEST +ok( + scalar($contents =~ /\# This is a parm comment[$ors]five=value5/), + "Contains Parm comment.", +); + +# Setting Section Comment +$ini->setval('foo','bar','qux'); +# TEST +ok ($ini->SetSectionComment('foo', 'This is a section comment', 'This comment takes two lines!'), + "SetSectionComment returns a true value.", +); + +# Getting Section Comment +my @comment = $ini->GetSectionComment('foo'); +# TEST +is_deeply( + \@comment, + ['# This is a section comment', '# This comment takes two lines!',], + "Section comments are OK.", +); + +# This is a test for: https://rt.cpan.org/Ticket/Display.html?id=8612 +# TEST +is( + scalar($ini->GetSectionComment('foo')), + "# This is a section comment$irs# This comment takes two lines!", + "GetSectionComment in scalar context returns a joined one.", +); + +# Deleting Section Comment +$ini->DeleteSectionComment('foo'); +# Should not exist! +# TEST +ok( + !defined($ini->GetSectionComment('foo')), + "foo section comment does not exist", +); + +# Setting Parameter Comment +# TEST +ok ( + $ini->SetParameterComment( + 'foo', 'bar', 'This is a parameter comment', + 'This comment takes two lines!' + ), + "SetParameterCount was successful", +); + +# Getting Parameter Comment +@comment = $ini->GetParameterComment('foo', 'bar'); +# TEST +is_deeply( + \@comment, + ['# This is a parameter comment', '# This comment takes two lines!'], + "GetParameterComment returns the correct result.", +); + +# TEST +is( + scalar($ini->GetParameterComment('foo', 'bar')), + "# This is a parameter comment$irs# This comment takes two lines!", + "GetParameterComment returns comments separated by newlines", +); + +# Deleting Parameter Comment +$ini->DeleteParameterComment('foo', 'bar'); +# Should not exist! +# TEST +ok( + !defined($ini->GetSectionComment('foo','bar')), + "GetSectionComment for foo/bar should not exist" +); + +# Reading a section comment from the file +@comment = $ini->GetSectionComment('test1'); +# TEST +is_deeply( + \@comment, + ['# This is a section comment'], + "A single section comment for test1", +); + +# Reading a parameter comment from the file +@comment = $ini->GetParameterComment('test2', 'five'); +# TEST +is_deeply( + \@comment, + [ '# This is a parm comment'], + "Reading a parameter comment from the file", +); + +# Reading a comment that starts with ';' +@comment = $ini->GetSectionComment('MixedCaseSect'); +# TEST +is_deeply( + \@comment, + [ '; This is a semi-colon comment' ], + "Singled Section Comment for MixedCaseSect. Reading a comment that starts with ;", +); + +# Test 13 +# Loading from a file with alternate comment characters +# and also test continuation characters (in one file) +$ini = Config::IniFiles->new( + -file => t_file("cmt.ini"), + -commentchar => '@', + -allowcontinue => 1 +); + +# TEST +ok($ini, "cmt.ini instance was properly initialised."); + +$value = $ini->GetParameterComment('Library', 'addmultf_lib'); + +# TEST +ok ( + scalar($value =~ /\@#\@CF Automatically created by 'config_project' at Thu Mar 21 08:46:54 2002/), + "Contains Parameter Comment starting with an at-sign.", +); + +# Test 15 +$value = $ini->val('turbo_library', 'TurboLibPaths'); + +# TEST +ok ( + scalar($value =~ m{\$WORKAREA/resources/c11_test_flow/vhdl_rtl\s+\$WORKAREA/resources/cstarlib_reg_1v5/vhdl_rtl}), + "value is OK." +); + +# Clean up when we're done +t_unlink("test03.ini"); + diff --git a/t/04import.t b/t/04import.t new file mode 100644 index 0000000..6ad14aa --- /dev/null +++ b/t/04import.t @@ -0,0 +1,101 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 19; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; + +my $ors = $\ || "\n"; + +my ($ini,$value); + +# +# Import tests added by JW/WADG +# + +# test 1 +# print "Import a file .................... "; +my $en = Config::IniFiles->new( -file => t_file('en.ini') ); +# TEST +ok ($en, "IniEn was initialized." ); + +# test 2 +my $es = Config::IniFiles->new( -file => t_file('es.ini'), -import => $en ); +# TEST +ok( $es, "Ini es was initialized." ); + + +# test 3 +# Imported values are good +my $en_sn = $en->val( 'x', 'ShortName' ); +my $es_sn = $es->val( 'x', 'ShortName' ); +my $en_ln = $en->val( 'x', 'LongName' ); +my $es_ln = $es->val( 'x', 'LongName' ); +my $en_dn = $en->val( 'm', 'DataName' ); +my $es_dn = $es->val( 'm', 'DataName' ); + +# TEST +is ($en_sn, 'GENERAL', "en_sn is GENERAL"); + +# TEST +is ($es_sn, 'GENERAL', "es_sn is GENERAL too"); + +# TEST +is ($en_ln, 'General Summary', "en_ln is OK."); + +# TEST +is ($es_ln, 'Resumen general', "es_ln is in Spanish"); + +# TEST +is ($en_dn, 'Month', "dn is in English"); + +# TEST +is ($es_dn, 'Mes', "es_dn is in Spanish"); + +# test 4 +# Import another level +my $ca = Config::IniFiles->new( -file => t_file('ca.ini'), -import => $es ); + +# TEST +is ($en_sn, $ca->val( 'x', 'ShortName' ), "en_sn is OK."); +# TEST +is ($es_sn, $ca->val( 'x', 'ShortName' ), "es_sn is OK."); +# TEST +is ($ca->val( 'x', 'LongName' ), 'Resum general', "LongName is OK."); +# TEST +is ($ca->val( 'm', 'DataName' ), 'Mes', "DateName is OK."); + +# test 5 +# Try creating a config file that imports from a hand-built object +my $ini_a = Config::IniFiles->new(); +$ini_a -> AddSection('alpha'); +$ini_a -> AddSection('x'); +$ini_a -> newval('x', 'x', 1); +$ini_a -> newval('x', 'LongName', 1); +$ini_a -> newval('m', 'z', 1); +# TEST +is ($ini_a->val('x', 'x'), 1, "x/x"); + +# TEST +is ($ini_a->val('x', 'LongName'), 1, "x/LongName"); + +# TEST +is ($ini_a->val('m', 'z'), 1, "m/z"); + +# test 6 +## show that importing a file-less object into a file-based one works +my $ini_b = Config::IniFiles->new( -file=>t_file('ca.ini'), -import=>$ini_a ); +# TEST +is ($ini_b->val('x', 'LongName'), 'Resum general', "x/Longname"); +# TEST +is ($ini_b->val('x', 'x', 0), 1, "x/x"); +# TEST +is ($ini_b->val('m', 'z', 0), 1, "m/z"); +# TEST +is ($ini_b->val('m', 'LongName'), 'Resum mensual', "m/LongName"); diff --git a/t/05hash.t b/t/05hash.t new file mode 100644 index 0000000..04fb726 --- /dev/null +++ b/t/05hash.t @@ -0,0 +1,178 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 19; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; + +my %ini; +my ($ini, $value); +my (@value); + +# Get files from the 't' directory, portably +t_unlink("test05.ini"); + +# Test 1 +# Tying a hash. +# TEST +ok ( + (tie %ini, 'Config::IniFiles', + ( -file => t_file("test.ini"), -default => 'test1', -nocase => 1 ) + ), + "Tie to test.ini was successful." +); + +tied(%ini)->SetFileName(t_file("test05.ini")); +tied(%ini)->SetWriteMode("0666"); + +# Test 2 +# Retrieve scalar value +$value = $ini{test1}{one}; +# TEST +is($value, 'value1', + "Value is value1" +); + +# Test 3 +# Retrieve array reference +$value = $ini{test1}{mult}; +# TEST +is (ref($value), 'ARRAY', "test1/mult is an array."); + +# Test 4 +# Creating a scalar value using tied hash +$ini{'test2'}{'seven'} = 'value7'; +tied(%ini)->RewriteConfig; +tied(%ini)->ReadConfig; +$value = $ini{'test2'}{'seven'}; +# TEST +is ($value, 'value7', "test2/seven is value7"); + +# Test 5 +# Deleting a scalar value using tied hash +delete $ini{test2}{seven}; +tied(%ini)->RewriteConfig; +tied(%ini)->ReadConfig; +$value=''; +$value = $ini{test2}{seven}; +# TEST +ok(! defined ($value), "test2/seven does not exist"); + +# Test 6 +# Testing default values using tied hash +# TEST +is ( $ini{test2}{three}, 'value3', "test2/three is equal to value3" ); + +# Test 7 +# Case insensitivity in a hash parameter +# TEST +is ($ini{test2}{FOUR}, 'value4', "test2/FOUR is value4 - case insensitivity" ); + +# Test 8 +# Case insensitivity in a hash section +# TEST +is( $ini{TEST2}{four}, 'value4', "TEST2/four is value4 - case insensitivity" ); + +# Test 9 +# Listing section names using keys +$ini = Config::IniFiles->new( -file => t_file("test.ini"), -default => 'test1', -nocase => 1 ); +$ini->SetFileName(t_file("test05b.ini")); +{ + my @S1 = $ini->Sections; + my @S2 = keys %ini; + + # TEST + is_deeply (\@S1, \@S2, "All sections OK."); +} + +# Test 10 +# Listing parameter names using keys +{ + my @S1 = sort { $a cmp $b } $ini->Parameters('test1'); + my @S2 = sort { $a cmp $b } keys %{$ini{test1}}; + # TEST + is_deeply (\@S1, \@S2, "All keys of section 'test1' are OK."); +} + +# Test 11 +# Copying a section using tied hash +my %bak = %{$ini{test2}}; +# TEST +is ($bak{six}, "value6", "Copied value is OK."); + +# Test 12 +# Deleting a whole section using tied hash +delete $ini{test2}; +$value = $ini{test2}; +# TEST +ok ( (!$value), "test2 section was deleted"); + +# Test 13 +# Creating a section and parameters using a hash +$ini{newsect} = {}; +%{$ini{newsect}} = %bak; +$value = $ini{newsect}{four} || ''; +# TEST +is ($value, 'value4', "Creating a section and parameters using a hash"); + +# Test 14 +# Checking use of default values for newly created section +$value = $ini{newsect}{one}; +# TEST +is ( $value , "value1", + "Checking use of default values for newly created section" +); + +# Test 15 +# print "Store new section in hash ........ "; +tied(%ini)->RewriteConfig; +tied(%ini)->ReadConfig; +$value = $ini{newsect}{four}; +# TEST +is ($value, 'value4', "Store new section in hash"); + +# Test 16 +# Writing 2 line multivalue and returning it +$ini{newsect} = {}; +$ini{test1}{multi_2} = ['line 1', 'line 2']; +tied(%ini)->RewriteConfig; +tied(%ini)->ReadConfig; +@value = @{$ini{test1}{multi_2}}; +# TEST +is_deeply ( \@value, ['line 1', 'line 2'], + "Writing 2 line multivalue and returning it" +); + +# Test 17 +# Getting a default value not in the file +tie %ini, 'Config::IniFiles', ( -file => t_file("test.ini"), -default => 'default', -nocase => 1 ); +$ini{default}{cassius} = 'clay'; +$value = $ini{test1}{cassius}; +# TEST +is ( $value, 'clay', "Getting a default value not in the file"); + +# Test 18 +# Setting value to number of elements in array +my @thing = ("one", "two", "three"); +$ini{newsect}{five} = @thing; +$value = $ini{newsect}{five}; +# TEST +is ($value, 3, "Setting value to number of elements in array"); + +# Test 19 +# Setting value to number of elements in array +@thing = ("one"); +$ini{newsect}{five} = @thing; +$value = $ini{newsect}{five}; +# TEST +is ($value, 1, "Testing that value is 1."); + +# Clean up when we're done +t_unlink("test05.ini"); + diff --git a/t/06oo.t b/t/06oo.t new file mode 100644 index 0000000..2714517 --- /dev/null +++ b/t/06oo.t @@ -0,0 +1,102 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# Originally: 9 +use Test::More tests => 11; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; + +my ($en, $ini, $success); + +# test 1 +# print "Empty list when no groups ........ "; +$en = Config::IniFiles->new( -file => t_file('en.ini') ); +# TEST +is ( scalar($en->Groups), 0, "Empty list when no groups" ); + +# test 2 +# print "Creating new object, no file ..... "; +$ini = Config::IniFiles->new; +# TEST +ok ($ini, "Creating new object"); + +# test 3 +# print "Setting new file name ............."; +# TEST +ok ($ini->SetFileName(t_file('test06.ini')), "Setting new file name"); + +# test 4 +# print "Saving under new file name ........"; +# TEST +ok ($ini->RewriteConfig() && (-f t_file('test06.ini')), + "Saving under new file name ........" +); + +# test 5 +# print "SetSectionComment ................."; +$ini->newval("Section1", "Parameter1", "Value1"); +my @section_comment = ("Line 1 of section comment.", "Line 2 of section comment", "Line 3 of section comment"); + +# TEST +ok( + $ini->SetSectionComment("Section1", @section_comment), + "SetSectionComment() was successful." +); + +# test 6 +# print "GetSectionComment ................."; +{ + my @comment = $ini->GetSectionComment("Section1"); + + # TEST + is_deeply( + \@comment, + [ + "# Line 1 of section comment.", + "# Line 2 of section comment", + "# Line 3 of section comment", + ], + "multi-line GetSectionComment", + ); +} + +# test 7 +# print "DeleteSectionComment .............."; +$ini->DeleteSectionComment("Section1"); +# TEST +ok(!defined($ini->GetSectionComment("Section1")), + "DeleteSectionComment was successful."); + +# test 8 +# CopySection +$ini->CopySection( 'Section1', 'Section2' ); +# TEST +ok( $ini->Parameters( 'Section2' ), "CopySection was successful." ); + +# test 9 +# DeleteSection +$ini->DeleteSection( 'Section1' ); +# TEST +ok( ! $ini->Parameters( 'Section1' ), "DeleteSection was successful." ); + +# test 10 +# RenameSection +$ini->RenameSection( 'Section2', 'Section1' ); +# TEST +ok( ! $ini->Parameters( 'Section2' ) && $ini->Parameters( 'Section1' ) && $ini->val('Section1','Parameter1') eq 'Value1' , "RenameSection was successful." ); + +# test 11 +# Delete entire config +$ini->Delete(); +# TEST +ok( ! $ini->Sections(), "Delete entire config"); + +# Clean up when we're done +t_unlink("test06.ini"); + diff --git a/t/07misc.t b/t/07misc.t new file mode 100644 index 0000000..ba834d4 --- /dev/null +++ b/t/07misc.t @@ -0,0 +1,71 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 5; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; + +my ($ini, $value); + +t_unlink("test07.ini"); + +# Test 1 +# Multiple equals in a parameter - should split on the first +$ini = Config::IniFiles->new( -file => t_file('test.ini') ); +# TEST +is ( scalar($ini->val('test7', 'criterion')), 'price <= maximum', + "Multiple equals in a parameter - should split on the first", +); + +# Test 2 +# Parameters whose name is a substring of existing parameters should be loaded +$value = $ini->val('substring', 'boot'); +# TEST +is( $value, 'smarty', + "Parameters whose name is a substring of existing parameters should be loaded"); + +# test 3 +# See if default option works +$ini = Config::IniFiles->new( -file => t_file("test.ini"), -default => 'test1', -nocase => 1 ); +$ini->SetFileName(t_file("test07.ini")); +$ini->SetWriteMode("0666"); + +# TEST +ok (defined($ini), + "default option works - \$ini works."); + +# TEST +is ( scalar($ini->val('test2', 'three')), 'value3', + "default option works - ->val" +); + +# Test 4 +# Check that Config::IniFiles respects RO permission on original INI file +$ini->WriteConfig(t_file("test07.ini")); +chmod 0444, t_file("test07.ini"); + +SKIP: +{ + if (-w t_file("test07.ini")) + { + skip ('RO Permissions not settable.', 1); + } + else + { + $ini->setval('test2', 'three', 'should not be here'); + $value = $ini->WriteConfig(t_file("test07.ini")); + warn "Value is $value!" if (defined $value); + # TEST + ok(!defined($value), "Value is undefined."); + } # end if +} + +# Clean up when we're done +t_unlink("test07.ini"); + diff --git a/t/08group.t b/t/08group.t new file mode 100644 index 0000000..302ca6a --- /dev/null +++ b/t/08group.t @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; + +my $ini = Config::IniFiles->new( -file => t_file('test.ini') ); +my $members; + +# Test 1 +# Group members with spaces + +# TEST +is_deeply( + [$ini->GroupMembers("group")], + ["group member one", "group member two", "group member three"], + "Group members with spaces", +); + +# Test 2 +# Adding a new section - updating groups list + +# Test 3 +# Deleting a section - updating groups list diff --git a/t/09case.t b/t/09case.t new file mode 100644 index 0000000..f985909 --- /dev/null +++ b/t/09case.t @@ -0,0 +1,177 @@ +#!/usr/bin/perl + + +use strict; +use warnings; + +use Test::More tests => 14; + +use List::Util qw(first); + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; + + +my $ini; +my @members; +my $string; + +# CASE SENSITIVE CHECKS + +# Test 1 +# newval and val - Check that correct case brings back the correct value +$ini = Config::IniFiles->new; +$ini->newval("Section", "PaRaMeTeR", "Mixed Case"); +$ini->newval("Section", "Parameter", "Title Case"); +my $mixed_case = $ini->val("Section", "PaRaMeTeR"); +my $title_case = $ini->val("Section", "Parameter"); + +# TEST +is ($mixed_case, "Mixed Case", "correct case - Mixed Case"); + +# TEST +is ($title_case, "Title Case", "correct case - Title Case"); + +# Test 2 +# Sections +# Set up a controlled environment +$ini = Config::IniFiles->new; +$ini->newval("Section", "Parameter", "Value"); +$ini->newval("section", "parameter", "value"); + +# TEST +is (scalar($ini->Sections()), 2, "2 sections"); + +# Test 3 +# Deleting values +# Set up a controlled environment + +$ini = Config::IniFiles->new; + +$ini->newval("Section", "Parameter", "Title Case"); +$ini->newval("Section", "parameter", "lower case"); +$ini->newval("Section", "PARAMETER", "UPPER CASE"); + +my $delete_case_check_pass = 1; + +@members = $ini->Parameters("Section"); + +# TEST +is (scalar(@members), 3, "Delete check pass - 3 members"); + +$ini->delval("Section", "PARAMETER"); + +@members = $ini->Parameters("Section"); + +# TEST +is (scalar(@members), 2 , "Delete check pass after delete - 2 members"); + +# TEST +ok (first { index($_, "Parameter") >= 0 } @members, "Parameter exists"); + +# TEST +ok (first { index($_, "parameter") >= 0 } @members, "parameter exists"); + +{ + # Test 4 + # Parameters + $ini = Config::IniFiles->new; + $ini->newval("Section", "PaRaMeTeR", "Mixed Case"); + $ini->newval("Section", "Parameter", "Title Case"); + $ini->newval("SECTION", "Parameter", "N/A"); + my @parameter_list = $ini->Parameters("SECTION"); + my $parameters_case_check_pass = 1; + $parameters_case_check_pass = 0 unless scalar(@parameter_list) == 1; + $parameters_case_check_pass = 0 unless $parameter_list[0] eq "Parameter"; + @parameter_list = $ini->Parameters("Section"); + $parameters_case_check_pass = 0 unless scalar(@parameter_list) == 2; + my $parameters = join " ", @parameter_list; + $parameters_case_check_pass = 0 unless ($parameters =~ /PaRaMeTeR/); + $parameters_case_check_pass = 0 unless ($parameters =~ /Parameter/); + # TEST + ok ($parameters_case_check_pass, "Parameters case check pass"); +} + +{ + # Test 5 + # Case sensitive handling of groups + # Set up a controlled environment + $ini = Config::IniFiles->new; + $ini->newval("interface foo", "parameter", "foo"); + $ini->newval("interface bar", "parameter", "bar"); + $ini->newval("INTERFACE blurgle", "parameter", "flurgle"); + my $group_case_check_pass = 1; + # We should have two groups - "interface" and "Interface" + my $group_case_count = $ini->Groups(); + $group_case_check_pass = 0 unless ($group_case_count == 2); + # We don't want to get the "interface" entries when we use the wrong case + @members = $ini->GroupMembers("Interface"); + $group_case_check_pass = 0 unless (scalar(@members) == 0); + # We *do* want to get the "interface" entries when we use the right case + @members = $ini->GroupMembers("interface"); + $group_case_check_pass = 0 unless (scalar(@members) == 2); + $group_case_check_pass = 0 unless (grep {/interface foo/} @members); + $group_case_check_pass = 0 unless (grep {/interface bar/} @members); + # TEST + ok ($group_case_check_pass, "Group cae check pass"); +} + + + +# CASE INSENSITIVE CHECKS + +{ + # Test 6 + # newval - Check that case-insensitive version returns one value + $ini = Config::IniFiles->new( -nocase => "1" ); + $ini->newval("Section", "PaRaMeTeR", "Mixed Case"); + $ini->newval("Section", "Parameter", "Title Case"); + my @values = $ini->val("Section", "parameter"); + + # TEST + is_deeply (\@values, ["Title Case"], + "case-insensitive version returns one value" + ); +} + +# Test 7 +# Case insensitive handling of groups +$ini = Config::IniFiles->new( -file =>t_file('test.ini'), -nocase => 1 ); +# TEST +is_deeply( + [$ini->GroupMembers("GrOuP")], + ["group member one", "group member two", "group member three"] +); + +$ini = Config::IniFiles->new( -file => t_file("test.ini"), -default => 'test1', -nocase => 1 ); +$ini->SetFileName(t_file("test09.ini")); + +# test 8 +# Case insensitivity in parameters + +# TEST +is( scalar($ini->val('test2', 'FOUR')), + 'value4', + "Case insensitivity in parameters", +); + +# test 9 +# Case insensitivity in sections + +# TEST +is ( scalar($ini->val('TEST2', 'four')), + 'value4', + "Case insensitivity in sections", +); + + +# TEST +is ( + scalar($ini->val('mixedcasesect', 'mixedcaseparam')), + 'MixedCaseVal', + "Mixed case val.", +); + diff --git a/t/10delta.t b/t/10delta.t new file mode 100644 index 0000000..d0a16ec --- /dev/null +++ b/t/10delta.t @@ -0,0 +1,97 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 6; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; +use Config::IniFiles::Slurp qw( slurp ); + +my $ors = $\ || "\n"; +my ($ini,$value); + +# +# Delta tests added by D/DO/DOMQ +# + +# test 1 +# print "Import a file .................... "; +my $en = Config::IniFiles->new( -file => t_file('en.ini') ); + +# TEST +ok ( $en, "En was instantiated." ); + +# test 2 +my $es = Config::IniFiles->new( -file => t_file('es.ini'), -import => $en ); + +# TEST +ok( $es, "Es was instantiated." ); + +my $estext=slurp(t_file("es.ini")); +$estext =~ s/\s*//g; + +# test 3 +## Delta without any update should result in exact same file (ignoring +## distinctions about leading whitespace) +t_unlink('delta.ini'); +$es->WriteConfig(t_file('delta.ini'), -delta=>1); + +my $deltatext=slurp(t_file('delta.ini')); +$deltatext =~ s/\s*//g; + +# TEST +is ($deltatext, $estext, + "Delta without any update should result in exact same file " +); + +t_unlink('delta.ini'); + +# test 4 +## Delta with updates +$es->newval("something", "completely", "different"); +$es->WriteConfig(t_file('delta.ini'), -delta=>1); +$deltatext=slurp(t_file('delta.ini')); + +# TEST +if (!ok( + scalar($deltatext =~ m/^[something].*completely=different/sm), + "Delta with updates", + ) +) +{ + diag($deltatext); +} + +t_unlink('delta.ini'); + +# test 5 +## Delta with deletion marks +$es->delval("x", "LongName"); +$es->DeleteSection("m"); +$es->WriteConfig(t_file('delta.ini'), -delta=>1); +$deltatext=slurp(t_file('delta.ini')); + +# TEST +if (!ok(($deltatext =~ m/^. \[m\] is deleted/m) && + ($deltatext =~ m/^. LongName is deleted/m), + "Delta with deletion marks", + ), + ) +{ + diag($deltatext); +} + +# test 6 +## Parsing back deletion marks + +$es=Config::IniFiles->new( -file => t_file('delta.ini'), -import => $en ); +# TEST +ok((!defined $es->val("x", "LongName")) && + (! $es->SectionExists("m")), + "Parsing back deletion marks"); +t_unlink("delta.ini"); diff --git a/t/11copy-ties-with-array-vals.t b/t/11copy-ties-with-array-vals.t new file mode 100644 index 0000000..5f2ba6e --- /dev/null +++ b/t/11copy-ties-with-array-vals.t @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use Test::More tests => 1; + +use strict; +use warnings; + +use File::Spec; + +use Config::IniFiles; + +{ + tie my %ini, 'Config::IniFiles', + (-file => File::Spec->catfile('t', 'array.ini')) + ; + + my %new_sect; + + %new_sect = %{$ini{Sect}}; + + $new_sect{Par}[1] = 'A'; + + # TEST + is_deeply ($ini{Sect}{Par}, [1,2,3], '%ini was not modified'); +} diff --git a/t/12open-empty-file.t b/t/12open-empty-file.t new file mode 100644 index 0000000..f924f03 --- /dev/null +++ b/t/12open-empty-file.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; +use File::Spec; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; + +my $filename = t_file("empty.ini"); + +{ + my $cfg=Config::IniFiles->new; + $cfg->WriteConfig($filename); +} + +{ + my $cfg=Config::IniFiles->new(-file => $filename, -allowempty => 1); + + # TEST + isa_ok ($cfg, "Config::IniFiles", '$cfg'); +} + +t_unlink("empty.ini"); diff --git a/t/13val-in-list-context.t b/t/13val-in-list-context.t new file mode 100644 index 0000000..38abc0b --- /dev/null +++ b/t/13val-in-list-context.t @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +# Fixes: +# https://sourceforge.net/tracker/index.php?func=detail&aid=767913&group_id=6926&atid=106926 + +use Test::More tests => 1; + +use strict; +use warnings; + +use File::Spec; + +use Config::IniFiles; + +{ + my $ini = Config::IniFiles->new( + -file => File::Spec->catfile('t', 'array.ini') + ); + + + my $verdict; + if (my @v = $ini->val("Sect", "NotExist")) + { + $verdict = 1; + } + else + { + $verdict = 0; + } + + # TEST + ok(!$verdict, "False should be returned in list context."); +} + diff --git a/t/14brackets-within-values.t b/t/14brackets-within-values.t new file mode 100644 index 0000000..76d9b3b --- /dev/null +++ b/t/14brackets-within-values.t @@ -0,0 +1,24 @@ +#!/usr/bin/perl + +# This script attempts to reproduce: +# https://sourceforge.net/tracker/index.php?func=detail&aid=2030786&group_id=6926&atid=106926 + +use strict; +use warnings; + +use Test::More tests => 1; +use File::Spec; + +use Config::IniFiles; + +my $filename = File::Spec->catfile(File::Spec->curdir(), "t", "brackets-in-values.ini"); + +{ + my $cfg=Config::IniFiles->new(-file => $filename, -allowempty => 1); + + # TEST + is ($cfg->val('SiteName', 'file'), + "http://www.example.com/files/file[1-22].gz", + "Reading value containing brackets well" + ); +} diff --git a/t/15store-and-retrieve-here-doc-terminator.t b/t/15store-and-retrieve-here-doc-terminator.t new file mode 100644 index 0000000..50e8428 --- /dev/null +++ b/t/15store-and-retrieve-here-doc-terminator.t @@ -0,0 +1,63 @@ +#!/usr/bin/perl + +# This script attempts to reproduce: +# https://sourceforge.net/tracker/index.php?func=detail&aid=1230339&group_id=6926&atid=106926 + +use strict; +use warnings; + +use Test::More tests => 2; +use File::Spec; + +use Config::IniFiles; + +my $filename = File::Spec->catfile( + File::Spec->curdir(), "t", "store-and-retrieve-here-doc-terminator.ini" +); + +my @file_write_subs = +( + sub { + my ($cfg) = @_; + + $cfg->WriteConfig($filename); + + return; + }, + sub { + my ($cfg) = @_; + + open my $fh, '>', $filename + or die "Cannot open '$filename' for writing - $!"; + $cfg->OutputConfigToFileHandle($fh); + close($fh); + + return; + }, +); +foreach my $write_sub (@file_write_subs) +{ + # Prepare the offending file. + { + # Delete the stray file - we want to over-write it. + unlink($filename); + my $cfg=Config::IniFiles->new(); + + $cfg->newval ("MySection", "MyParam", "Hello\nEOT\n"); + + $write_sub->($cfg); + } + + { + my $cfg=Config::IniFiles->new(-file => $filename); + + # TEST*2 + is (scalar($cfg->val ("MySection", "MyParam")), + "Hello\nEOT\n", + "Default here-doc terminator was stored and retrieved correctly", + ); + } + +# Delete it again to keep the working-copy clean. + unlink($filename); +} diff --git a/t/16case-sensitive-default.t b/t/16case-sensitive-default.t new file mode 100644 index 0000000..630ff85 --- /dev/null +++ b/t/16case-sensitive-default.t @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +# This script attempts to reproduce: +# https://sourceforge.net/tracker/index.php?func=detail&aid=1565180&group_id=6926&atid=106926 + +use strict; +use warnings; + +use Test::More tests => 1; +use File::Spec; + +use Config::IniFiles; + +my $filename = File::Spec->catfile( + File::Spec->curdir(), "t", "case-sensitive-default.ini", +); + +{ + my $cfg=Config::IniFiles->new(-file => $filename, -default => "Common", -nocase => 1,); + + # TEST + is ($cfg->val("MyScript", "stopfile", "not defined"), + "myscript-stop", + "Default section handled in nocase => 1", + ); +} + diff --git a/t/17untainted-multiline-values.t b/t/17untainted-multiline-values.t new file mode 100644 index 0000000..f9ecb49 --- /dev/null +++ b/t/17untainted-multiline-values.t @@ -0,0 +1,30 @@ +#!/usr/bin/perl -T + +# This script attempts to reproduce: +# https://sourceforge.net/tracker/index.php?func=detail&aid=1565180&group_id=6926&atid=106926 + +use strict; +use warnings; + +use Test::More tests => 2; +use File::Spec; +use Scalar::Util qw(tainted); + +use Config::IniFiles; + +my $filename = File::Spec->catfile( + File::Spec->curdir(), "t", "array.ini", +); + +{ + my $cfg=Config::IniFiles->new(-file => $filename, -default => "Common", -nocase => 1,); + + my @val = $cfg->val("Sect", "Par"); + + # TEST + ok (!tainted($val[0]), "val[0] is not tainted"); + + # TEST + ok (!tainted($val[1]), "val[1] is not tainted"); +} + diff --git a/t/18non-contiguous-groups.t b/t/18non-contiguous-groups.t new file mode 100644 index 0000000..f33cdc9 --- /dev/null +++ b/t/18non-contiguous-groups.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl -T + +# This script attempts to reproduce: +# https://sourceforge.net/tracker/index.php?func=detail&aid=1720915&group_id=6926&atid=106926 + +use strict; +use warnings; + +use Test::More tests => 1; +use File::Spec; + +use Config::IniFiles; + +my $filename = File::Spec->catfile( + File::Spec->curdir(), "t", "non-contiguous-groups.ini", +); + +{ + my $cfg=Config::IniFiles->new(-file => $filename); + + my @members = $cfg->GroupMembers("A"); + + # TEST + is_deeply( + \@members, + ["A 1", "A 2", "A 3"], + ); +} + diff --git a/t/19param-found-outside-section.t b/t/19param-found-outside-section.t new file mode 100644 index 0000000..bb789bb --- /dev/null +++ b/t/19param-found-outside-section.t @@ -0,0 +1,79 @@ +#!/usr/bin/perl + +# This script attempts to reproduce: +# https://rt.cpan.org/Ticket/Display.html?id=36584 + +# Written by Shlomi Fish. +# This file is licensed under the MIT/X11 License. + +use strict; +use warnings; + +use Test::More tests => 7; + +use Config::IniFiles; +use File::Spec; + +use File::Temp qw(tempdir); + +{ + my $dir_name = tempdir(CLEANUP => 1); + my $filename = File::Spec->catfile($dir_name, "foo.ini"); + my $data = join "", ; + { + open my $fh, '>', $filename; + print {$fh} $data; + close ($fh); + } + + my $ini = Config::IniFiles->new(-file => $filename); + + # TEST + ok(!defined($ini), "Ini was not initialised"); + + # TEST + is (scalar(@Config::IniFiles::errors), 1, + "There is one error." + ); + + # TEST + like ($Config::IniFiles::errors[0], + qr/parameter found outside a section/, + "Error was correct - 'parameter found outside a section'", + ); + + $ini = Config::IniFiles->new(-file => $filename, -fallback => 'GENERAL'); + + # TEST + ok(defined($ini), "(-fallback) Ini was initialised"); + + # TEST + ok($ini->SectionExists('GENERAL'), "(-fallback) Fallback section exists"); + + # TEST + ok($ini->exists('GENERAL', 'wrong'), + "(-fallback) Fallback section catches parameter"); + + # TEST + my $newfilename = File::Spec->catfile($dir_name, "new.ini"); + my $content; + $ini->WriteConfig($newfilename); + { + local $/; + open my $fh, '<', $newfilename; + $content = <$fh>; + } + ok($content =~ /^wrong/m && $content !~ /^\[GENERAL\]/m, + "(-fallback) Outputting fallback section without section header"); +} + +__DATA__ + +; This is a malformed ini file with a key/value outside a scrtion + +wrong = wronger + +[section] + +right = more right + diff --git a/t/20allowedcommentchars.t b/t/20allowedcommentchars.t new file mode 100644 index 0000000..732ce2a --- /dev/null +++ b/t/20allowedcommentchars.t @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +# This script attempts to reproduce: +# https://rt.cpan.org/Ticket/Display.html?id=36309 + +use strict; +use warnings; + +use Test::More tests => 1; +use File::Spec; + +use Config::IniFiles; + +my $filename = File::Spec->catfile( + File::Spec->curdir(), "t", "allowed-comment-chars.ini", +); + +{ + my $cfg = + Config::IniFiles->new( + -file => $filename, + -allowedcommentchars => '};', + ); + + # TEST + is ($cfg->val("cat1", "mykey"), + "500", + "Proper comments are ignored.", + ); +} + diff --git a/t/22trailing-comment-lines.t b/t/22trailing-comment-lines.t new file mode 100644 index 0000000..bbc0b81 --- /dev/null +++ b/t/22trailing-comment-lines.t @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +# This script attempts to reproduce: +# https://rt.cpan.org/Ticket/Display.html?id=30402 +# +# #30402: WriteConfig does not write the last lines of a file if they are comments + +use Test::More tests => 1; + +use strict; +use warnings; + +use File::Spec; + +use Config::IniFiles; + +{ + my $conf = Config::IniFiles->new( + -file => File::Spec->catfile(File::Spec->curdir(), 't', 'trailing-comments.ini') + ); + + my $new_file = File::Spec->catfile( + File::Spec->curdir(), 't', 'new-trail.ini' + ); + + $conf->WriteConfig($new_file); + + my $buffer; + { + local $/; + open my $fh, "<", $new_file; + $buffer = <$fh>; + close($fh); + } + + # TEST + like( + $buffer, + qr{; End Comment 1\n; End Comment 2\n+\z}ms, + "WriteConfig() Preserved end comments." + ); + + # Remove the generated files so they won't pollute the filesystem / + # working-copy. + unlink($new_file); +} + diff --git a/t/23scalar-ref.t b/t/23scalar-ref.t new file mode 100644 index 0000000..576149f --- /dev/null +++ b/t/23scalar-ref.t @@ -0,0 +1,42 @@ +#!/usr/bin/perl + +# This script is a regression test for: +# +# https://rt.cpan.org/Ticket/Display.html?id=45588 +# +# Failure to read the ini file contents from a scalar + +use Test::More; + +use strict; +use warnings; + +use Config::IniFiles; + +if ( ! eval { require IO::Scalar; } ) +{ + plan skip_all => "IO::Scalar is not available"; +} +else +{ + plan tests => 2; +} + +{ + my $contents = <<'EOF'; +[section1] +key = val +EOF + + my $conf = Config::IniFiles->new( -file => \$contents); + + # TEST + ok ($conf, "Object was initialised from reference to scalar."); + + # TEST + is ($conf->val("section1", "key"), + "val", + "Object works." + ); +} + diff --git a/t/24case-sensitive-exists.t b/t/24case-sensitive-exists.t new file mode 100644 index 0000000..97203d0 --- /dev/null +++ b/t/24case-sensitive-exists.t @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +# This script attempts to reproduce: +# https://rt.cpan.org/Ticket/Display.html?id=46721 +# +# #46721: $config->exists() does not pay attention to -nocase => 1 + +use Test::More tests => 2; + +use strict; +use warnings; + +use File::Spec; + +use Config::IniFiles; + +{ + my $conf = Config::IniFiles->new( + -file => File::Spec->catfile(File::Spec->curdir(), + 't', 'case-sensitive.ini' + ), + -nocase => 1 + ); + + # TEST + ok (scalar($conf->exists('FOO', 'BAR')), + "->exists() Handles case well" + ); + + # TEST + is (scalar($conf->val('FOO', 'BAR')), "goodness", + "->val() Handles case well" + ); +} + diff --git a/t/25line-endings.t b/t/25line-endings.t new file mode 100644 index 0000000..a606800 --- /dev/null +++ b/t/25line-endings.t @@ -0,0 +1,85 @@ +#!/usr/bin/perl + +# This script attempts to reproduce: +# http://rt.cpan.org/Public/Bug/Display.html?id=51445 +# +# #51445: 2.52 CRLF-ini with multi-value params fails under Linux + +use Test::More tests => 20; + +use strict; +use warnings; + +use Config::IniFiles; + +use File::Spec; + +my $ini_filename = + File::Spec->catfile( + File::Spec->curdir(), "t", 'test25.ini' + ); + +{ + + # being pedantic, we don't take line breaks from this or an external file for granted + my $sample_ini = + " + # this is a sample file for testing the proper detection of line endings in Config::IniFiles + + [single values] + firstval = first value + secondval=2nd + + # in v2.52 on linux multi values with crlf lines are failing + [multi value] + Paths=< + path1 + path2 + EOT + + "; + + foreach my $lf (("\x0d\x0a", "\x0d", "\x0a", "\x15", "\n")) { + my $ini = $sample_ini; + $ini =~ s/[^<]*/$lf/g; + + open my $INI, '>', $ini_filename or die $!; + binmode $INI; + print $INI $ini; + close $INI; + + my $lf_print = join('', map {sprintf '\x%0.2x', ord $_} split(//, $lf)); + + my $cfg = Config::IniFiles->new(-file => $ini_filename); + + # TEST + ok($cfg, "Loading from a '$lf_print'-separated file"); + + # TEST + my $value = $cfg->val('single values', 'firstval'); + is ( + $value, 'first value', + "Reading a single value from a '$lf_print'-separated file" + ); + + # TEST + $value = $cfg->val('single values', 'secondval'); + is ( + $value, '2nd', + "Reading a single value from a '$lf_print'-separated file" + ); + + my @vals = $cfg->val("multi value", "Paths"); + + # TEST + is_deeply( + \@vals, + ['path1', 'path2'], + "Reading a multiple value from a '$lf_print'-separated file", + ); + + } + +} + +unlink ($ini_filename); diff --git a/t/26scalar-filehandle.t b/t/26scalar-filehandle.t new file mode 100644 index 0000000..ef60cb6 --- /dev/null +++ b/t/26scalar-filehandle.t @@ -0,0 +1,56 @@ +#!/usr/bin/perl +# This script is a regression test for: +# +# https://rt.cpan.org/Ticket/Display.html?id=54997 +# +# Failure to read the ini file contents from a filehandle made out of a scalar +# +# <<< [patch] stat() on unopened filehandle warning thrown when using +# filehandle made from a scalar. >>> + +use Test::More; + +use strict; +use warnings; + +use Carp qw(cluck); +use English qw(-no_match_vars); + +use Config::IniFiles; + +if ( ! eval { require 5.008; } ) +{ + plan skip_all => "We need filehandles made from scalar which is a feature of Perl above 5.8.x"; +} +else +{ + plan tests => 2; +} + +{ + my $contents = <<'EOF'; +[section1] +key = val +EOF + + open my $scalar_fh, "<", \$contents; + + my $conf = eval { + $WARNING = 1; + $SIG{__WARN__} = \&Carp::croak; + Config::IniFiles->new( -file => $scalar_fh); + } or warn $EVAL_ERROR; + + # TEST + ok(!$EVAL_ERROR, "Object was initialised from filehandle made out of a scalar."); + + # TEST + is ($conf->val("section1", "key"), + "val", + "Object works." + ); + + undef $conf; + close $scalar_fh; +} + diff --git a/t/27empty-ini.t b/t/27empty-ini.t new file mode 100644 index 0000000..12d4ff3 --- /dev/null +++ b/t/27empty-ini.t @@ -0,0 +1,32 @@ +#!/usr/bin/perl +# This script is a regression test for: +# +# https://rt.cpan.org/Ticket/Display.html?id=45997 +# +# Failure to read the ini file contents from a filehandle made out of a scalar + +use Test::More tests => 2; + +use strict; +use warnings; + +use Config::IniFiles; +use File::Spec; + +my $empty_fn = File::Spec->catfile(File::Spec->curdir(), "t", "for-27-empty.ini"); + +{ + my $cfg = Config::IniFiles->new( -file => $empty_fn, -allowempty => 1); + + # TEST + ok ($cfg, "object was initialized."); + + my @Groups = $cfg->GroupMembers("test"); + + # TEST + is_deeply ( + \@Groups, + [], + "Groups is empty." + ); +} diff --git a/t/28nomultiline.t b/t/28nomultiline.t new file mode 100644 index 0000000..40a5284 --- /dev/null +++ b/t/28nomultiline.t @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +use Test::More tests => 2; + +use strict; +use warnings; + +use Config::IniFiles; +use File::Spec; + +use File::Temp qw(tempdir); + +{ + my $dir_name = tempdir(CLEANUP => 1); + my $filename = File::Spec->catfile($dir_name, "foo.ini"); + my $data = join "", ; + { + open my $out, '>', $filename; + print {$out} $data; + close( $out ); + } + + my $ini = Config::IniFiles->new(-file => $filename, -nomultiline => 1); + + # TEST + ok(defined($ini), "Ini was initialised"); + + $ini->RewriteConfig; + my $content; + { + open my $fh, '<', $filename; + local $/; + $content = <$fh>; + } + ok($content !~ /EOT/ && $content =~ /^a=1/m && $content =~ /^a=2/m, + "No multiline is output"); +} + +__DATA__ +[section] +a = 1 +a = 2 + diff --git a/t/29end-of-line-comment.t b/t/29end-of-line-comment.t new file mode 100644 index 0000000..b6d3751 --- /dev/null +++ b/t/29end-of-line-comment.t @@ -0,0 +1,76 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 9; + +use Config::IniFiles; +use File::Spec; + +use lib "./t/lib"; +use Config::IniFiles::TestPaths; + +use File::Temp qw(tempdir); + +my $writefile = "end-trailing-comment-writeback.ini"; + +# Test of handle_trailing_comment enabled +{ + my $ini = Config::IniFiles->new( -file => t_file( + "end-of-line-comment.ini"), -handle_trailing_comment => 1); + + # TEST + is($ini->val("section1", "param1"), "value1", + "Comments after ';' should be omitted when tailing comment enabled"); + + # TEST + is($ini->GetParameterTrailingComment("section1", "param1"), + "comment1", "Test GetParameterTrailingComment()"); + + # Test write back + my $dirname = tempdir(CLEANUP => 1); + my $filename = File::Spec->catfile($dirname, $writefile); + # TEST + ok($ini->WriteConfig($filename), "Write trailing comments back"); + + open my $fh, '<', $filename; + my $works = 0; + while (my $line = <$fh>) { + $works = 1 if ($line =~ /param1\s*=\s*value1\s*[;#]\s*comment1/); + } + close $fh; + # TEST + ok($works, "Test trailing comment rewrite ok."); + + # Test set() + # TEST + ok($ini->SetParameterTrailingComment("section1", "param1", + "changed comment1"), "Test SetParameterTrailingComment() returns."); + # TEST + is($ini->GetParameterTrailingComment("section1", "param1"), + "changed comment1", "Test whether SetParameterTrailingComments() works."); +} + +# Test of handle_trailing_comment disabled +{ + my $ini = Config::IniFiles->new( -file => t_file( + "end-of-line-comment.ini"), -handle_trailing_comment => 0); + # TEST + is($ini->val("section1", "param1"), "value1;comment1", + "Comments after ';' should be kept when tailing comment disabled"); + # TEST + is($ini->GetParameterTrailingComment("section1", "param1"), + "", "Test whether SetParameterTrailingComments() works."); +} + +# Test of default handle_trailing_comment +{ + # The default handle_trailing_comment param should be off + my $ini = Config::IniFiles->new( -file => + t_file("end-of-line-comment.ini") ); + # TEST + is($ini->val("section1", "param1"), "value1;comment1", + "Test default trailing comment, which should be off."); +} + diff --git a/t/30parameters-with-empty-values.t b/t/30parameters-with-empty-values.t new file mode 100644 index 0000000..d206d64 --- /dev/null +++ b/t/30parameters-with-empty-values.t @@ -0,0 +1,68 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# This test file tests: +# +# https://rt.cpan.org/Public/Bug/Display.html?id=68554 +# +# << Parameters with empty values not written to file >> + +use Test::More tests => 1; + +use Config::IniFiles; + +use lib "./t/lib"; + +use Config::IniFiles::TestPaths; +use Config::IniFiles::Slurp qw( slurp ); + +{ + my $filename = t_file('params_30.ini'); + + unlink($filename); + + my $ini = Config::IniFiles->new(); + + $ini->newval('MySection', 'MyParam', ''); + + $ini->WriteConfig($filename); + + my $contents = slurp($filename); + + unlink($filename); + + # TEST + like ($contents, qr{^MyParam=$}ms, 'Empty parameter was written.'); +} + +=head1 COPYRIGHT & LICENSE + +Copyright 2011 by Shlomi Fish + +This program is distributed under the MIT (X11) License: +L + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +=cut diff --git a/t/31comments_with_spaces.t b/t/31comments_with_spaces.t new file mode 100644 index 0000000..e43e306 --- /dev/null +++ b/t/31comments_with_spaces.t @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# This test attempts to reproduce +# https://sourceforge.net/tracker/?func=detail&aid=3388382&group_id=6926&atid=106926 + +use strict; +use warnings; + +use Test::More tests => 4; +use File::Spec; + +use Config::IniFiles; + +my $ini_filename = + File::Spec->catfile( + File::Spec->curdir(), "t", 'test31.ini' + ); + +my $content = <<'EOT'; +[section] +value1 = xxx ; My Comment +value2 = xxx ; My_Comment +EOT + +{ + open my $ini_fh, '>', $ini_filename + or die "Cannot open '$ini_filename' - $!"; + print {$ini_fh} $content; + close ($ini_fh); +} + +my $ini = Config::IniFiles->new( -file => $ini_filename, +-handle_trailing_comment => 1, +-commentchar => ';', +-allowedcommentchars => ';#'); + +# TEST +is( $ini->val('section','value1'), 'xxx' ); +# TEST +is( $ini->GetParameterTrailingComment('section','value1'), 'My Comment'); +# TEST +is( $ini->val('section','value2'), 'xxx' ); +# TEST +is( $ini->GetParameterTrailingComment('section','value2'), 'My_Comment'); + +unlink( $ini_filename ); diff --git a/t/32mswin-outputs-contain-crs.t b/t/32mswin-outputs-contain-crs.t new file mode 100644 index 0000000..4058a85 --- /dev/null +++ b/t/32mswin-outputs-contain-crs.t @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +# As of version 2.73 the .ini files generated on Microsoft Windows did not +# contain carriage returns (CRs). This is a regression test for that. + +# Thanks to Ben Johnson for reporting this and for a preliminary test +# script. + +use strict; +use warnings; + +use lib "./t/lib"; +use Config::IniFiles::Slurp qw( bin_slurp ); + +use Test::More; + +if ($^O !~ m/\AMSWin/) +{ + plan skip_all => 'Test is only relevant for Microsoft Windows'; +} +else +{ + plan tests => 1; +} + +use Config::IniFiles; +use File::Spec; + +my $config_filename = File::Spec->catdir(File::Spec->curdir(), "t", "testConfig.ini"); + +writeNewUserIni ($config_filename); + +for my $s (1 .. 4) +{ + print "s = $s\n"; + for my $p (1 ..4) + { + print "p = $p\n"; + writeIni($config_filename, "Section$s", "Param$p", "Value$p"); + } +} + + +# TEST +unlike( + scalar(bin_slurp($config_filename)), + qr/[^\x0D]\x0A/, # \x0D is CR ; \x0A is LF. See "man ascii". + "Checking that all line feeds are preceded by carriage returns", +); + +sub writeNewUserIni +{ + my ($config_fn) = @_; + + open my $fh, '>', $config_fn + or die "Cannot open $config_fn for writing. - $!"; + print {$fh} "[UserConfigFile]\n"; + close ($fh); + + return; +} + +sub writeIni +{ + my ($userConfig_fn, $section, $param, $value) = @_; + + my $usrCfg = Config::IniFiles->new( -file => $userConfig_fn ) + or die "Failed! Could not open $userConfig_fn with error @Config::IniFiles::errors\n" ; + + $usrCfg->newval($section, $param, $value) + or die "Could not set newval in writeIni for $section, $param -> $value\n"; + + my $c = 0; + + while ($c < 6) + { + if ( $usrCfg->RewriteConfig() ) + { + $c=6; + print "Writing [$section] $param -> $value\n"; + } + else + { + warn "Error: could not write $param=$value to $userConfig_fn\n"; + sleep 1; + $c++; + print "c = $c\n"; + } + } + + return; +} diff --git a/t/33update-using-tied-fh-w-shorter-names.t b/t/33update-using-tied-fh-w-shorter-names.t new file mode 100644 index 0000000..efd3bde --- /dev/null +++ b/t/33update-using-tied-fh-w-shorter-names.t @@ -0,0 +1,68 @@ +#!/usr/bin/perl +# This script is a regression test for: +# +# https://rt.cpan.org/Ticket/Display.html?id=45997 +# +# Failure to read the ini file contents from a filehandle made out of a scalar + +use Test::More tests => 1; + +use strict; +use warnings; + +use IO::File; + +use lib "./t/lib"; +use Config::IniFiles::Slurp qw( slurp ); + +use Config::IniFiles; +use File::Spec; + +use File::Temp qw(tempdir); + +my $dirname = tempdir(CLEANUP => 1); +my $filename = File::Spec->catfile($dirname, 'toto.ini'); + +{ + { + open my $fh, '>', $filename; + print {$fh} <<'EOF'; +[toto] +tata=verylongstringtoreplacewithashorterone + +[section3] +arg3=valf +EOF + close($fh); + } + { + my %ini; + + my $fh = IO::File->new( $filename , 'r+' ); + die "Couldn't open file ${filename}: $!" if not defined $fh; + tie %ini, 'Config::IniFiles', ( -file => $fh, -allowempty => 1 ); + + + tied( %ini )->delval("toto", "tata"); + tied( %ini )->RewriteConfig; + + $ini{toto}{tata} = 'short'; + tied( %ini )->RewriteConfig; + + $fh->close; + untie %ini; + } + + # TEST + is ( + scalar (slurp($filename)), + <<'EOF', +[toto] +tata=short + +[section3] +arg3=valf +EOF + "Test that the value was properly shortened.", + ); +} diff --git a/t/34trailing-comments-double-delimeter.t b/t/34trailing-comments-double-delimeter.t new file mode 100644 index 0000000..5c2ae23 --- /dev/null +++ b/t/34trailing-comments-double-delimeter.t @@ -0,0 +1,63 @@ +#!/usr/bin/perl + +# See: https://rt.cpan.org/Ticket/Display.html?id=105255 +# RT #105255 + +use strict; +use warnings; + +use Test::More tests => 1; + +use Config::IniFiles; + +# Test of handle_trailing_comment enabled +{ + my $ini_contents = <<'EOF'; +[MySect] +key=val ; a comment ; further comment + +EOF + + my $cfg = Config::IniFiles->new( + -file => \$ini_contents, + -handle_trailing_comment => 1, + ); + + # TEST + is (scalar( $cfg->val("MySect", "key") ), + "val", + "Value with a double trailing comment." + ); +} + +__END__ + +=head1 COPYRIGHT & LICENSE + +Copyright 2015 by Shlomi Fish + +This program is distributed under the MIT (X11) License: +L + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +=cut diff --git a/t/allowed-comment-chars.ini b/t/allowed-comment-chars.ini new file mode 100644 index 0000000..3e7faea --- /dev/null +++ b/t/allowed-comment-chars.ini @@ -0,0 +1,6 @@ +[cat1] + +mykey = 500 +}This is comment out. +}mykey = 1000 +;This too. diff --git a/t/array.ini b/t/array.ini new file mode 100644 index 0000000..4e77519 --- /dev/null +++ b/t/array.ini @@ -0,0 +1,6 @@ +[Sect] +Par = < 'these tests are for testing by the author'); + } +} + +# This file was automatically generated by Dist::Zilla::Plugin::PodCoverageTests. + +use Test::Pod::Coverage 1.08; +use Pod::Coverage::TrustPod; + +all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' }); diff --git a/t/author-pod-syntax.t b/t/author-pod-syntax.t new file mode 100644 index 0000000..35fb1b9 --- /dev/null +++ b/t/author-pod-syntax.t @@ -0,0 +1,15 @@ +#!perl + +BEGIN { + unless ($ENV{AUTHOR_TESTING}) { + require Test::More; + Test::More::plan(skip_all => 'these tests are for testing by the author'); + } +} + +# This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. +use strict; use warnings; +use Test::More; +use Test::Pod 1.41; + +all_pod_files_ok(); diff --git a/t/bad.ini b/t/bad.ini new file mode 100644 index 0000000..769b11f --- /dev/null +++ b/t/bad.ini @@ -0,0 +1,12 @@ +; Comment +# Alternate comment +[section 1] +Parameter = value +key = 3 + +; this next line is bad +no[value] + +[section 2] +parameter 5 = 34 + diff --git a/t/blank.ini b/t/blank.ini new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/t/blank.ini diff --git a/t/brackets-in-values.ini b/t/brackets-in-values.ini new file mode 100644 index 0000000..2f8d6de --- /dev/null +++ b/t/brackets-in-values.ini @@ -0,0 +1,2 @@ +[SiteName] +file=http://www.example.com/files/file[1-22].gz diff --git a/t/ca.ini b/t/ca.ini new file mode 100644 index 0000000..501ffdd --- /dev/null +++ b/t/ca.ini @@ -0,0 +1,16 @@ +[x] +LongName = Resum general +DataName = Resum general +Description = < 'Test::CPAN::Changes required for this test' if $@; + +changes_ok(); + diff --git a/t/en.ini b/t/en.ini new file mode 100644 index 0000000..48f980b --- /dev/null +++ b/t/en.ini @@ -0,0 +1,27 @@ +[x] +ShortName = GENERAL +LongName = General Summary +DataName = General Summary +ReportType = GeneralSummary +Description = <Note: Depending on the +report time frame, the first and last months may not represent a complete +month's worth of data, resulting in lower hits. +END + + diff --git a/t/end-of-line-comment.ini b/t/end-of-line-comment.ini new file mode 100644 index 0000000..537d3b4 --- /dev/null +++ b/t/end-of-line-comment.ini @@ -0,0 +1,2 @@ +[section1] +param1=value1;comment1 diff --git a/t/es.ini b/t/es.ini new file mode 100644 index 0000000..9cfbaea --- /dev/null +++ b/t/es.ini @@ -0,0 +1,23 @@ +[x] +LongName = Resumen general +DataName = Resumen general +Description = < +Nota: Dependiendo del periodo del informe, el primer y +último mes pueden no representar un mes completo, resultando en menos +aciertos de los reales. +FIN + diff --git a/t/for-27-empty.ini b/t/for-27-empty.ini new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/t/for-27-empty.ini diff --git a/t/lib/Config/IniFiles/Debug.pm b/t/lib/Config/IniFiles/Debug.pm new file mode 100644 index 0000000..81ca497 --- /dev/null +++ b/t/lib/Config/IniFiles/Debug.pm @@ -0,0 +1,49 @@ +use strict; +use warnings; + +use Carp; + +# Checks that the following relationships hold set-wise (e.g. ignoring order): +# +# keys($self->{v}) = $self->{sects} +# +# And for every section $sect: +# +# keys($self->{v}{sect}) = $self->{v}{params} +# +# This should be the case whenever control flows outside this module. Croaks +# upon any error. +sub Config::IniFiles::_assert_invariants { + my ($self)=@_; + my %set; + foreach my $sect (@{$self->{sects}}) { + croak "Non-lowercase section $sect" if ($self->{nocase} && + (lc($sect) ne $sect)); + $set{$sect}++; + } + foreach my $sect (keys %{$self->{v}}) { + croak "Key $sect in \$self->{v} and not in \$self->{sects}" unless + ($set{$sect}++); + } + grep { croak "Key $_ in \$self->{sects} and not in in \$self->{v}" unless + $set{$_} eq 2 } (keys %set); + + foreach my $sect (@{$self->{sects}}) { + %set=(); + + foreach my $parm (@{$self->{parms}{$sect}}) { + croak "Non-lowercase parameter $parm" if ($self->{nocase} && + (lc($parm) ne $parm)); + $set{$parm}++; + } + foreach my $parm (keys %{$self->{v}{$sect}}) { + croak "Key $parm in \$self->{v}{'$sect'} and not in \$self->{parms}{'$sect'}" + unless ($set{$parm}++); + } + grep { croak "Key $_ in \$self->{parms}{'$sect'} and not in in \$self->{v}{'$sect'}" + unless $set{$_} eq 2 } (keys %set); + } +} + +1; + diff --git a/t/lib/Config/IniFiles/Slurp.pm b/t/lib/Config/IniFiles/Slurp.pm new file mode 100644 index 0000000..8494cb1 --- /dev/null +++ b/t/lib/Config/IniFiles/Slurp.pm @@ -0,0 +1,58 @@ +package Config::IniFiles::Slurp; + +use strict; +use warnings; + +use File::Spec; + +use base 'Exporter'; + +use vars (qw(@EXPORT_OK)); + +@EXPORT_OK = (qw( bin_slurp slurp )); + +=head2 slurp() + +Reads the entire file. + +=cut + + +sub slurp +{ + my $filename = shift; + + open my $in, '<', $filename + or die "Cannot open '$filename' for slurping - $!"; + + local $/; + my $contents = <$in>; + + close($in); + + return $contents; +} + +=head2 slurp() + +Reads the entire file with binmode + +=cut +sub bin_slurp +{ + my $filename = shift; + + open my $in, '<', $filename + or die "Cannot open '$filename' for slurping - $!"; + + binmode $in; + local $/; + my $contents = <$in>; + + close($in); + + return $contents; +} + +1; + diff --git a/t/lib/Config/IniFiles/TestPaths.pm b/t/lib/Config/IniFiles/TestPaths.pm new file mode 100644 index 0000000..fe6e9fe --- /dev/null +++ b/t/lib/Config/IniFiles/TestPaths.pm @@ -0,0 +1,41 @@ +package Config::IniFiles::TestPaths; + +use strict; +use warnings; + +use File::Spec; + +use base 'Exporter'; + +use vars (qw(@EXPORT)); + +@EXPORT = (qw(t_file t_unlink)); + +=head2 my $t_filename = t_file($filename) + +Returns a portable filename under "./t" for $filename. + +=cut + +sub t_file +{ + my $filename = shift; + + return File::Spec->catfile(File::Spec->curdir(), "t", $filename); +} + +=head2 t_unlink($filename) + +Unlinks the t_file $filename. + +=cut + +sub t_unlink +{ + my $filename = shift; + + return unlink(t_file($filename)); +} + +1; + diff --git a/t/non-contiguous-groups.ini b/t/non-contiguous-groups.ini new file mode 100644 index 0000000..3271a90 --- /dev/null +++ b/t/non-contiguous-groups.ini @@ -0,0 +1,11 @@ +[A 1] +foo = low + +[A 2] +foo = boo + +[B 1] +foo = roo + +[A 3] +foo = gnome diff --git a/t/pod-coverage.t b/t/pod-coverage.t new file mode 100644 index 0000000..703f91d --- /dev/null +++ b/t/pod-coverage.t @@ -0,0 +1,6 @@ +#!perl -T + +use Test::More; +eval "use Test::Pod::Coverage 1.04"; +plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; +all_pod_coverage_ok(); diff --git a/t/pod.t b/t/pod.t new file mode 100644 index 0000000..976d7cd --- /dev/null +++ b/t/pod.t @@ -0,0 +1,6 @@ +#!perl -T + +use Test::More; +eval "use Test::Pod 1.14"; +plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; +all_pod_files_ok(); diff --git a/t/release-cpan-changes.t b/t/release-cpan-changes.t new file mode 100644 index 0000000..214650f --- /dev/null +++ b/t/release-cpan-changes.t @@ -0,0 +1,19 @@ +#!perl + +BEGIN { + unless ($ENV{RELEASE_TESTING}) { + require Test::More; + Test::More::plan(skip_all => 'these tests are for release candidate testing'); + } +} + + +use strict; +use warnings; + +use Test::More 0.96 tests => 2; +use_ok('Test::CPAN::Changes'); +subtest 'changes_ok' => sub { + changes_file_ok('Changes'); +}; +done_testing(); diff --git a/t/release-kwalitee.t b/t/release-kwalitee.t new file mode 100644 index 0000000..e641ee8 --- /dev/null +++ b/t/release-kwalitee.t @@ -0,0 +1,19 @@ +#!perl + +BEGIN { + unless ($ENV{RELEASE_TESTING}) { + require Test::More; + Test::More::plan(skip_all => 'these tests are for release candidate testing'); + } +} + + +# This test is generated by Dist::Zilla::Plugin::Test::Kwalitee::Extra +use strict; +use warnings; +use Test::More; # needed to provide plan. + +eval { require Test::Kwalitee::Extra }; +plan skip_all => "Test::Kwalitee::Extra required for testing kwalitee: $@" if $@; + +eval "use Test::Kwalitee::Extra"; diff --git a/t/style-trailing-space.t b/t/style-trailing-space.t new file mode 100644 index 0000000..0d1459a --- /dev/null +++ b/t/style-trailing-space.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +eval "use Test::TrailingSpace"; +if ($@) +{ + plan skip_all => "Test::TrailingSpace required for trailing space test."; +} +else +{ + plan tests => 1; +} + +my $finder = Test::TrailingSpace->new( + { + root => '.', + filename_regex => qr/(?:(?:\.(?:t|pm|pl|PL|yml|json|arc|vim))|README|Changes|LICENSE)\z/, + }, +); + +# TEST +$finder->no_trailing_space( + "No trailing space was found." +); + diff --git a/t/test.ini b/t/test.ini new file mode 100644 index 0000000..9e156d2 --- /dev/null +++ b/t/test.ini @@ -0,0 +1,55 @@ +# This is a section comment +[test1] +three=value3 +one=value1 +two=value2 +mult=one +mult=two +mult=three + +[test2] +four=value4 +# This is a parm comment +five=value5 +six=value6 + +[[w]eird characters] +multiline= <