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

Hi, I reached the point of asking as I am struggling a bit to reach that end which I am not sure if that is totally possible. is it possible something like:

<a href=\"perlfunction\">This</a> ?

Within the same perl script to recall the function embedding something like:

&perlfunction; ?

is it possible to do that without using extra modules? Sorry but still learning, then I got some questions about perl but, I guess that is the way many thanks

Replies are listed 'Best First'.
Re: Run Perl function using href
by NetWallah (Canon) on Aug 15, 2011 at 02:13 UTC
    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."

      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."

        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.

Re: Run Perl function using href
by Anonymous Monk on Aug 14, 2011 at 20:01 UTC