Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] unicode and Perl- how to pass command line unicodearguments



>>>>> "gabor" == gabor  <G> writes:

    gabor> could you explain this part to me? why is your own
    gabor> source-code unreliable? :)

Because *my* source code is all *open source*, and it is *intended*
that people other than me use it.  Who knows what they might do?
Consider

iconv -f utf-8 -t shift_jis oh-wow-colors.py \
  | mail -s "way cool!" i-use-becky@example.com

I've seen this kind of thing altogether too often (not just Japanese,
Russians have a fondness for encoding proliferation, too).  And you
know what happens if you *edit* such a file with a cookie-aware
editor!

    gabor> for example, this works fine:
    gabor> =======
    gabor> #!/usr/bin/python
    gabor> # -*- coding: utf-8 -*-
    gabor> 
    gabor> text = u"これは日本語です"
    gabor> print len(text)
    gabor> ========

And for this

=======
#!/usr/bin/python
# -*- coding: utf-8 -*-

text = u"これは日本語です"
print text
========

the output is

Traceback (most recent call last):
  File "./junk.py", line 5, in ?
    print text
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-7: ordinal not in range(128)

Not what we want to see, but that is _precisely_ the right answer
here, since you cannot know what encoding the stream being print'ed to
accepts.  You could say "well, at least the cookie saves you from
typing ".encode('utf-8')" after every literal," but that may or may
not really be very helpful to someone not as familiar with Python's
Unicode facilities as you or I.

"Explicit is better than implicit."<wink> 



-- 
School of Systems and Information Engineering http://turnbull.sk.tsukuba.ac.jp
University of Tsukuba                    Tennodai 1-1-1 Tsukuba 305-8573 JAPAN
               Ask not how you can "do" free software business;
              ask what your business can "do for" free software.

Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links