Boberts has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to Perl and I am trying to count the word frequency of a given txt document from a URL using the CGI stuff, I have this word frequency count which I took from the Learning Perl textbook but I don't know to get it to take it from a URL, any suggestions would be great

#!/usr/bin/perl use CGI qw(-utf-8 :all *table); use LWP::Simple qw(get); binmode(STDOUT, ":encoding(utf-8)"); print header(-charset=>'utf-8'), "\n", start_html({-title=>'URL retrieval', -author=>'Bob'}), "\n"; if (param('URL')) { my(@words, %count, $word); @words = get(param('URL')); foreach $word (@words) { $count {word} += 1; } foreach $word (keys %count) { print "$word was seen $count{word} times.\n"; } print start_table ({-border=>1}); print("Word Count Statistics for ".param('URL')), "\n"; print caption ("Frequency"); print end_table ; } print h1("URL retrieval"), "\n"; print start_form({-method=>"POST"}); print label("URL: "); print textfield({-name=>'URL', -size=>200}), "\n"; print br(), "\n"; print submit({-name=>'submit', -value=>'Process'}), "\n"; print end_form, end_html;
Discipulus added correct ending code tag 28 Feb 2019 20:15 GMT+1

Replies are listed 'Best First'.
Re: Word frequency from URL Doc
by hippo (Archbishop) on Feb 28, 2019 at 15:43 UTC
    @words = get(param('URL'));

    LWP::Simple::get returns a scalar, not an array. You could convert the scalar into an array with eg. split.

Re: Word frequency from URL Doc
by Lotus1 (Vicar) on Feb 28, 2019 at 15:45 UTC

    Make sure to verify your post looks right after you click preview. You can edit until it does before you submit. You need to end your code section with </code>.

    Try the example given in the synopsis for LWP::Simple.

    use LWP::Simple; $content = get("http://www.sn.no/"); die "Couldn't get it!" unless defined $content;