Issues reading a simple text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tbonell
    New Member
    • Mar 2010
    • 2

    #1

    Issues reading a simple text file

    Hi,

    Thank you in advance for your help.

    I am new to PERL, just a couple of days, but reading a lot. I found your site very exciting and informative.

    I am trying to read a text file with a key 10 to 120 numbers in a single lime into an array. But I am getting a 500 error and do't know exactly what is the error. I am confused on how to make the code read from my web server (the path).
    Perhaps my approach is incorrect!.

    my code is as follows:

    Code:
    #!/usr/bin/perl -w
    # readtext.pl
    
    use strict;
    use CGI ':standard';
    
    $filename = "//mypage.com/key.txt";
    
    open (filename) || die "Couldn't open $filename: $!n";
    my @params=<filename>;
    
    print
    	   header(),
    	   start_html(
    	        -title => 'Welcome!',
    	        -text  => '#520063'
    	        ),
    	        h1("the key is: $@params"),
    	        end_html();
    ]
    Thanks again

    Tony
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Add these use statements.
    Code:
    use warnings;
    use CGI::Carp qw(fatalsToBrowser);
    Remove the -w switch

    Lines 7 and 9 have problems which should become apparent after adding the use statements.

    Line 18 also has a problem, but lets start with the more important issues.

    Comment

    • tbonell
      New Member
      • Mar 2010
      • 2

      #3
      Ron,

      Thank you for your fast response, and comments.

      I am still confuse on how to address the path for files under my web server. Could you please recommend any book or web page to read.

      I am getting the following error after entering the two lines you suggested. Please see my code and error code as follows:

      Error:
      open //bcomputingcom.i page.com/cgi-bin/key.txt: No such file or directory at /home/users/web/b1789/ipg.bcomputingc om/cgi-bin/readtext.pl line 12.

      #!/usr/bin/perl
      # readtext.pl

      use strict;
      use CGI ':standard';
      use warnings;

      use CGI::Carp qw(fatalsToBrow ser);

      my $inFile = "//bcomputingcom.i page.com/cgi-bin/key.txt";

      open(IN, $inFile) or die "open $inFile: $!";
      my $line=<IN>;

      print
      header(),
      start_html(
      -title => 'Welcome!',
      -text => '#520063'
      ),
      h1("the key is: $line"),
      end_html();
      Last edited by tbonell; Apr 1 '10, 12:34 AM. Reason: spelling

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        The open function expects a relative or absolute system path, not a url or unc path. The path you're using is not a system path.

        Comment

        Working...