How can I call a subroutine using a variable?

Posted on 2003-03-23
Medium Priority
Last Modified: 2013-12-25
I have a whole bunch of subroutines written with names like this... Chapter1, Chapter2, Chapter3, etc.  I want to call these subroutines using a variable, like this...  &Chapter$num;

I tried doing this and the script fell flat on it's face.  Any ideas how I can accomplish this without rewriting the whole thing???
Question by:Donboy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 84

Accepted Solution

ozo earned 400 total points
ID: 8193201

#but I wouldn't reccomend that style.
#a better method would be to use references


&{$Chapter[$num]||sub{"No such chapter"}};


Author Comment

ID: 8196343
Excellent!  Thanks a million. 100 pts coming your way.

Also, why wouldn't you recommend that style?  What's wrong with it?  Just curious.
LVL 84

Expert Comment

ID: 8196532
Symbolic references are powerful, and slightly dangerous, in that it's
possible to intend (with the utmost sincerity) to use a
hard reference, and accidentally use a symbolic reference instead.
so only hard references are allowed under
use strict 'refs';

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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

777 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