Prev: FAQ 9.10 How do I decode or create those %-encodings on the web?
Next: FAQ 4.14 How can I compare two dates and find the difference?
From: John on 22 Apr 2010 12:26 I have a text file test.txt: <style> a {font-size: 12px;font-family: Arial,Helvetica,sans-serif;} </style> I want to print the file with the following Perl program: #!/usr/bin/perl print "Content-Type: text/html; charset=iso-8859-1\n\n"; print '<html><head>'; print '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'; print '</head> <BODY>'; open MYFILE,"<test.txt"; $ii=1; while ($myline=<MYFILE>) { print "The value is now $ii <br>"; # I expect to see "1" --> OK! print "Line : ".$myline." was here<br>"; # I get "Line : was here" print "Now it is $ii <br>"; # I get "Now it is 3 $ii=$ii+1; } close MYFILE; print "</body> </HTML>"; Somehow the varaiable $ii changes value from 1 to 3 in the middle. Is this a bug in Perl or what is going on? If I change the contents on the test.txt to something else the problem goes away. If the file test.txt has for example: A quick fox jumps over a fence The end ...then no problem is evident..
From: J�rgen Exner on 22 Apr 2010 13:03 John <John.Smith(a)invalid.com> wrote: >I have a text file test.txt: ><style> >a {font-size: 12px;font-family: Arial,Helvetica,sans-serif;} ></style> > >I want to print the file with the following Perl program: > >#!/usr/bin/perl You are missing use strict; use warnings; >print "Content-Type: text/html; charset=iso-8859-1\n\n"; >print '<html><head>'; >print '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'; >print '</head> <BODY>'; >open MYFILE,"<test.txt"; Nowadays the three-argument form of open using a lexical file handle is the preferred method of opening a file. >$ii=1; If you want the line number then you can use the predefined variable $. >while ($myline=<MYFILE>) > { > print "The value is now $ii <br>"; # I expect to see "1" --> OK! > print "Line : ".$myline." was here<br>"; # I get "Line : was here" Cannot reproduce your result (This is perl, v5.10.1 built for MSWin32-x64-multi-thread). I am getting Line : <style> was here<br> and subsequenlty corresponding results for the other two lines. > print "Now it is $ii <br>"; # I get "Now it is 3 I am getting Now it is 1 <br> during the first iteration and Now it is 3 <br> during the last iteration, just as I would have expected. > $ii=$ii+1; > } >close MYFILE; >print "</body> </HTML>"; jue
From: John on 22 Apr 2010 13:29 J�rgen Exner <jurgenex(a)hotmail.com> wrote: >John <John.Smith(a)invalid.com> wrote: >>I have a text file test.txt: >><style> >>a {font-size: 12px;font-family: Arial,Helvetica,sans-serif;} >></style> >> >>I want to print the file with the following Perl program: >> >>#!/usr/bin/perl > >You are missing > use strict; use warnings; > >>print "Content-Type: text/html; charset=iso-8859-1\n\n"; >>print '<html><head>'; >>print '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'; >>print '</head> <BODY>'; >>open MYFILE,"<test.txt"; > >Nowadays the three-argument form of open using a lexical file handle is >the preferred method of opening a file. > >>$ii=1; > >If you want the line number then you can use the predefined variable $. Please explain? I am giving variable $ii the value on one (number 1). > >>while ($myline=<MYFILE>) >> { >> print "The value is now $ii <br>"; # I expect to see "1" --> OK! >> print "Line : ".$myline." was here<br>"; # I get "Line : was here" > >Cannot reproduce your result (This is perl, v5.10.1 built for >MSWin32-x64-multi-thread). I am getting > >Line : <style> > was here<br> > >and subsequenlty corresponding results for the other two lines. > >> print "Now it is $ii <br>"; # I get "Now it is 3 > >I am getting > >Now it is 1 <br> > >during the first iteration and > >Now it is 3 <br> > >during the last iteration, just as I would have expected. > >> $ii=$ii+1; >> } >>close MYFILE; >>print "</body> </HTML>"; > >jue If $ii has the value of one shouldn't it be 2 after $ii=$ii+1 ? I've also tried $ii++; and still get 3 I do get correct operaton with a a diffent text file (the quick brown fox .. version). I checked both text files and they bothe were ANSI with carriage+line feed endings.
From: Uri Guttman on 22 Apr 2010 13:37 >>>>> "J" == John <John.Smith(a)invalid.com> writes: >> John <John.Smith(a)invalid.com> wrote: >>> I have a text file test.txt: >>> <style> >>> a {font-size: 12px;font-family: Arial,Helvetica,sans-serif;} >>> </style> J> I do get correct operaton with a a diffent text file (the quick J> brown fox .. version). I checked both text files and they bothe J> were ANSI with carriage+line feed endings. if that is the case, then i will guess you are looking at the results in a browser. try running this as a script directly on your box. it should print as you think it will. and in your browser, look at the page source after you run it. again, it should be as you think. so this means the html font stuff you are printing is causing the middle line or something to not be properly displayed. ALWAYS check your output independently of what the browser shows you. look at the page source or run it outside a browser. this is your lesson of the day. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Jim Gibson on 22 Apr 2010 14:10
In article <clt0t5pdv9tck9nq1a9f77sif91bpgir1h(a)4ax.com>, John <John.Smith(a)invalid.com> wrote: > I have a text file test.txt: > <style> > a {font-size: 12px;font-family: Arial,Helvetica,sans-serif;} > </style> > > I want to print the file with the following Perl program: > > #!/usr/bin/perl > print "Content-Type: text/html; charset=iso-8859-1\n\n"; > print '<html><head>'; > print '<meta http-equiv="Content-Type" content="text/html; > charset=iso-8859-1">'; > print '</head> <BODY>'; > open MYFILE,"<test.txt"; > $ii=1; > while ($myline=<MYFILE>) > { > print "The value is now $ii <br>"; # I expect to see "1" --> > OK! > print "Line : ".$myline." was here<br>"; # I get "Line : was here" > print "Now it is $ii <br>"; # I get "Now it is 3 > $ii=$ii+1; > } > close MYFILE; > print "</body> </HTML>"; > > > Somehow the varaiable $ii changes value from 1 to 3 in the middle. > > Is this a bug in Perl or what is going on? If I change the contents on the > test.txt to something else the problem goes away. > > If the file test.txt has for example: > A quick fox > jumps over a fence > The end > > ..then no problem is evident.. Are you running your program from the command-line or viewing the output in a browser? If the latter, then the browser may not be showing characters between the <style> and </style> tags. Try encoding special characters in your strings so your browser doesn't interpret them. I don't know the best way to do this, but the HTML::Entities module might work. Others will be found on CPAN (<http://search.cpan.org>). -- Jim Gibson |