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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Max58kl
    New Member
    • Nov 2007
    • 37

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

    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:
    [CODE=Perl]#!/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;[/CODE]


    I get the following error:

    Code:
    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.

    [CODE=perl]
    #!/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;[/CODE]

    So why will it not recognize the mydatabase.txt file using the I/O box

    Any help would be welcome.

    Regards Max
    Last edited by eWish; Mar 10 '08, 08:30 PM. Reason: Please use [CODE][/CODE] tags rather than [B] tags for code
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    To begin with, I would neaten up and change your script to be:

    [code=perl]
    #!/usr/contrib/bin/perl -w

    use strict;
    use warnings;

    print("Enter file name: "); # Get the file name.
    my $name = <STDIN>;
    chomp($name); # Chomp removes the newline, not chop()

    open(INPUT, $name) or die "Cannot open $name: $!"; # Try to open the file.

    print "Contents of $name:\n";

    while (<INPUT>) # Print the file on the screen.
    {
    print("$_");
    }

    close(INPUT); # Close the file.

    [/code]

    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

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Use the full path to the file

      Comment

      • Max58kl
        New Member
        • Nov 2007
        • 37

        #4
        Originally posted by numberwhun
        To begin with, I would neaten up and change your script to be:

        [code=perl]
        #!/usr/contrib/bin/perl -w

        use strict;
        use warnings;

        print("Enter file name: "); # Get the file name.
        my $name = <STDIN>;
        chomp($name); # Chomp removes the newline, not chop()

        open(INPUT, $name) or die "Cannot open $name: $!"; # Try to open the file.

        print "Contents of $name:\n";

        while (<INPUT>) # Print the file on the screen.
        {
        print("$_");
        }

        close(INPUT); # Close the file.

        [/code]

        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

        Comment

        Working...