Glenn Linderman wrote:
> So I'm trying to make a sub to tr iso-8859-1 to unaccented ASCII 
> equivalents.  Maybe one already exists, somewhere, but now that I've 
> gotten this far, I'm confused:
> 
> #!perl

Where's your use strict and use warnings ?

> BEGIN {
>   my ( $tfrom, $tto );
>   $tfrom = 'AàÀáÁâÂãÃäÄåÅæÆBCçÇDðÐEèÈéÉêÊëËFGHIìÌíÍîÎïÏJKLM';
>   $tto   = 'aaaaaaaaaaaaaaabcccdddeeeeeeeeefghiiiiiiiiijklm';
>   $tfrom .= 'NñÑOòÒóÓôÔõÕöÖøØPQRSßTþÞUùÙúÚûÛüÜVWXYýÝÿZ';
>   $tto   .= 'nnnooooooooooooopqrsstttuuuuuuuuuvwxyyyyz';
>   eval <<END_OF_SUB
> sub tr_iso_8859_1__ASCII
> { my ( \$name ) = @_;
>   \$name =~ tr/$tfrom/$tto/;
>   print "in: \$_[ 0 ] out: \$name\n";
>   return ( \$name );
> }
> END_OF_SUB

You need an ';' here.

> }
> 
> & 
> tr_iso_8859_1__ASCII('abcdefABCDEFAàÀáÁâÂãÃäÄåÅæÆBCçÇDðÐEèÈéÉêÊëËFGHIìÌíÍîÎïÏJKLM');
> __END__
> 
> produces
> 
> Undefined subroutine &main::tr_iso_8859_1__ASCII called at 
> d:\my\perl\src\test-tr.pl line 17.
> 
> which is the last line of the code.  So what about eval, or what about 
> BEGIN, or what about my syntax, is causing the sub not to be available 
> later... and how do I work around it?
> 
> Naturally the BEGIN/eval idea was formulated so that I could use 
> variables to help define the tr parameters, mostly just to avoid long 
> lines, and I already have the sub working using long lines with 
> hard-coded strings.  But why doesn't this one work?

Here's my attempt:

use strict;
use warnings;

my $tfrom = 'AàÀáÁâÂãÃäÄåÅæÆBCçÇDðÐEèÈéÉêÊëËFGHIìÌíÍîÎïÏJKLMNñÑ' .
   'OòÒóÓôÔõÕöÖøØPQRSßTþÞUùÙúÚûÛüÜVWXYýÝÿZ';
my $tto   = 'aaaaaaaaaaaaaaabcccdddeeeeeeeeefghiiiiiiiiijklmnnn' .
   'ooooooooooooopqrsstttuuuuuuuuuvwxyyyyz';

eval <<END_OF_SUB
sub tr_iso_8859_1__ASCII {
        my \$name = \$_[0];
\$name =~ tr/$tfrom/$tto/;
print "in : \$_[0]\nout: \$name\n";
return \$name;
}
END_OF_SUB
;

tr_iso_8859_1__ASCII(
   'abcdefABCDEFAàÀáÁâÂãÃäÄåÅæÆBCçÇDðÐEèÈéÉêÊëËFGHIìÌíÍîÎïÏJKLM');

__END__
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to