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

Help!!!! I need to make a cgi script that updates an html file with a link to a file I tell it to. But first it need to send the current link in that file to one of the subcategories' files(It's a NEWS section of my page, what I am doing is sending the current article in the headlines to its corresponding subcategory. So I created a form wich asks me for the new file name, for the link.,the headline and a little excerpt from the article (1 and a half lines). But It won't work. I have been studying PERL for 3 months and I thought it would somthin simple but it wasn't. PLease somebody help, If I don't finish this srcipt by friday I won't be able to go on vacation next week.

Replies are listed 'Best First'.
Re: My vacation depends on it
by arturo (Vicar) on Apr 03, 2001 at 22:26 UTC

    Yes it is fairly simple in Perl.

    Maybe you could post some code and ask a specific question? This looks a lot like "do my work for me", and that probably isn't going to happen. We're happy to help you with specific questions about how to surmount specific problems. We're not here to design and write your code for you.

    good luck!

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      OK here's my code so far....
      #!/usr/bin/perl -w print "Content-type: text/html\n\n"; #Here I am getting the values from #the GET form. foreach $pair (split('&', $form)) { if ($pair =~ /(.*)=(.*)/) { # found key=value; ($key,$value) = ($1,$2); # get key, value. $value =~ s/\+/ /g; # substitute spaces for + signs. $value =~ s/%(..)/pack('c',hex($1))/eg; $inputs{$key} = $value; # Create Associative Array. } } #This next assigns assigns the values to variables $nombre = $inputs{'nombre'}; $encabezado = $inputs{'encabezado'}; $cat = $inputs{'cat'}; #Now I created this rudimentary statements that by seeing #which categ +ory I chose, select which file I am going to #move the link to. if($cat == t){ $catfile = "notitec.html"; } if($cat == a){ $catfile = "notiagro.html"; } if($cat == d){ $catfile = "notideportes.html"; } if($cat == h){ $catfile = "notihogar.html"; } if($cat == r){ $catfile = "notirecrea.html"; } if($cat == s){ $catfile = "notisociales.html"; } #Now I open the current headline and read it to an array #but changing + locally the separator character so I don't #get a straight line of h +tml code { $current = "/var/www/html/notidia.html"; open(DIA, "< $current") or die("Couldn't open $current\n"); local $/ = >; @file_data = <DIA>; close DIA; } #now open the category file and insert the previous data $catnoticia = "/var/www/html/$catfile"; open(NOT, "> $catnews") or die("Couldn't open $catnews\n"); foreach $line (@file_data) { print "$line"; } close NOT; #that's what I got so far. I think I got the idea right but #didn't ex +press it correctly on the code (duh) #If I have some variable names wrong its because I changed #the names +to english or something understandable to you so #you can get a bette +r idea.

        OK, here are some general pointers:

        1. use CGI qw(:param) to process your form data; handrolled stuff is messy, hard to maintain, and insecure. See use CGI or die; for more about why.
        2. use strict always and run under -w, the latter especially when you're developing. Perl will catch all sorts of errors you won't. As things stand, your script could fail for lots of reasons in lots of places and you'd never know why.

        our own Ovid has written a CGI tutorial guide, check his homenode for the link (follow the link in this paragraph)

        Now, some specifics:

        What your ifs are doing is done more maintainably with a hash. I like to do this at the top of complex scripts:

        my %actions = ( option1 => \&process_option1, option2=>\&processoption2 ); my $option = param('option'); $action{$option}->() or default_action(); exit;

        And so forth. That nasty-looking \& thing creates references to subroutines, which are called by the $action{$option} line. Here, what you should do is create a hash whose keys are as you have them, and whose values are the files that each kind of action works on:

        my %file_hash = ( a=>'notiagro.html', d=>'notideportes.html' # and so on ); my $catfile = $file_hash{$cat} || 'default value';

        I know that doesn't get you everything you need. You have a start on the harder parts, but get this part working first and then come back to those.

        For finding the link to extract and replace, you need to think about how your file looks. I can't help you with that with the information I have now, but I've gone on long enough so far. Hope this gets you over a hurdle or two.

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        Everything arturo says, plus:

        1. The string comparison operator is "eq", not "==".

        2. You're opening /var/www/html/$catfile for writing, but you're not actually writing to it.

        Replace   print "$line"; with   print NOT $line;