Link to home
Start Free TrialLog in
Avatar of seow
seow

asked on

passing parameters

i've got a subrountine XYZ (in my library file)  which takes in 2 parameters and then call another subroutine (not in the library file, in another file) accordingly.

eg.  &XYZ("para1", "para2");

sub XYZ
{
  ($para1, $para2) = @_;

if (like this.....)
{

&$para1;   qn=> i know the code looks stupid, but how do i call the subroutine???
}
else { &$para2; }
ASKER CERTIFIED SOLUTION
Avatar of b2pi
b2pi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
Passing the name of the functions as seow is doing should work too.
Yeap, it should work (and I even handed off code to show how to do it).  On the other hand, I don't use labels and goto's in my perl code, even though it should work.  I don't use the map function, because I'm not a lisp programmer.

M \y point was that it was stylistically preferable, and much more maintainable, to use references.
Avatar of alamo
alamo

I think ozo's point may have been that no eval or explicit reference is required, that the seow's original code actually works as is.

That is, that something like

$para1 = "para1";
&$para1;

works, calling sub para1.
Ummm, yeah, as long as you don't have use strict .

On the other hand, I thinnk whan my answer really shows is that I hand't had enough coffee.  Talk about bitchy... Some people's kids!
I prefer references stylisticly too.
I'd probably even prototype XYZ as taking code referneces,
call it withthout the &, and declare my($para1, $para2)
(I'd also indent differently, but that's more a matter of personal taste)
Avatar of seow

ASKER

thanx to all for the help.. now then i realize that my code really works.. (don know why it doesn't work b4) .. anyway..  just a few qns..

ozo: u mean i can just call my subroutine without the "&" ?? i don quite understand your last comment. Can pls elaborate more..

b2pi : thanx for your suggestions to use reference. i never thought of that and never even tried it b4.. (me still a novice) . Need to clarify something :
       
          on your suggested answer (the 1st one) , i don understand the point 2 part.
        u mean if i use reference, i must call my subroutine like &$para1() , with the brackets ?? can leave out the brackets..? does it matters that my "para1" is a subroutine ?

prototypes only work when you call a subroutine without using &
(which means it doesn't work calling a reference, whether strict or symbolic)
you can leave out the parentheses, see
perldoc perlsub
Try it a few different ways.  We'll try to clarify anything confusing.
If you pass references to routines, they you do have to call them as &$para.
You can also call them as &{$para}

You don't need the parentheses, nor do you need to identify the package they came from. In fact, you can do them anonymously....

 &XYZ(sub {
              print "This is the first sub\n";
          },
          sub {
              print "This is the second sub\n"
          });

sub XYZ  {
      ($sub1, $sub2) = @_;
    if (like this.....)  {
       &$sub1(parm1, parm2, parm3);
    } else {
       &$sub2(parm1, parm2, parm3);
    }
}

Some object to anonymous sub's... some object to lambda's...
[hint: I'm not one of them.. .ignore the remark about lambda's, it's really close to flamebait! :))]