Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

How can I call a subroutine using a variable?

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???
0
Donboy
Asked:
Donboy
  • 2
1 Solution
 
ozoCommented:
&{Chapter.$num};

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

@Chapter=(
\&Chapter0,
\&Chapter1,
\&Chapter2,
\&Chapter3,
\&Chapter4,
);

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


0
 
DonboyAuthor Commented:
Excellent!  Thanks a million. 100 pts coming your way.

Also, why wouldn't you recommend that style?  What's wrong with it?  Just curious.
0
 
ozoCommented:
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';
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now