424,279 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,279 IT Pros & Developers. It's quick & easy.

Trying to access data and print it to the screen using Perl Builders I/O Window

P: 37
Trying to access data and print it to the screen using Perl Builders I/O Window
--------------------------------------------------------------------------------

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
  1. #!/usr/contrib/bin/perl -w
  2.  
  3. print "Enter file name: "; # Get the file name.
  4. $name = <STDIN>;
  5. chop($name);
  6.  
  7. open(INPUT, $name) || # Try to open the file.
  8. die "Cannot open $name";
  9.  
  10. print "Contents of $name:\n";
  11.  
  12. while ($line = <INPUT>) # Print the file on the screen.
  13. {
  14. print $line;
  15. }
  16.  
  17. close(INPUT); # Close the file.
  18.  
  19. exit 0;

I get the following error:

Expand|Select|Wrap|Line Numbers
  1. 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
  1. #!/usr/bin/perl
  2. ##Getting current directory information
  3. use Cwd ;
  4. $directory = cwd ;
  5.  
  6. ##Setting variable to database name
  7. $file = "$directory/mydatabase.txt" ;
  8.  
  9. ##Opening up the file for reading
  10. open ( CTRL, $file ) ;
  11.  
  12. ##reading the contents of the file and
  13. ##putting it to the array @fruit
  14. @fruit = <CTRL> ;
  15.  
  16. ##closing the file
  17. close ( CTRL ) ;
  18.  
  19. ##running through each item in @fruit
  20. foreach ( @fruit ) 
  21.  
  22. ##taking of the return of each item
  23. chomp ( $_ ) ;
  24.  
  25. ##printing each item on a new line
  26. print "$_\n" ;
  27. exit;
So why will it not recognize the mydatabase.txt file using the I/O box

Any help would be welcome.

Regards Max
Mar 10 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
To begin with, I would neaten up and change your script to be:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/contrib/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. print("Enter file name: ");      # Get the file name.
  7. my $name = <STDIN>;
  8. chomp($name);  # Chomp removes the newline, not chop()
  9.  
  10. open(INPUT, $name) or die "Cannot open $name:  $!";   # Try to open the file.
  11.  
  12. print "Contents of $name:\n";
  13.  
  14. while (<INPUT>) # Print the file on the screen.
  15. {
  16.      print("$_");
  17. }
  18.  
  19. close(INPUT);      # Close the file.
  20.  
  21.  
As for why it cannot find the file, I am not sure. Is the file there? Do you have permissions to at least read it? The "$!" in the die statement of the open will give you the error that is produced when the script tries to open the file.

Regards,

Jeff
Mar 10 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Use the full path to the file
Mar 10 '08 #3

P: 37
To begin with, I would neaten up and change your script to be:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/contrib/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. print("Enter file name: ");      # Get the file name.
  7. my $name = <STDIN>;
  8. chomp($name);  # Chomp removes the newline, not chop()
  9.  
  10. open(INPUT, $name) or die "Cannot open $name:  $!";   # Try to open the file.
  11.  
  12. print "Contents of $name:\n";
  13.  
  14. while (<INPUT>) # Print the file on the screen.
  15. {
  16.      print("$_");
  17. }
  18.  
  19. close(INPUT);      # Close the file.
  20.  
  21.  
As for why it cannot find the file, I am not sure. Is the file there? Do you have permissions to at least read it? The "$!" in the die statement of the open will give you the error that is produced when the script tries to open the file.

Regards,

Jeff

Thanks for the advice!
The problem was the word, contrib in the path to the perl exe

#!/usr/contrib/bin/perl -w

I tried

#!/usr/bin/perl -w

and the file information I needed printed to the screen.

Regards Max
Mar 11 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.