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?

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
Patrick Böker
  • 3,173
  • 1
  • 18
  • 24
  • What are you trying to accomplish? Just switching on `utf8;` by default isn't as simple as it sounds. – Sobrique Sep 23 '15 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](http://stackoverflow.com/a/6163129/1975049) in its entirety. – Patrick Böker Sep 23 '15 at 16:54
  • This might be relevant reading then: https://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default – Sobrique Sep 23 '15 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? – Patrick Böker Sep 23 '15 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. – ThisSuitIsBlackNot Sep 23 '15 at 17:27
  • See [How can I export a list of modules with my own module?](http://stackoverflow.com/questions/30814892/how-can-i-export-a-list-of-modules-with-my-own-module) – Håkon Hægland Sep 23 '15 at 18:13

3 Answers3

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
ikegami
  • 367,544
  • 15
  • 269
  • 518
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.

Community
  • 1
  • 1
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
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.

Patrick Böker
  • 3,173
  • 1
  • 18
  • 24