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

Hello, I'm new to perlmonks. So little introduction: I maintain Unix and Citrix application and using perl now for about 4 years. Perl is THE language for me. Problem: when i click on an url, i would like to obtain the value of it (fetch url). I did a lot of searching, and found hints but ..... Goal: create a page which shows the most viewed site on top. I'm reading a csv file, and publish it in a web Snippet:
sub print_prompt { my($query) = @_; open( INFILE, "$conf_file" ) or die "$!"; while (<INFILE>) { chomp; next if 0 == length($_); # Solves "Use o +f uninitialized value in .......... " my @elem = split ","; print("$elem[0]\n") if $debug; #description print("$elem[1]\n") if $debug; #URL print("$elem[2]\n") if $debug; #nr how many tim +es viewed the URL print "<a href=$elem[1] target=\"_blank\" >$elem[0]</a><br>"; } close INFILE; }
Question: must it be i a form?
Must I use the "my $query = new CGI;" codes
I thought something whith:

print "This site : $ENV{'REQUEST_URI'}
\n";
print "Previous site : $ENV{'HTTP_REFERER'}
\n";
Thanks in advance,
Perlboer

Replies are listed 'Best First'.
Re: fetch url
by Joost (Canon) on May 27, 2005 at 10:30 UTC
    You want to:
    Fetch an url. (Or so you say)
    use LWP::Simple
    Read a CSV file.
    It's not as easy as you think. use Text::xSV
    Render dynamic HTML
    If your script gets more complicated you might want to try a templating module. there are loads of them on CPAN

    Generic notes: I think in this case

    next unless $_;
    is much neater than
    next if 0 == length($_);
    but if you need that line, you're not reading CSV anyway.

    I really have no idea what you mean with "must it be i a form"? There is no form.

    As for "Must I use the "my $query = new CGI;" codes" - well, if you're going to parse input, yes. Also, if you use the CGI modules methods $query->request_uri and $query->referer(), that'll trap misspellings of the %ENV entries.

Re: fetch url
by dorward (Curate) on May 27, 2005 at 10:32 UTC

    Click on a URL where? Obtain the value where? (i.e. what is the relationship between the software recording the click, the software making the http request, and the URL being requested (is it your site? various different 3rd party sites? a site that is generated by one of the afore mentioned pieces of software?)

    A form is used in an HTML document when you want the user to enter some data - pretty much anytime you don't want to give them a list of choices (as they can be represented by regular links). From your description it doesn't sound like you need that.

    CGI.pm is a nice, mature bit of software. While you can read $ENV directly, using the CGI.pm interface will probably be more elegent for basic functions, and protect you from silly mistakes in less trivial functions (such as parsing query strings). So use CGI.pm.

      Thank you for the reaction.
      Sorry if it isn't clear. (first time you know)

      Let I put it this way:
      I have a "start_page" with +/- 100 urls on it.
      When I hit one of these url's, i would like to go to the url
      (could be www.perlmonks.org), and I would like to log in a file that "www.perlmonks.org" was viewed.
      input to load the "start_page" (.conf file):

      desc,url,hitcounts
      nu,http://www.nu.nl,0
      cvs,http://www.cvshome.org/,1
      perl tutorial,http://www.steve.gb.com/perl/index.html,4

      Than i'm able to count with that url.
      Than i'm able to add 1 to "hitcounts".

        Don't worry about it. You clearly put some effort into describing the problem, and given that you are in the Netherlands, I'm guessing English isn't your first language. Its laziness that most people hate around here. We don't mind helping you refine a question if you put the effort in.

        First I would suggest that you use a Real Database instead of a CSV file. I would suspect that using a CSV file could lead to file locking and efficiency issues. That said, you can use the DBI interface to CSV which looks like it solves the file locking problem. I've never used it myself, so I can't be certain it would work nicely.

        As for the actual problem - when the user clicks on their link, their browser will go to it. If that link goes to a different server, the browser won't send any information back to your server so you can't know that they clicked on that link.

        The solution is to have the link point to your server, log the request, and then perform an HTTP redirect.

        To do this you have your script change:

        <a href="http://www.cvshome.org/">CVS</a>

        To:

        <a href="my.cgi?gotoSite=1">CVS</a>

        Where 1 is a unique identifier for the link. You then look up that entry in the database (in my.cgi), log the visit, then redirect (the CGI.pm perldoc tells you how) to the URL stored in your database.

        You should use identifiers and database lookups rather then that URL itself for two main reasons. First, it prevents people using your domain to cloak their dodgy URL, and second it saves you from the effort of having to URL Encode and HTML Encode the string when you generate the webpage.