On 3/11/07, Benct Philip Jonsson <[EMAIL PROTECTED]> wrote:
snip
:    use HTML::Entities;
:
:    sub transliterate ($;$){ my($text,$option) = @_;
:
:        # Begin troblesome part!
:        if($option && ref $option ne 'HASH'){
:            die "The second argument to transliterate() must"
:                . "be a hash reference"; }
:        # End troublesome part
:
:        # Doing transliteration stuff to $text here!
:
:        if($$option{html}){
:            $text = encode_entities($text)
:        }
:        return $text;
:    }

so that using the function will look like this:

     $text = transliterate($text,{html = 1});

I want to ensure that the second argument, if it exists, is
a hash reference, and die with a complaint if it is not!
What I wonder is if there is some 'automatic' way to do the
check rather than using the conditional to check within the
sub as I have done, preferably if there is a way to tell the
prototype that it should look for a hash reference.
snip

No, and I would advise against using the prototypes.  They are a
misfeature that is being corrected in Perl 6.

snip
Also, and perhaps more importantly, is there any way to
tell the module to HTML-encode the output of
transliterate() when I import the module, sort of like use-
ing the module with options:

     use MyTransliterator --html-entities;
snip

When a module is use'ed its import function is called with the
arguments passed to the use.  So, all you need to do is add an import
function like this:

sub import {
   our $html = 0;
   for my $option (@_) {
       if ($option eq 'html-entities') {
           $html = 1;
       }
   }
}

And then you can add a check to transliterate:

our $html;
if($html or $option->{html}){


The module would be use'ed like this

use MyTransliterator qw(html-entities);

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to