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:
Help Needed..Perl Beginner


New User

Jul 29, 2011, 2:34 AM

Post #1 of 2 (1731 views)
Help Needed..Perl Beginner Can't Post

Please tell what is the problem with this code. I'm using Komodo Edit.


$FH = "filehandle";
open (FH, "test.txt") or die "Can't open the file!";
for $line (<FH>) {
print $line;
close FH;

I'm getting this as an output:

Can't open the file! at C:\Users\avashish\Documents\new folder\perl codes\ line 2.

2) I could not print spaces using - print <br \> . Please tell me the reason for this?

Veteran / Moderator

Jul 29, 2011, 6:19 AM

Post #2 of 2 (1714 views)
Re: [akshay] Help Needed..Perl Beginner [In reply to] Can't Post

1a) The die statement should include the reason it failed, which can be done by adding $! to the statement and it should include what you're were trying to open.

1b) Use a while loop instead of the for loop.

1c) You should be using a lexical var for the filehandle instead of the bareword and you should be using the 3 arg form of open.

2) Surround your code with code tags.

open my $FH, '<', 'test.txt' or die "Can't open 'test.txt' $!"; 
while ( my $line = <$FH> ) {
print $line;
close $FH;


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

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