Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

File Handles Chapter(not homework)

by Parmenides (Initiate)
on Aug 02, 2012 at 07:34 UTC ( #984964=perlquestion: print w/ replies, xml ) Need Help??
Parmenides has asked for the wisdom of the Perl Monks concerning the following question:

For fun, I am working my way through a perl text book and I have a question about how to make one of the chapter assignments better. Topic: The chapter that I just read was about filehandles. My chapter assignment: Write a program that opens a file with STDIN and then writes each line of the file proceded by the file's name and a colon. My question: Upon reading this chapter assignment, I immediately thought, huh. I don't know how to write the program line by line. After scowring the book for something I might have missed, I finally gave in and took a peek at the answer. The books answer: assume that each line ends with a \n. Since that is just a bit rediculous, how do I parse a file line by line?

Comment on File Handles Chapter(not homework)
Re: File Handles Chapter(not homework)
by choroba (Abbot) on Aug 02, 2012 at 07:58 UTC
    To read a file line by line, you typically use
    while (my $line = <>) { # do something with the line }
    If this construct is missing in your textbook, somebody probably tore some pages from the book.

    If you meant "preceded" by "proceded", you might use $ARGV: look it up in perlvar.

Re: File Handles Chapter(not homework)
by moritz (Cardinal) on Aug 02, 2012 at 08:01 UTC
    Write a program that opens a file with STDIN

    What does that mean? Open a file with a name read from STDIN? If so,

    use autodie; my $filename = <STDIN>; chomp $filename; # remove trailing newline character open my $handle, '>', $filename; # ^ if you want to write to that file
    how do I parse a file line by line?
    use autodie; open my $handle, '<', 'yourfile.txt'; while (my $line = <$handle>) { chomp $line; # remove trailing newline # do something with $line here. }

    See also: perlintro, perlopentut.

Re: File Handles Chapter(not homework)
by Anonymous Monk on Aug 02, 2012 at 08:04 UTC
Re: File Handles Chapter(not homework)
by jwkrahn (Monsignor) on Aug 02, 2012 at 10:01 UTC
    Write a program that opens a file with STDIN and then writes each line of the file proceded by the file's name and a colon.

    Strictly speaking, Standard IO is a streaming protocol and there is no meta-data associated with it (like name, size or date) so you must have been asked a different question.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://984964]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-08-29 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls