On Oct 22, 3:27 pm, [EMAIL PROTECTED] (Ayesha) wrote:
> Hi all
>
> I wrote this code to read a file (in the same directory as the script)
> on Win XP
> ***************************************************************************­************************
> #!/usr/local/bin/perl
> use strict;
> use warnings;
>
> open(READFILE1,"<./Sample_text_file.txt") or die ("Cannot open the
> given file");
> my $record;
>  while ($record = <READFILE1>) {
>       print $record;
>    }
>
> close READFILE1;
>
> #print "Is this even working? \n"
> ***************************************************************************­*************************
> It is gives me output that "Cannot open the given file". However, the
> same program is working on linux/mac platforms, i.e. can open files
> and read them. To check that I have perl correctly installed I added a
> print statement at the end. When I block everything regarding opening
> the file and just have the print statement in the script, the script
> works OK, implying Perl is installed correctly. Can anyone tell me
> what is wrong. Can anyone tell me what am I doing wrong here? It seems
> some Windows specific thing.

You are asking *us* why Perl can't open the file, before you ask
*Perl* why it can't open the file.  That is most illogical...

Your die() message should include the $! variable, which contains the
last operating system error.  It will tell you why the file could not
be opened.  Change your die() to:

die "Cannot open the given file: $!";

Not relevant to the problem at hand, but you should also be using
lexical filehandles instead of global barewords, and get into the
habbit of using the three-argument form of open:

open my $READFILE1, '<', './Sample_text_file.txt' or
   die "Cannot open the given file: $!";

Paul Lalli


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to