in reply to Run Perl function using href

The CGI module does this kind of thing rather well, but you want to avoid modules.

The way to avoid quote-confusion is to learn to use the q() and qq() functions which provide single and double quotes. So your link could be built like this:

my $new_link = qq|<a href="perlfunction">Info on perl functions</a>|;
Note - the q and qq functions allow the usage of almost any delimiter - I choose the vertical bar, which is not commonly used for quoted stuff or HTML.

            "XML is like violence: if it doesn't solve your problem, use more."

Replies are listed 'Best First'.
Re^2: Run Perl function using href
by rainmanh (Initiate) on Aug 15, 2011 at 14:42 UTC

    many thanks for your replies. I went through manuals and they use to go through a simple href pointer but never got an approximation on how to execute a function within the perl command recalled by href which was my problem, and I did not want to include more forms as I already got my share of them to run this application.

      Just trying to understand your question.

      It looks like you want to display a link, that when clicked, will call up some perl code (a specific function).

      If this is the case (and you are doing simple CGI), the href target will be the perl code file. You can pass additional parameters to the code file to call specific functions.

      Assuming your code is called cgi2.pl, and your cgi directory is "cgi-bin", and the function name is myfunc2, your initial page should have the href (statically or dynamically generated) to say:

      <a href="/cgi-bin/cgi2.pl?pleasecall=myfunc2&param1=one&param2=two"> +Click here to call func</a>
      Now your cgi2.pl needs code like this (All this gets much nicer, if you "use CGI;"):
      my %param; # Access the Query String Data if (length ($ENV{'QUERY_STRING'}) > 0){ my @pairs = split(/&/, $ENV{'QUERY_STRING'}); foreach my $pair (@pairs){ my ($name, $value) = split(/=/, $pair); $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg +; $param{$name} = $value; } } # Find function, and call it with params .. if ($param{pleasecall} eq "myfunc2"){ myfunc2 ($param{param1}, $param{param2}); }

                  "XML is like violence: if it doesn't solve your problem, use more."

        All this gets much nicer, if you "use CGI;"

        CGI has far fewer bugs, too.

      You do understand, don't you, that a browser -- by itself -- can NOT execute a perl function?

      ... and which "manuals" did you go through? Your response seems to suggest that you must have skipped those suggested in Re: Run Perl function using href.

        There is no need for an aggressive behaviour neither a need to make feel bad newcomers