Solved

passing parameters

Posted on 1998-07-30
9
160 Views
Last Modified: 2010-03-04
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; }
0
Comment
Question by:seow
9 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 20 total points
ID: 1208879
1.) $para1 and $para2 should be _references_ to functions, rather than the names of the functions.  Then you'd call

&XYZ(\&para1, \&para2);

2. ) If that's the case &$para1() or &$para2() should do the trick.

3.) if you insist on $para1 and $para2 being the names of the functions (really ugly, I mean, after all, we don't have to be stuck in perl4 forever), I suppose you could do

eval("$para1()");

If it's in a different file, you may need to prepend the package:

eval("main::$para1()"); ## or whatever package it's in

Life get's hard if you need to pass parameters to the sub.  Really, use refs :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208880
Passing the name of the functions as seow is doing should work too.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208881
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.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208882
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:b2pi
ID: 1208883
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!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208884
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)
0
 

Author Comment

by:seow
ID: 1208885
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 ?

0
 
LVL 84

Expert Comment

by:ozo
ID: 1208886
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.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208887
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! :))]

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now