Hi Shmuel,
See my comments in the body of your reply, down below. Meir Well, let me first try to help: I would add a forward looking note to Chapter 6 (Your actual request-for-comment :-)), section 6. 3: אחרי הקטע: אז הנה, ככה פותחים קובץ: open(my $fh, ">", "wwwest.txt"); הייתי מוסיף: הארגומנט הראשון הוא ה-File Handler, אולם כאן הוא משתנה ככל משתנה, ואפילו בקונטקסט מקומי. (במאמר מוסגר, נדמה לי שהמושג המקובל הוא File-Handle, "ידית", לשיקולך.) הארגומנט האחרון הוא כמובן שם הקובץ או המשתנה המכיל את שם הקובץ. עד כאן הכול כמו קודם. הארגומנט שנוסף, האמצעי, מציין האם הקובץ נפתח לקריאה, לכתיבה, או לצירוף (append) לתוכן קיים, ראה להלן. יש עוד אפשרויות בנוסף לשלוש אלו, שימושיות פחות אך כדאי להיות מודעים להן, ראה בדף ה-MAN של פונקצית open. אולם לארגומנט זה תפקידים נוספים, ואחד החשובים בהם הוא אפיון קידוד הטקסט בקובץ, נושא הראוי לפרק שלם משלו, הוא פרק 9 להלן. בפרק 9.4 הייתי מנסח מחדש ומאזכר את הקטע לעיל. היינו, במקום: וזה יגיד לפרל להתייחס לקלט שלו כמקודד ב-utf8. אפשר להפעיל את אותה הפקודה על הפלט ועל כל קובץ פתוח. כדי לפתוח קובץ עם קידוד כבר, אפשר לכתוב: הייתי מנסח: כדי להכתיב לפרל לכתוב לקובץ בקידוד רצוי, למשל UTF-8, או "להודיע" לו שאנו פותחים קובץ טקסט שמקודד בקידוד מסוים, למשל windows-1255 (Hebrew-Windows), אפשר להשתמש בארגומנט המרכזי של open (ראה בפרק 6.3 לעיל), כך: כמוכן היית מוסיף בסוף פרק 9.4 את הטקס הבא: מספר לא קטן של מודולים מוכנים, חלקם בשימוש נרחב, אינם מודעים לאפשרות שקובץ עשוי/עלול להכיל טקסט שאיננו ASCII. נחסוך לעצמנו הרבה תסכול וזמן אם אנו נהיה מודעים לכך וננקוט בטקטיקה מתאימה. טקטיקה כזו אינה זמינה בהכרח לכל מודול. אולם לחלקם ניתן לאפיין קידוד. כדוגמה נביא את המודולHTML::TreeBuilder מתוך החבילה LWP. למודול זה יש קיצור דרך המאפשר ליצור parsing tree ישירות מן הקובץ: my @tree = HTML::TreeBuilder->new_from_file($file_name); הפעלת "שיטה" (method) זו תפתח את הקובץ, תקרא את תכולתו, תבצע HTML parsing על תוכנו, תבנה "עץ" כשכל node בעץ מאפיין תגית HTML ובסופו של התהליך תסגור את הקובץ. מכולנו הזרים נמנע שימוש בקיצור-דרך זה. עלינו נגזר ללכת בדרך הארוכה: * ליצור אובייקט HTML::TreeBuilder ריק באמצעות השיטה הבסיסית "new" * לפתוח את הקובץ בפקודת "open" רגילה בשיטה לעיל, עם אפיון הקידוד בארגומנט האמצעי * להשתמש בשיטה נוספת של מודול זה HTML::TreeBuilder->parse_file(), אלא שבמקום להעביר לשיטה זו את _שם_ הקובץ, נעביר לו את ה-file-handler. * נסגור את הקובץ להלן דוגמה : use LWP; my $tree = HTML::TreeBuilder->new(); open (my $fh, "<:encoding(windows-1255)", $filename) or die "$!"; $tree->parse_file($fh); close $fh; -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Shmuel Fomberg Sent: Monday, January 17, 2011 4:54 PM To: Perl in Israel Subject: Re: [Israel.pm] My Perl-Hebrew-Tutorial Hi Meir. So, what do you want me to write in this chapter? See above I'm trying to explain a bit about unicode and encodings, and not to talk about certain modules. So do I... About the module itself, I agree with you. especially when the developers are English natives, the need to support other encodings does not come easily to them. Tell me something I don't know... :-( did you open a bug for this module about this issue? I think you should. I don't consider that a "bug". They provided a shortcut/default, but as shortcuts and default go, they don't fit all situations. Well, at least in that case there *is* a workaround. sometimes there isn't, and you need to hack to module to make it do the right thing... Then I would open a bug report. Shmuel. On 2011/01/17 14:59, Meir Guttman wrote: > Dear Shmuel, > > My comment here relates to your Chapter 9, "Hebrew in Perl". > > Unicode text will often force us to avoid many shortcuts. As an > example, I am attaching a (slightly modified version) of an actual > Perl snippet I am using to parse HTML files with the LWP module. This > immensely helpful module has a very nice shortcut that can build a > parse tree directly, given a file name. Unfortunately, this often > breaks down if the file is not Unicode encoded. So here one must use > the long route: First create a "new" /empty/ parse-tree; then "open" > the HTML file to be parsed using a three-argument "open" version > specifying the files encoding; and finally, use this file's /handle/ > (rather than the file's /name/) to populate the tree. > > # The HTML::TreeBuilder->new_from_file(...) method cannot be used > > # here because the site's HTML files are not UTF-8 encoded. Rather, > > # we first create a "new" empty tree object "$root". We then "open" > > # the file, thus giving us the opportunity to specify its encoding. > > # This root is then populated with the file's contents using its > > # handle rather then name in the "parse_file" method. > > Use LWP; > > my $root = HTML::TreeBuilder->new(); > > open (my $fh, "<:encoding(windows-1255)", $filename) or die "$!"; > > $root->parse_file($fh); > > close $fh; > > I found this long route to be required in many other modules too. I > would therefore suggest that module /writers/ be aware of this and > always provide a way to support files having all kinds of text-encoding. > > This is my 2¢ contributions… > > Meir > > -----Original Message----- > From: [email protected] [mailto:[email protected]] On > Behalf Of Shmuel Fomberg > Sent: Sunday, January 16, 2011 11:25 PM > To: Perl in Israel > Subject: [Israel.pm] My Perl-Hebrew-Tutorial > > Hi All. > > I have some time in my hands, so I'm trying to improve my > > Perl-Hebrew-Tutorial a bit. > > http://code.semuel.co.il/perlhebtut/index.html > > Now I improved chapter 6, about handling files. what do you think? > > Also, any more points in the tutorial that need some updating? > > Shmuel. > > _______________________________________________ > > Perl mailing list > > [email protected] > > http://mail.perl.org.il/mailman/listinfo/perl > > > _______________________________________________ > Perl mailing list > [email protected] > http://mail.perl.org.il/mailman/listinfo/perl _______________________________________________ Perl mailing list [email protected] http://mail.perl.org.il/mailman/listinfo/perl
_______________________________________________ Perl mailing list [email protected] http://mail.perl.org.il/mailman/listinfo/perl
