<rambling_intro>
A workmate directed me at the semantic web in haiku. This in turn put me in mind of TheDamian's Coy.pm which intercepts Perl's error messages and translates them into haiku. So after a quick perl -MCPAN -e "install 'Coy'" I attempted to induce an error with this one-liner:

perl -MCoy -e "wibble()"

Instead of a gentle soothing stanza, I was greeted with:

Modification of a read-only value attempted at /usr/lib/perl5/site_per +l/5.6.1/Lingua/EN/Inflect.pm line 168. Compilation failed in require at /usr/lib/perl5/site_perl/5.6.1/Coy.pm + line 19. BEGIN failed--compilation aborted at /usr/lib/perl5/site_perl/5.6.1/Co +y.pm line 19. Compilation failed in require. BEGIN failed--compilation aborted.

Not quite the effect I was going for :-(
</rambling_intro>

Anyway, to get to the point ("at last!" I hear you say), I dived into Lingua/EN/Inflect.pm and at line 168, found this code...

my $PL_sb_C_en_ina = join "|", map { chop; chop; $_; } ( "stamen", "foramen", "lumen", );

Now clearly this is a bug - the values passed to map are constants and since map aliases $_ to each value in turn, chop does attempt to modify a constant. It's also a bug that Damian has fixed already (all I had to do was install the latest Lingua::EN::Inflect). But this being Perl, there's "More Than One Way To Fix It". My first fix was to introduce an intermediate variable:

my $PL_sb_C_en_ina = join "|", map { my $a = $_; chop $a; chop $a; $a +} ( "stamen", "foramen", "lumen", );
My second fix included changing the source data format:
my $PL_sb_C_en_ina = join "|", map { chop; chop; $_ } split /\s+/, q(stamen foramen lumen);
My third included a gratuitous use of a regex:
my $PL_sb_C_en_ina = join "|", map { /^(.*)..$/ && $1 } split /\s+/, q(stamen foramen lumen);

Without peeking at Damian's code (in the latest version of Lingua::EN::Inflect) how would you fix it?

Update: fixed typo in first fix as per abell's note below.

Replies are listed 'Best First'.
Re: Modification of a read-only value attempted: MTOWTFI
by jsprat (Curate) on Oct 14, 2002 at 22:37 UTC
    I'd use this:

    my $myPL_sb_C_en_ina = join "|", map {substr $_, 0, -2} ( "stamen", "foramen", "lumen", );

    Straightforward, uses substr like it was meant to be used.

Re: Modification of a read-only value attempted: MTOWTFI
by abell (Chaplain) on Oct 15, 2002 at 11:07 UTC

    Your "first fix" sets the variable as "e|e|e", since chop returns the chopped character. You should have ended the mapping block with a $a.

    Being as lazy as I am, I would write the snippet as
    my $PL_sb_C_en_ina = 'stam|foram|lum';

    Bye

    Antonio

    The stupider the astronaut, the easier it is to win the trip to Vega - A. Tucket

      ++abell K.I.S.S wins in my book. It's hard to see why the code was written to set up an array of constants two chars longer than needed, then use a map to chop them down to size?


      Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

      Thanks for picking up the error - I had actually found and fixed the bug in my testing but apparently pasted in the broken version. Doh!

      As to why Damian chose to include a list of the full words and then incur the processing overhead of chopping them down, my best guess is to make it easier to maintain the list of words. A more efficient solution would be to generate the Lingua::EN::Inflect module (using the form you suggest) from the list of full words at the time the module was installed.

Re: Modification of a read-only value attempted: MTOWTFI
by Ovid (Cardinal) on Oct 14, 2002 at 23:10 UTC

    A simple way to do this is with local:

    my $PL_sb_C_en_ina = join "|", map { local $_ = $_; chop; chop; $_; } ( "stamen", "foramen", "lumen", );

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Modification of a read-only value attempted: MTOWTFI
by Zaxo (Archbishop) on Oct 15, 2002 at 02:02 UTC

    I'd make a copy:

    my $PL_sb_C_en_ina = join '|', map { chop; chop; $_; } @{[ qw( stamen foramen lumen) ]};

    After Compline,
    Zaxo

(tye)Re: Modification of a read-only value attempted: MTOWTFI
by tye (Sage) on Oct 14, 2002 at 22:52 UTC
Re: Modification of a read-only value attempted: MTOWTFI
by Aristotle (Chancellor) on Oct 14, 2002 at 23:49 UTC
    Just being silly: join "|", split /\S\S(?:\s+|$)/, q(stamen foramen lumen);

    Makeshifts last the longest.