On Oct 23, 6:55 pm, [EMAIL PROTECTED] (Paul Lalli) wrote:
> 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- Hide quoted text -
>
> - Show quoted text -

Some suggestions:
1. Instead of using $! for displaying error messages, use $^E. U will
get more descriptive error messages.
2. Quickly going thru this digest i observerd usage of '\' in double
quotes, this is not recommended. always escape '\' in double quotes
like this "\\". Heres an example where things can go wrong, lets say
ur file/directory name starts with 'n' and u say somthing like open
FILE, "<.\newFile"; "\n" is interpreted as special character by perl
or for that matter any other language. Correct way would be "open
FILE, ".\\newFile";

~emptee.


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


Reply via email to