2

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?

6
  • What are you trying to accomplish? Just switching on utf8; by default isn't as simple as it sounds.
    – Sobrique
    Sep 23, 2015 at 16:47
  • I want use utf8; so I can use non ASCII chars in variable names (german umlauts in my case). I do admit, that I have not understood the world of unicode in Perl in its entirety. Sep 23, 2015 at 16:54
  • This might be relevant reading then: stackoverflow.com/questions/6162484/…
    – Sobrique
    Sep 23, 2015 at 16:55
  • Shame on me... I had a typo in use ProLogue;. I am on Windows, which is case insensitive and did not report the error. Should this question better be deleted? Sep 23, 2015 at 17:15
  • @patszim It actually can't be deleted because it's got an upvoted answer. I would make your last comment an answer in the off chance that someone else in the future has a similar issue. Sep 23, 2015 at 17:27

3 Answers 3

3

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
0

(Self-answered by patszim)

As pointed out by ikegami this does work as expected. My failure was a typo in the use statement: use ProLogue; with a capital "L" instead of use Prologue;. On my case-insensitive Windows system this causes Perl to silently not import the Prologue module.

The silent import failure on Windows now has a bug report.

0

This is not a direct answer but a pointer for people trying to create their own boilerplate modules.

The Import::Into module can import arbitrary modules into other packages. It has a very good explanation of what can go wrong and what to do about it: Why to use this module? I myself did not use that module but copied the respective tricks into my boilerplate module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.