From: Steve M on
On 4/22/2010 10:29 AM, John wrote:
> 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).
>

$. is the INPUT_LINE_NUMBER


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

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

--
"There is no use in your walking five miles to fish when you can depend
on being just as unsuccessful near home." M. Twain
From: Tad McClellan on
John <John.Smith(a)invalid.com> wrote:

> Is this a bug in Perl


Asking that is a good way to get your future posts ignored,
so you should resist the urge to ask it.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.