Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get a sub routine name from a reference 1

Status
Not open for further replies.

audiopro

Programmer
Joined
Apr 1, 2004
Messages
3,165
Location
GB
I am assigning the following references to sub routines.
Code:
my %field_names=(
 _adcat => \&ADCAT,
 Xadcat => \&CHECK_ADCAT,

 _adcat2 => \&ADCAT,
 Xadcat2 => \&NC,

 _adcat3 => \&ADCAT,
 Xadcat3 => \&NC);
and calling them with
Code:
$VALUE=$field_names{"$ref_name"}->($TEXT1,$TNAME,$TVALUE,$COMMENT,$PICCY,$field_id,$DISP_TYPE);
and all is working well.
I need to check if the sub routine being called is &NC, how do I extract the name from the reference?

Keith
 
There is probably another way to do this, but you could alter your hash a little to make it easy:

Code:
my %field_names=(
 _adcat => [\&ADCAT,'ADCAT'],
 Xadcat => [\&CHECK_ADCAT,'CHECK_ADCAT'],

 _adcat2 => [\&ADCAT,'ADCAT'],
 Xadcat2 => [\&NC,'NC'],

 _adcat3 => [\&ADCAT,'ADCAT'],
 Xadcat3 => [\&NC,'NC']);
 
my $ref_name = 'Xadcat3';
my $VALUE = ''; 
if ($field_names{$ref_name}[b][1][/b] eq 'NC') {
   do something
}

to call the sub routine properly:

Code:
  $VALUE=$field_names{"$ref_name"}[b][0][/b]->($TEXT1,$TNAME,$TVALUE,$COMMENT,$PICCY,$field_id,$DISP_TYPE);
 
Thanks for the reply.
As I understand it, rather than try to get the name of the referenced array, the second item in the declared array can be read directly?



Keith
 
I'm not aware of a way of doing exactly what you're looking for. However, I am curious about why you want to do it. Can you elaborate on what you're doing, overall?
 
As I understand it, rather than try to get the name of the referenced array, the second item in the declared array can be read directly?

You understand it. But I think you meant to say:

rather than try to get the name of the referenced sub routine


I am also curious about what you are wanting to do.
 
Ok I'll try and keep this brief.
I have several websites which use numerous HTML forms for user input and user editing. I have written two sub routines to handle the operations. 1 sub routine controls the display of the relevant fields and the other controls field level checking.
The simplified vars in my example are the names fields in numerous MySQL tables entered into a pre declared hash.
Each element appears twice, the first points to a display function to determine how a field with that particular name is displayed, (text box, text area, dropdown menu, date entry etc.) mostly by CGI query commands.
The second element points to a checking function for that particular field name. I have found this works well and adding another type of form to the website, is a simple matter of creating an array containing the field names and an array containing the associated label text and the form is done and working.
Now for why I asked the question.
Each field has an associated picture, blank at first but changed to a cross if the associated field did not meet the required check and the form had to be re-displayed to the user. Not all fields need to be completed, such as extra address lines etc. so I needed a way to indicate which are the required fields. Fields which do not need checking are simply directed to a sub routine called 'NC'.
So the check I am doing is:-
If NC leave the picture blank.
If not NC, field required, display 'required' graphic.
It may sound complicated but it has made my life much easier considering one of the applications uses 23 different input/edit forms.


Keith
 
One thought would be to add an optional argument to your subs that, if a value is passed in, the sub can return a string with the name of the sub (or anything else you want) rather than doing the normal work.
 
I thought of that but I need to decide which picture should be diplayed before I call the relevant display sub, otherwise I would have to process the picture code in each individual display sub, rather than the main display routine.

Keith
 
It may sound complicated but it has made my life much easier considering one of the applications uses 23 different input/edit forms.

Yes it does sound way complicated. But you are probably too far along to want to change your system at this point.

Did my suggestion help you at all? Or is it too late in your development phase to alter the data structure?
 
Yes thanks Kevin, your solution was spot on but only added the code this morning and it worked first time, that has to be a first, no typo's, syntax errors, just working code.
I was finding that creating numerous forms and keeping track of all the error checking was getting way out of hand, this way I reuse the display and checking code routines.
Is there a simpler way to handle form input, error checking?


Keith
 
you can really handle the error checking/validation anyway you want. If what you have works for you then that is fine. It sounds complicated to me but in might be a good solution for your script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top