in reply to a new example

G'day Raymond,

Here's a few examples that may clarify the differences between handling UTF-8 in your output and in your source code. Here's how those characters should render: &#xc4; = Ä and &#x394; = Δ. I've used <pre>...</pre> tags so that the characters (e.g. Δ), and not the entities (e.g. &#x394;), are displayed.

Baseline code generating "Wide character" warning:

$ perl -Mstrict -Mwarnings -E '
    say "\xC4 and \x{0394} look different";
'
Wide character in say at -e line 2.
Ä and Δ look different

Using binmode function to specify UTF-8 output:

$ perl -Mstrict -Mwarnings -E '
    binmode STDOUT => ":utf8";             
    say "\xC4 and \x{0394} look different";
'
Ä and Δ look different

Using the open pragma to specify UTF-8 output:

$ perl -Mstrict -Mwarnings -E '
    use open qw{:std :utf8};
    say "\xC4 and \x{0394} look different";
'
Ä and Δ look different

Attempting to use UTF-8 in the source code without letting Perl know:

$ perl -Mstrict -Mwarnings -E '
    binmode STDOUT => ":utf8";
    say "\xC4 and \x{0394} look different";
    say "Ä and \x{c4} look different";
    say "Δ and \x{0394} look different";
'
Ä and Δ look different
Ä and Ä look different
Δ and Δ look different

Using the utf8 pragma to tell Perl there's UTF-8 in the source code:

$ perl -Mstrict -Mwarnings -E '
    use utf8;
    binmode STDOUT => ":utf8";
    say "\xC4 and \x{0394} look different";
    say "Ä and \x{c4} look the same";
    say "Δ and \x{0394} look the same";
'
Ä and Δ look different
Ä and Ä look the same
Δ and Δ look the same

-- Ken

Replies are listed 'Best First'.
5.pl i have another new example
by Raymond (Novice) on Jul 29, 2013 at 23:09 UTC
    #!/usr/bin/perl use strict; use warnings; my $string = "This is what you have"; print $string; #this part does not print: substr($string, 5, 2) = "wasn't"; #change "is" to "wasn't" substr($string, -12) = "ondrous"; #"this wasn't wondrous" substr($string, 0, 1) = ""; #delete first character substr($string, -10) = ""; #delete last 10 characters #printing problem end here
      Uh... well, yeah, it is a new example of something.

      Please share the 'of what' as I can't see the relevance in a thread devoted to encoding/decoding utf8.

      Oh, yes. When I print the part you've labeled as non-printing, I see this:

      #!/usr/bin/perl use strict; use warnings; # 1046930 my $string = "This is what you have" . "\n";; print $string; #this part does not print: substr($string, 5, 2) = "wasn't"; #change "is" to "wasn't" print $string . "\n"; substr($string, -12) = "ondrous"; #"this wasn't wondrous" print $string . "\n"; substr($string, 0, 1) = ""; #delete first character print $string . "\n"; substr($string, -10) = ""; #delete last 10 characters print $string . "\n"; #printing problem end here =head out: This is what you have This wasn't what you have This wasn't whondrous his wasn't whondrous his wasn't =cut

      ...which is at some variance with what your comments suggest you expected.

      If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

      This makes no sense as a reply to what I wrote, nor does it make any sense in the context of this thread.

      If you'd care to clarify your intent, that would be good. :-)

      -- Ken

        Iam triyng to make a perl site, with examples, this is another one of the samples on perll cookbook and they don't work, and iam asking for help, this one was used on the wrong place, my apologies. Other think I bought a license of Mirc, and i can't use it because it was stolen electroniclly, I don't know were to ask for help, any ideas? I usally put my head intgo work when this thinks happen so:
        #!/usr/bin/perl use strict; use warnings; #extract column with unpack $a = "To be or not to be"; $b = unpack("x6 A6", $a); #skip 6, grab 6 print $b; ($b, $c) = unpack("x6 A2 x5 A2", $a); #forward 6, grab 2; backward 5 +, grab 2 print "$b\n$c\n";
        errors:

        Global symbol "$c" requires explicit package name at 7.pl line 10.

        Global symbol "$c" requires explicit package name at 7.pl line 11.

        Execution of 7.pl aborted due to compilation errors.