--------------------------------------------------------------------------------
Hi
I am using a program called Perl Builder which is
an Integrated Development Environment for Perl scripting.
I am trying to access the data in a txt file and then print
the results to the screen.
The procedure for doing this is write a script then click on the
Run button, which takes you to an interactive I/O window.
You then type information into a I/O box Click Enter and the
content should display.
Using the following script:
Expand|Select|Wrap|Line Numbers
- #!/usr/contrib/bin/perl -w
- print "Enter file name: "; # Get the file name.
- $name = <STDIN>;
- chop($name);
- open(INPUT, $name) || # Try to open the file.
- die "Cannot open $name";
- print "Contents of $name:\n";
- while ($line = <INPUT>) # Print the file on the screen.
- {
- print $line;
- }
- close(INPUT); # Close the file.
- exit 0;
I get the following error:
Expand|Select|Wrap|Line Numbers
- Enter file name: Cannot open .mydatabase.txt at test line 18, <STDIN> line 1.
I thought maybe it had something to do with the file not being in the Working directory.
But when I try to access the same file using a file handle and the Cwd module, the content displays as it should.
Expand|Select|Wrap|Line Numbers
- #!/usr/bin/perl
- ##Getting current directory information
- use Cwd ;
- $directory = cwd ;
- ##Setting variable to database name
- $file = "$directory/mydatabase.txt" ;
- ##Opening up the file for reading
- open ( CTRL, $file ) ;
- ##reading the contents of the file and
- ##putting it to the array @fruit
- @fruit = <CTRL> ;
- ##closing the file
- close ( CTRL ) ;
- ##running through each item in @fruit
- foreach ( @fruit )
- {
- ##taking of the return of each item
- chomp ( $_ ) ;
- ##printing each item on a new line
- print "$_\n" ;
- }
- exit;
Any help would be welcome.
Regards Max