abhishes has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I wrote this simple perl program to read java source code files. But to my surprise the program is behaving very stangely.

open FILE, "Sample.java"; my @lines = <FILE>; close FILE; foreach my $str (@lines){print $str};

the source fille which I am reading is

package com.test.pack.app.visual.command; import java.math.BigDecimal; import java.text.ParseException; import javax.servlet.http.HttpServletRequest; import com.test.util.validation.MoneyValidator; import com.test.util.validation.StringValidator; import com.test.util.typeconverter.ExternalTextConverter;

The output of my program is
"import com.test.util.typeconverter.ExternalTextConverter;"

Where did all the other lines go?? why didn't the program print all the lines?

regards,
Abhishek.

PS: Is it just me?? every time I write a program it behaves is a most funny way in the begining!!

Replies are listed 'Best First'.
Re: Strange File read problem
by BrowserUk (Patriarch) on Apr 09, 2003 at 15:16 UTC

    Try changing the last line of the program to

    foreach my $str (@lines){print $str, "\n"};

    I'm guessing that all the lines are being printed one over the other on the terminal?


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.

      Possible, but doubtful. He's not using chomp or removing the newline in any other way, so the \n should still be there....

      That said, I don't know what the problem is either.

Re: Strange File read problem
by VSarkiss (Monsignor) on Apr 09, 2003 at 15:21 UTC

    It's a little known fact that the Perl 5 parser will reject any input lines containing the string "Java". That's probably what's getting you.1

    Seriously, there's nothing obviously wrong with your program. I would suggest first trying to pin down whether the problem is with reading the file or outputting results. For instance, right after you close the filehandle, add: print "I got ", scalar(@lines), " from the file\n"; If you get one line, the problem's with the input, and if you get seven lines, it's with the output. At least that will split the program in two, and you can concentrate on half as much code.

    Is there anything screwy with the file delimiters? Did you by chance copy it from, say, Unix to Windows in binary mode? Also, if your output is to a shell, try saving it in a file and seeing if there's any funny characters in there.

    HTH

    1<voice type="cartoon" character="Foghorn Leghorn">That's a joke, son. Yuk it up.</voice>

      OK the problem is with screwy file delimiters. If I open the same file in notepad, then the file appears all in one line with a "rectangle" like character in between my lines. (funny thing is that if I copy paste that "rectange like" character into this window it results into a new line!!)

      I suppose this is a character which textpad and eclipse java editor find OK but notepad and perl do not recognize this as a newline character.

      But now how do I solve this problem ? I am kind of stuck...I have two kinds of newlines in my source code and I have to write a utiltity which has to reads all the lines of the source code and process it. Can I find this character and replace it with the one which is universally regarded as a newline?

      Please help I am confused!

      regards,
      Abhishek.

        Try to find out the hex value of the character first - maybe by using a hex editor on the file(?)

        IMO, it'll differ from the newline hexcode

        Cheers,
        MichaelD

Re: Strange File read problem
by chromatic (Archbishop) on Apr 09, 2003 at 19:08 UTC

    I'm betting you have carriage returns and not newlines at the end of each line. Try this:

    open FILE, "Sample.java" or die "Cannot open file: $!\n"; while (<FILE>) { s/\r/\\r/g; print "$_\n"; } close FILE;
      Yes !!! That did the trick!!!
      I changed the code to
      open FILE, "c:\sample.java"; while(<FILE>) { s/\r/\n/g; print "$_\n"; } close FILE;

      And it printed the file correctly.

      thanks for your help. I was so puzzled. How did you figure it out?

      regards,
      Abhishek.
Re: Strange File read problem
by nothingmuch (Priest) on Apr 09, 2003 at 15:08 UTC
    here are a few ideas for you to try: print @lines; - print can take a list. perhaps the problem is in the loop? unlikely. it's probably in the assignment somewere while (<FILE>){ print } - print the lines as you read them, instead of many at a time. If this works and the prior solution doesn't i suspect something has gone wrong in you assignment to @lines.

    This is very odd behavior indeed. The code works perfectly on my machine, and there is nothing in it to go wrong. Perhaps something is wrong with your terminal?

    -nuffin
    zz zZ Z Z #!perl
      I created a second file like
      package m.y; import n.a.m.e; import i.s; import a.b.h.i.s.h.e.k; import s.r.i.v.a.s.t.a.v.a;

      It worked perfectly as well. so to me it looks that nothing wrong with the terminal or loop.

      I am on W2k, active state perl 5.8.0

      I print the number of lines at the end of the for loop for Sample.java it says number of lines is 1. for sample2.java it says number of lines is 5.

      regards,
      Abhishek.
        There's obviously somethine wrong with the file. If slurping didn't work, and the same stuff works for other files then the OS is at fault. I think. Perl is not sensitive to the content of the file. The readline library - perhaps you can say that, but generally no. I think the lines are simply not there. Rewrite the file to a new one, and see if the problem returns.

        -nuffin
        zz zZ Z Z #!perl
Re: Strange File read problem
by derby (Abbot) on Apr 09, 2003 at 15:33 UTC
    My guess ... the source file you think you're reading is not the source file you're reading. Try changling the relative Sample.java to the full path of what you want.

    -derby

A reply falls below the community's threshold of quality. You may see it by logging in.