Const-Fast reviews

cpanratings
 

RSS | Module Info | Add a review of Const-Fast

Const-Fast (0.014) ****

I am impressed with the speed improvement over Readonly (as reported at neilb.org/reviews/constants.html), but the treatment of hashes has bitten me a few times. The documentation explains that a hash that is made into a constant by this module becomes readonly but that Perl can't distinguish between a readonly hash and a restricted hash, such that an error occurs when testing for the truthiness of $somehash{'no-such-key'} (namely: "Attempt to access disallowed key 'no-such-key' in a restricted hash"), and the documentation suggests using "if (exists $somehash{'no-such-key'})" instead of "if ( $somehash{'no-such-key'})". The problem I've had is when a readonly hash is sent to a CPAN module that uses a truthiness test in checking for a key that's nonexistent in the hash. The error arises, and there seems to be no way to avoid it other than modifying the module and putting it into a local library. In particular, this comes up when I use the DBI module and send over a reference to a readonly hash of DBI connection attributes that has been set as readonly by Const::Fast but that doesn't expressly include a "dbi_connect_method" key. For example: my $dbh = DBI->connect( $DSN, $USERNAME, $DB_PASSWORD, $ATTRIBUTES_REF ) gives rise to this error: "attempt to access disallowed key 'dbi_connect_method' in a restricted hash at /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBI.pm line 578." I know that I could write my program to include an empty-value 'dbi_connect_method' key in the attributes hash, but the general concern is that the issue will come up (at runtime) with other modules.

Const-Fast (0.011) *****

I just found this module on CPAN while searching on another topic and was so pleased. Have been using Readonly for years to do constants and as the author of Const::Fast mentions in the documentation Readonly can have some weird behavior due to its internals which I'd become rather familiar with after so long :-)

I just took an entire application and swapped Readonly for Const::Fast and everything worked out-of-the-box, the only additional things I had to fix were things I was doing incorrectly in my code like accidentally autovivifying read-only hash values during tests that Readonly didn't notice :-)

Const-Fast (0.011) *****

Very fast, does 90% of what we want it to do (subject to limitations in perl which are documented). Further, the author responded to a bug I filed in RT within 24 hours. Kudos.

Const-Fast (0.009) *****

Comparing the three possible ways to define constants (Const::Fast, Readonly, constant) I found Const::Fast to be the best. It is very light and operates directly on Perl's internal SvREADONLY. It adds almost none processing overhead. Readonly is much heavier (its implementation is quite complex) and it has a performance overhead (as its documentation clearly states). The third way, Perl's default standard constant, is annoying when concatenating strings since it doesn't interpolate.

I just converted a project consisting of 75 Perl source code files to use Const::Fast exclusively. It was an easier task than I expected. First I forgot to use "=>" in assigning the value and used "=" instead. This resulted "Type of arg 1 to Const::Fast::const must be one of [$@%]..." errors. After these were fixed I needed to change the code in approx. 15 places to use "exists" to test key presence (see documentation CAVEATS). But these steps were all. I then did some testing to verify that Const::Fast::const actually protects the data and yes, the data is protected: the program dies with "Modification of a read-only value attempted" or "Attempt to access disallowed key '*****' in a restricted hash".

Const-Fast (0.005) ****

A replacement for Readonly.

Although I welcome this module, specially if it is much faster than Readonly there are a few things that I would have liked to see in the documentation.

The author writes "I stumbled on some serious issues of Readonly that aren't easily fixable without breaking backwards compatibility in subtle ways" and "Readonly's use of ties is a source of subtle bugs and bad performance". Please, please enlighten us. What are the performance problems, and show us a benchmark so the zillion users don't have to run their own, and tell us about the subtle bugs so we don't fall pray to them. Those get the documentation a miserable two stars.

Then Leon goes: "this module uses the builtin readonly feature of perl". From which version of perl? ah! right, 5.008. that could be in the documentation instead for the source.

Really, wasn't there a way to make this a Readonly update? I'm quite sure Readonly author is not that difficult to work with.

Otherwise a good module, which I came to expect from this author; Check the other goodies he wrote.

1 hidden unhelpful review