Strange file handling - please help

J

John

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..
 
J

Jürgen Exner

John said:
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.

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
 
J

John

Jürgen Exner said:
You are missing
use strict; use warnings;


Nowadays the three-argument form of open using a lexical file handle is
the preferred method of opening a file.


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).

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.


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.


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.
 
U

Uri Guttman

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
 
J

Jim Gibson

John said:
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>).
 
U

Uri Guttman

J> I got it figured. My html code was in error and browser threw away output.

which is just what i guessed. did you learn the lesson i sent?

uri
 
U

Uri Guttman

JE> And how would that affect the output of your Perl script?

as i said (and guessed correctly) he was only looking at the browser and
not the page source or running the script directly. somehow his wacky
html (not going there) obscured his counter.

uri
 
S

Steve M

Please explain? I am giving variable $ii the value on one (number 1).

$. is the INPUT_LINE_NUMBER

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.

Given the above you are banging on a simple cgi script and viewing the
output in a browser which makes this more of a CGI issue than a Perl
issue, but:

If you view the source you should see the the text you expect to see is
in fact in the source, but given that it is wrapped in the <style> tags
your browser is not displaying it.

The final line with '3' in it shows up because it is outside the closing
</style> tag.

Firebug is (or should be) your friend if you are going to do CGI work.
Get it. Learn it. Love it. Firebug is a CGI/Javascript/AJAX developers
absolute *must have* plugin for FireFox.

Your mention of the cr/lf endings AND the shebang line used suggest you
are pretty new at cross-platform CGI work. Placing the <style> where you
did (inside the <body>) is very suggestive of your relative HTML knowledge.

I'm thinking you might need to brush up on CGI in general, the
differences between *nix files/file systems and lesser operating
systems, and some HTML basics.

Uh... more Perl knowledge is always a good thing too. :)

I'm not putting you down, we all have to start somewhere, and you *have*
started. Keep plugging away at it, you'll get there.

Here's a little framework that you may find useful in playing with CGI
'stuff'. use #!perl as the shebang for Apache on Windows (typically)


#!/usr/bin/perl

use warnings;
use strict;

# use CGI qw( :cgi );

# use Whatever::Module;

our %V; # useful global



# browser error dump routine (with Caller_error_path, EOF)
# does NOT catch earlier errors (if any) above
# also has a tendency to mess with $@ so using eval
# to trap errors won't do what you think it should
$SIG{'__DIE__'} = $SIG{'__WARN__'} = sub {
my $error = shift;
chomp $error;
$error =~ s/[<&>]/"&#".ord($&).";"/ge; # entity escape;
$error = &Caller_error_path( $error,1,0 );
&print_mime_type;
print "$error\n";
exit 0;
};

# test code goes here
&print_mime_type;
print "Success<br>\n";

# uncomment line below to test error trapping
# print &unknown_subroutine;


###############
## utility subs

# makes it easy to pass specific mime types, stops multiple
# mime types from being printed due to programmer error
# defaults to text/html
sub print_mime_type {
my $type = shift;
$V{'MIME_DONE'} and return;
$type ||= 'text/html';
print "Content-type: $type\n\n";
$V{'MIME_DONE'}++;
}

# constructs trace back to see where you failed and how you got there
# I've had this around for years, no doubt could be cleaned up a bit
sub Caller_error_path {
my $error = shift;
my $Shift = shift;
my $Pop = shift;
$error ||= '';

my $i = 0;
my @call_list = ();
while( my($p, $f, $l, $s, $h, $w ) = caller($i++) ){
my $string = '';
$f and $string .= "$f, ";
$l and $string .= "Line: $l\n";
$s and $s !~ /main::__ANON__/ and $string .= "$s, ";
push @call_list, $string;
}

$Shift and shift @call_list;
$Pop and pop @call_list;

@call_list = reverse @call_list;

my $path = qq~
<form>
<input type="button" Value="Back to Last Page"
onclick="window.history.go(-1)">
</form>
Trace:
~;
$path .= join '', @call_list;
return qq~
<pre>$path\n<span style="color: #FF0000;">$error</span></pre>
~;

1; # not required here, but IS for required library files


If someone else sees a major problem in any of the above, you have my
full attention. :)

hth,

\s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top