Strange file handling - please help

Discussion in 'Perl Misc' started by John, Apr 22, 2010.

  1. John

    John Guest

    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..
    John, Apr 22, 2010
    #1
    1. Advertising

  2. John <> 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
    Jürgen Exner, Apr 22, 2010
    #2
    1. Advertising

  3. John

    John Guest

    Jürgen Exner <> wrote:

    >John <> 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.
    John, Apr 22, 2010
    #3
  4. John

    Uri Guttman Guest

    >>>>> "J" == John <> writes:

    >> John <> 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 ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Apr 22, 2010
    #4
  5. John

    Jim Gibson Guest

    In article <>, John
    <> 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
    Jim Gibson, Apr 22, 2010
    #5
  6. John

    John Guest

    I got it figured. My html code was in error and browser threw away output.
    John, Apr 22, 2010
    #6
  7. John

    Uri Guttman Guest

    >>>>> "J" == John <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Apr 22, 2010
    #7
  8. John <> wrote:
    >Jürgen Exner <> wrote:
    >>John <> wrote:

    [...]
    >>>$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).


    Yes. And in each iteration you are reading one line and incrementing the
    number by one. In other words: $ii is a counter for the line number.
    $. does that for you automatically already.

    >>>while ($myline=<MYFILE>)

    [...]
    >>> $ii=$ii+1;


    jue
    Jürgen Exner, Apr 22, 2010
    #8
  9. John <> wrote:
    >I got it figured. My html code was in error and browser threw away output.


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

    jue
    Jürgen Exner, Apr 22, 2010
    #9
  10. John

    Uri Guttman Guest

    >>>>> "JE" == Jürgen Exner <> writes:

    JE> John <> wrote:
    >> I got it figured. My html code was in error and browser threw away output.


    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

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Apr 22, 2010
    #10
  11. John

    Steve M Guest

    On 4/22/2010 10:29 AM, John wrote:
    > Jürgen Exner<> wrote:
    >
    >> John<> 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
    Steve M, Apr 22, 2010
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    484
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    498
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,711
    Mike Hewson
    Jan 7, 2005
  4. Replies:
    1
    Views:
    313
    Roedy Green
    Apr 23, 2008
  5. mastermagrath
    Replies:
    4
    Views:
    384
    robic0
    Dec 1, 2005
Loading...

Share This Page