Hello wrkrbeee,

I think Discipulus provided this sample code to demonstrate a useful approach which you can adapt to your particular needs. If you want to process the read-in lines in the calling code (your “Primary loop”) rather than in the callback function, then you need to store the lines in a shared variable rather than print them in sub head_only. There is an additional complication: the last line read from the current chunk of data may not be complete, so you need to check for a trailing newline and handle its absence appropriately:

use strict; use warnings; use LWP::UserAgent; use feature 'state'; use constant LINES => 30; my @pages = ('http://www.perlmonks.org', 'http://perldoc.org'); my $ua = LWP::UserAgent->new; my $read_lines = 0; my @lines; for my $url (@pages) { my $response = $ua->get($url, ':content_cb' => \&head_only); if ($response->is_success) { print "$_\n" for @lines; print +("=" x 70), "\n"; } else { die $response->status_line; } $read_lines = 0; @lines = (); } sub head_only { state $last_line_incomplete = 0; my ($data, $resp, $protocol) = @_; my @all_lines = split "\n", $data; warn "LINES: ", scalar @all_lines, "\n"; if ($last_line_incomplete) { my $first_line = shift @all_lines; $lines[-1] .= $first_line; } for my $line (@all_lines) { if (++$read_lines > LINES) { $last_line_incomplete = 0; die; } push @lines, "line $read_lines: $line"; } $last_line_incomplete = $data !~ /\n$/; }
print +("=" x 70), "\n"; #what is this?

The x operator creates a string of 70 equals characters concatenated together:

======================================================================

— see perlop#Multiplicative-Operators. The plus sign is there to prevent the Perl parser from thinking that the parentheses contain the entire argument list to the print function — see print.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: Split web page, first 30 lines only -- :content_cb trick by Athanasius
in thread Split file, first 30 lines only by wrkrbeee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.