summaryrefslogtreecommitdiff
path: root/examples/utf8.pl
blob: cdab7f3025f2307e4adbea2691ced0735547fd38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env perl
###############################################################################
## ----------------------------------------------------------------------------
## UTF-8 example.
##
###############################################################################

use strict;
use warnings;

use Cwd qw(abs_path);
use lib abs_path . "/../lib";

use MCE::Loop chunk_size => 'auto', max_workers => 'auto';

use utf8;
use open qw(:utf8 :std);

###############################################################################
## ----------------------------------------------------------------------------
## UTF-8 example. This is based on a sample code sent to me by Marcus Smith.
##
###############################################################################

## Iterate over @list and output to STDOUT three times:
## - Once from a normal for-loop,
## - Once after fetching results from MCE->do()
## - Once after fetching results from MCE->gather()

## Some Unicode characters from Basic Latin, Latin-1, and beyond.
my @list = (qw(U Ö Å Ǣ Ȝ), "\N{INTERROBANG}");

print "0: for-loop: $_\n" for (@list);
print "\n";

MCE::Loop::init { gather => sub { print shift() } };

sub callback {
   print $_[0];
}

mce_loop {
   my $wid = MCE->wid();

   for (@{ $_ }) {
      MCE->do("callback", "$wid: MCE->do: $_\n");
   }

   MCE->sync();

   for (@{ $_ }) {
      MCE->gather("$wid: MCE->gather: $_\n");
   }

} @list;

print "\n";

###############################################################################
## ----------------------------------------------------------------------------
## Process a scalar like a file. Setting chunk_size to 1 for the demonstration.
##
###############################################################################

my $unicode = join("\n", @list) . "\n";

MCE::Loop::init { chunk_size => 1 };

mce_loop_f {
   my $wid = MCE->wid();
   MCE->print("$wid: MCE->print: $_");

} \$unicode;

print "\n";