www.webdeveloper.com
Results 1 to 4 of 4

Thread: Reading from files...

  1. #1
    Join Date
    May 2005
    Posts
    502

    Reading from files...

    A while ago I came on here and someone gave me a perl script which reads a text file and displays it on a web page. I need to edit it to only display 25 lines at a time, with links to scroll through the lines.

    I've already written it in ASP, but I'm useless with Perl I used a variable in the query string to tell the script which page to display, and therefore which lines to display.

    Can anyone give me some pointers please?

    PHP Code:
    #! /usr/bin/perl

    use LWP::UserAgent;

    my $ua = new LWP::UserAgent;
    my $req = new HTTP::Request 'GET','http://www.mydomain.co.uk/text.txt';
    my $res $ua->request($req);

    # check that a http error did not occur
    my $file $res->content;
    if (
    $res->is_error)
    $file 'something went wrong: ' $res->status_line;
    }

    my $html = <<_END_HTML_;
    <
    html>
      <
    head>
        <
    title>My Page</title>
        <
    link href="style.css" rel="stylesheet" type="text/css">
      </
    head>
      <
    body>Here are the contents of the file:
        <
    p>$file</p>
      </
    body>
    </
    html>
    _END_HTML_

    print "Content-type: text/html\n\n"$html

  2. #2
    Join Date
    Sep 2004
    Posts
    361
    Try the following:
    call like this: scriptname?numoflines=25


    #!/usr/bin/perl
    use CGI qw(:standard);
    use LWP::UserAgent;

    print header;

    # Create a user agent object
    $base_url ="http://www.mydomain.co.uk/text.txt";

    use LWP::UserAgent;
    my $ua = new LWP::UserAgent;
    $ua->agent("Lathi.net KDA/0.1" . $ua->agent);

    # Create a request
    my $req = new HTTP::Request GET => $base_url;
    $req->content_type('application/x-www-form-urlencoded');
    $req->content('match=www&errors=0');

    # Pass request to the user agent and get a response back
    my $res = $ua->request($req);

    # Check the outcome of the response
    unless ($res->is_success) {
    $res->status_line, "\n";
    }

    @words = split(/\n/, $res->content);
    foreach $line (@words){

    #print title line
    #if($line =~ /<title>/g) {
    #print qq|TITLE:$line<BR>\n|;
    #}
    print qq|line $x:$line<BR>\n|;
    last if $x == param(numoflines);
    $x++;
    }

    If you want only certain lines you'll have to use Regexp

    -Bill

  3. #3
    Join Date
    May 2005
    Posts
    502
    Fantastic, thank you very much!

  4. #4
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    $ua->agent("Lathi.net KDA/0.1" . $ua->agent);

    ...

    $req->content_type('application/x-www-form-urlencoded');
    $req->content('match=www&errors=0');
    What is the point of all of this?!

    cusimar9, here's what you would need to do after you've received the response.
    Code:
    my $LINES_PER_PAGE = 25;
    my $page = $cgi->param('page');
    $page = 1 if not defined($page) or $page =~ /\D/ or $page < 1;
    my @lines = ($res->content =~ /^(.*)/gm);
    
    for my $line_n (($page - 1) * $LINES_PER_PAGE .. $page * $LINES_PER_PAGE - 1) {
      print $lines[ $line_n ];
    }
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles