Search code examples
perlutf-8perl-module

Perl: How can I inject the UTF-8 pragma in a boilerplate module?


I have a Perl boilerplate module similar to common::sense or Modern::Perl. It's roughly a rip off of Modern::Perl. It looks like this (shortened to keep this question concise):

package Prologue;

use strict;
use feature  ();
use utf8;

sub import {
    strict  ->import;
    feature ->import( ':5.20', 'signatures' );
    utf8    ->import;
}
1;

All in all this works fine. Except for the UTF-8 pragma. Manually adding use utf8; in the calling code has the desired effect.

So how can I inject the UTF-8 pragma into the calling code?


Solution

  • Works for me.

    $ cat Prologue.pm
    package Prologue;
    require utf8;
    sub import { utf8->import }
    1;
    
    $ cat a.pl
    $_ = "é";
    CORE::say(sprintf("%vX", $_));
    use Prologue;
    $_ = "é";
    CORE::say(sprintf("%vX", $_));
    
    $ perl a.pl
    C3.A9
    E9