?
Solved

PERL. Check if subroutine exists

Posted on 2007-10-11
2
Medium Priority
?
2,000 Views
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:

test.cgi?act=response

and within test.cgi I have the code:

some cmd line parsing here, then
my $act=$FORM{act};
&$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.

0
Comment
Question by:Bob-Villa
2 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 1000 total points
ID: 20062929

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

Expert Comment

by:ozo
ID: 20065204
Do you really want to let the user do things like
test.cgi?act=CGI::_reset_globals

it may be safer to do something like
%act=(
response => \&response,
resp0nSe => \&resp0nSe,
'CGI::_reset_globals'=>\&CGI::_reset_globals
);
&{$act{$act}||sub{print "$act does not exist\n"}};
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

864 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