CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Open, reading data past the first line?



Jun 12, 2000, 11:14 AM

Post #1 of 3 (623 views)
Open, reading data past the first line? Can't Post

If I use a simple script:

$whatever = <IN>;

If filename looks like this for example:
$whatever will be 2468763 and 'asdf' will drop out.

This doesn't bother me as I can have the content of filename as "2468763\nasdf" or "2468763<br>asdf" (when doing with HTML as I mostly do) but I would still want to be able to read lines past line1, this way I could store more stuff in one file and not have 20 files with one short line.

Also, can(or actually how) I create a html file? (didn't get it to work with a script like this:

print OUT "goodbye world!";

Administrator / Moderator

Jun 12, 2000, 11:50 AM

Post #2 of 3 (623 views)
Re: Open, reading data past the first line? [In reply to] Can't Post

Excerpted from Turk Scripts' "Some Perl Tips" article at :

Instead of reading a file line by line, you might want to read the whole file to a variable at one step. This is useful especially if you are reading html files. If you open a file and try reading from that file, perl reads only until the first [enter] character and stops. The reason of this behavior is that the default "input record separator" in perl is the [enter] character. This separator is defined in the special variable $/. By default $/ is equal to "\n". If you undefine this variable using undef, you can read the file at one step,

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

undef $/;
open(FILE, "data.htm");
$html = <FILE>;

About your second question, make sure that the directory that you want to output files to exist before you try to write a file to that directory. A good habit to get into is to check to make sure that file openings and closings are successful, like so:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

open(OUT,">/whatever/whatever.html") or die "Couldn't open /whatever/whatever.html for writing. $!\n";
print OUT "goodbye world!";
close(OUT) or die "Couldn't close file /whatever/whatever.html $!\n";

Hope this helps!


Jun 12, 2000, 12:44 PM

Post #3 of 3 (623 views)
Re: Open, reading data past the first line? [In reply to] Can't Post

Thanks! I'll give it a try...


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives