PERL. Check if subroutine exists

Posted on 2007-10-11
Last Modified: 2013-12-25
Looking for a way to find out if a named subroutine exists.

If I use the following string to invoke test.cgi:


and within test.cgi I have the code:

some cmd line parsing here, then
my $act=$FORM{act};
sub response [
print "OK";

I want to catch a subroutine that does not exist before the default error handler gets it. Like if someone types:  test.cgi?act=resp0nSe I want to stop the script gracefully since this routine is not valid.

Question by:Bob-Villa
    LVL 48

    Accepted Solution


    print "$act does not exist\n" unless defined &$act;
    LVL 84

    Expert Comment

    Do you really want to let the user do things like

    it may be safer to do something like
    response => \&response,
    resp0nSe => \&resp0nSe,
    &{$act{$act}||sub{print "$act does not exist\n"}};

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    This is about my first experience with programming Arduino.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now