Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error: Illegal character \015 (carriage return) at read.cgi line 2.

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
I just bought a book on Perl-5 for web professionals and thought I'd learn the language. I've run into a problem on the first example program...can't believe it. I've wasted a lot of hours on what is most likely something real stupid.<br>
<br>
I’m to the point that I’m actually “asking for directions” . <br>
<br>
The example program is real real basic. The script example simply opens an ordinary text file and prints it on the screen. (later I’ll try and do via the web…right now it’s just supposed to run via a dumb terminal on a unix plateform).<br>
<br>
Help…please.<br>
<br>
Here is the contents of the read file called “article1.txt”:<br>
<br>
This is a test. Line1.<br>
this is a test. Line2.<br>
<br>
<br>
Here is the contents of the script file called “read.cgi”<br>
<br>
#!/usr/bin/perl<br>
open(ARTICLE1, &quot;article1.txt&quot;);<br>
while (&lt;ARTICLE1&gt;) # like while($_ = &lt;ARTICLE1&gt;)<br>
{<br>
print; # like print ($_);<br>
}<br>
<br>
I created both files on my pc and ftp’ed them into a unix computer at my ISP.<br>
Both files are located in a folder called cgi-bin.<br>
<br>
Thanks for the help.<br>
<br>
Jerry<br>

 
<br>
Hey Jerry,<br>
<br>
You don't know how close you are. All you need is this at the top of your program.<br>
<br>
print &quot;Content-type: text/html\n\n&quot;;<br>
<br>
That means anything that you want to print from that line down is going to be text or html.<br>
<br>
As for you code <br>
<br>
open(ARTICLE1, &quot;&lt;/article1.txt&quot;) ¦¦ die &quot;Unable to open article1.txt&quot;;<br>
while (&lt;ARTICLE1&gt;)<br>
{<br>
print;<br>
}<br>
<br>
It will work fine, but it is good practice to use die. And the &lt; means read only.<br>
<br>
The ¦¦ are two pipes wich should be near your shift key<br>
and they mean &quot;or&quot; in perl. I forgot what die means but you use it to print a message or execute a command when you are unable to open a file. In this case you will print a message between the &quot;&quot;.<br>
<br>
Now that you have figured that one out try somthing a little more fun.<br>
<br>
open(FILE,&quot;&lt;/article1.txt&quot;) or die &quot;unable to open the file&quot;;<br>
@data=&lt;FILE&gt;;<br>
close(FILE);<br>
foreach $line(@data) {<br>
@words=split(/\¦/,$line);<br>
print &quot;$words[0]&lt;br&gt;\n&quot;;<br>
}<br>
<br>
You also need the <br>
<br>
print &quot;Content-type: text/html\n\n&quot;;<br>
<br>
since you will be printing html or text again.<br>
<br>
Instead of your text file do somthing like this.<br>
<br>
text¦text¦text¦text¦text<br>
_0__1___2___3____4<br>
<br>
0,1,2,3,4 fields in your data base.<br>
<br>
Each separated by a pipe ¦. When you put all of the text in an @array, you are now able to print individual fields.<br>
<br>
Like words[0], that will print the first field for every row in the data file. So make more that one line/row in your data file.<br>
<br>
The split simply separates the fields in any manner you want. For pipe's you use \¦ between the // and commas you use /,/<br>
<br>
the \n means a newline after each rows field 0 is printed.<br>
<br>
and now you are one step from a very simple search engine!<br>
<br>
Don't forget to use this for the path to perl for good practice also,<br>
<br>
#!/usr/local/bin/perl<br>
<br>
Tony, aka perlkid<br>
<br>
<br>
<br>
<br>
<br>
<br>

 
Tony,<br>
<br>
Thanks for the help and quick response....however it didn't seem to work. I'll show you what I did later in the post...but my gut tells me that it has something to do with either unix and/or the first line of the program. <br>
<br>
I know the first line of a Perl program has to be entered in a certain way. The error appears to be pointing at the 2nd line...but that could be related to the first line??...please help.<br>
<br>
Anyway, my gut tells me I'm real real close to getting PERL to work for me. I also can't wait to try some of the other functionality that's available via PERL...If I could just get past the 2nd line.<br>
<br>
OK....below is a cut/paste of the file called &quot;read.cgi&quot;. It is located in a folder called cgi-bin.<br>
<br>
---------&quot;read.cgi&quot; is shown below----------------<br>
#!/usr/local/bin/perl<br>
print &quot;Content-type: text/html\n\n&quot;;<br>
open(ARTICLE1, &quot;article1.txt&quot;);<br>
while (&lt;ARTICLE1&gt;) # like while($_ = &lt;ARTICLE1&gt;)<br>
{<br>
print; # like print ($_);<br>
}<br>
close (ARTICLE1);[potomac@ssl cgi-bin]$<br>
---------end &quot;read.cgi&quot;----------<br>
<br>
ok, now for the complete error msg that I get when I enter &quot;perl read.cgi&quot;<br>
<br>
Illegal character \015 (carriage return) at read.cgi line 2.<br>
(Maybe you didn't strip carriage returns after a network transfer?)<br>
<br>
The error msg also references the network transfer process. I created the file on my home PC and use ws_ftp to transfer the file from my home computer to my ISP which I've got a standard unix account.<br>
<br>
Let me know if more information is needed to solve this error.<br>
<br>
I've got three books on PERL...but none of them have a reference to this type of error msg.<br>
<br>
thanks for the help....<br>
<br>
Jerry<br>

 
The error message is a big clue - as you've created the file in Windows, each line has got an extra carriage return (^M) character at the end of it. When you transferred the file, it's been transferred without any kind of &quot;text file translation&quot;, so it's retained the &quot;^M&quot; characters. This is making Unix perl barf :( (Oh yeah, there's probably also an end of file marker - ^Z - at the end of the file...)<br>
<br>
A couple of ways around this. First, see if there's any kind of &quot;strip ^M from lines when transferring text files&quot; option in your FTP client.<br>
<br>
Next option is to use a utility on the Unix server called &quot;<FONT FACE=monospace>dos2unix</font>&quot;. Just type &quot;<FONT FACE=monospace>dos2unix read.cgi newread.cgi; mv newread.cgi read.cgi</font>&quot;. This will strip the extra characters out of your script.<br>
<br>
As a final option, if &quot;<FONT FACE=monospace>dos2unix</font>&quot; isn't available, you could try the following:<br>
<FONT FACE=monospace><br>
sed -e 's/.$//' read.cgi &gt;newread.cgi; mv newread.cgi read.cgi<br>
</font><br>
<br>
This strips the final character off every line in read.cgi, and sends it's output to newread.cgi. It then moves newread.cgi back to read.cgi. This will have the effect of stripping the unwanted ^M and ^Z characters out of your script.<br>
<br>
Hope this helps :)
 
Well I tried to find an option in ws_ftp that would strip off the unwanted characters...but I couldn't find an option.<br>
<br>
Then I tried dos2unix, but the program doesn't exist on my system.<br>
<br>
Finally I tried the &quot;sed -e 's/.$//' read.cgi &gt;newread.cgi; mv newread.cgi read.cgi&quot; and that worked like a champ. Thank you.<br>
<br>
Question: Does anyone have a recommended FTP program that will automatically stripe out the unwanted DOS characters of the perl script?<br>
<br>
Again, thanks! and I'm on my way toward trying new features of PERL...like the one Tony suggestion above.<br>
<br>
jerry
 
That's basically handled in an editor. I use UltraEdit, but Programmer's File Editor and several other text editors will allow you to open as a certain type file, or to convert one to another automagically. Thats' the best way to handle it. Good luck!
 
When I started out, I just used Pico via the telnet to write my progs.. Never really had that specific problem your having. If all else fails, worth a try.
 
Thanks Again,<br>
<br>
Although I haven't used Pico in years I've gone back to using this editor since running into the carriage return problem. Pico was my editor of choice years and years ago. I think the last time I used it I was writing assembly code for a PDP-11/70......can't believe it's still around.<br>
<br>
It's worked fine for me since I ran into this problem, especially since my scripts are short at this point.<br>
<br>
thanks again,<br>
<br>
jerry<br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top