×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

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

I’m to the point that I’m actually “asking for directions” .

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

Help…please.

Here is the contents of the read file called “article1.txt”:

This is a test. Line1.
this is a test. Line2.


Here is the contents of the script file called “read.cgi”

#!/usr/bin/perl
open(ARTICLE1, "article1.txt");
while (<ARTICLE1>) # like while($_ = <ARTICLE1>)
{
print; # like print ($_);
}

I created both files on my pc and ftp’ed them into a unix computer at my ISP.
Both files are located in a folder called cgi-bin.

Thanks for the help.

Jerry

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


Hey Jerry,

You don't know how close you are. All you need is this at the top of your program.

print "Content-type: text/html\n\n";

That means anything that you want to print from that line down is going to be text or html.

As for you code

open(ARTICLE1, "</article1.txt") ¦¦ die "Unable to open article1.txt";
while (<ARTICLE1>)
{
print;
}

It will work fine, but it is good practice to use die. And the < means read only.

The ¦¦ are two pipes wich should be near your shift key
and they mean "or" 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 "".

Now that you have figured that one out try somthing a little more fun.

open(FILE,"</article1.txt") or die "unable to open the file";
@data=<FILE>;
close(FILE);
foreach $line(@data) {
@words=split(/\¦/,$line);
print "$words[0]<br>\n";
}

You also need the

print "Content-type: text/html\n\n";

since you will be printing html or text again.

Instead of your text file do somthing like this.

text¦text¦text¦text¦text
_0__1___2___3____4

0,1,2,3,4 fields in your data base.

Each separated by a pipe ¦. When you put all of the text in an @array, you are now able to print individual fields.

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.

The split simply separates the fields in any manner you want. For pipe's you use \¦ between the // and commas you use /,/

the \n means a newline after each rows field 0 is printed.

and now you are one step from a very simple search engine!

Don't forget to use this for the path to perl for good practice also,

#!/usr/local/bin/perl

Tony, aka perlkid






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

(OP)
Tony,

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.

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.

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.

OK....below is a cut/paste of the file called "read.cgi". It is located in a folder called cgi-bin.

---------"read.cgi" is shown below----------------
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
open(ARTICLE1, "article1.txt");
while (<ARTICLE1>) # like while($_ = <ARTICLE1>)
{
print; # like print ($_);
}
close (ARTICLE1);[potomac@ssl cgi-bin]$
---------end "read.cgi"----------

ok, now for the complete error msg that I get when I enter "perl read.cgi"

Illegal character \015 (carriage return) at read.cgi line 2.
(Maybe you didn't strip carriage returns after a network transfer?)

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.

Let me know if more information is needed to solve this error.

I've got three books on PERL...but none of them have a reference to this type of error msg.

thanks for the help....

Jerry

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

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 "text file translation", so it's retained the "^M" 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...)

A couple of ways around this. First, see if there's any kind of "strip ^M from lines when transferring text files" option in your FTP client.

Next option is to use a utility on the Unix server called "dos2unix". Just type "dos2unix read.cgi newread.cgi; mv newread.cgi read.cgi". This will strip the extra characters out of your script.

As a final option, if "dos2unix" isn't available, you could try the following:

sed -e 's/.$//' read.cgi >newread.cgi; mv newread.cgi read.cgi


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.

Hope this helps :)

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

(OP)
Well I tried to find an option in ws_ftp that would strip off the unwanted characters...but I couldn't find an option.

Then I tried dos2unix, but the program doesn't exist on my system.

Finally I tried the "sed -e 's/.$//' read.cgi >newread.cgi; mv newread.cgi read.cgi" and that worked like a champ. Thank you.

Question: Does anyone have a recommended FTP program that will automatically stripe out the unwanted DOS characters of the perl script?

Again, thanks! and I'm on my way toward trying new features of PERL...like the one Tony suggestion above.

jerry

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

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!

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

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.

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

(OP)
Thanks Again,

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.

It's worked fine for me since I ran into this problem, especially since my scripts are short at this point.

thanks again,

jerry

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close