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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



mwaf
Deleted

Jun 12, 2000, 11:14 AM

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

If I use a simple script:

Open(IN,"filename");
$whatever = <IN>;
Close(IN);

If filename looks like this for example:
-------------
2468763
asdf
-------------
$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:

open(OUT,">/whatever/whatever.html");
print OUT "goodbye world!";
close(OUT);


Jasmine
Administrator / Moderator

Jun 12, 2000, 11:50 AM

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

Excerpted from Turk Scripts' "Some Perl Tips" article at http://tlc.perlarchive.com/00056/03.shtml :

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,
Example:

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


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

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";
</pre><HR></BLOCKQUOTE>

Hope this helps!


mwaf
Deleted

Jun 12, 2000, 12:44 PM

Post #3 of 3 (323 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