Solved

"Use Strict"

Posted on 1998-11-10
14
291 Views
Last Modified: 2013-11-19
I need to do this:

my $f_name = "SomeFunctionName";
my $myvar = &$f_name($param);

This works, except when I put "use strict" at the top of my script.  How can I get "use strict" to like this?

webslider
0
Comment
Question by:webslider
[X]
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
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
ID: 1206109
my($param) = 12;

my($f_ptr) = \&SomeFunctionName;
my($myvar) = &$f_prtr($param);
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1206110
$ perldoc strict
.
     strict refs
           This generates a runtime error if you use symbolic
           references (see the perlref manpage).
 
               use strict 'refs';
               $ref = \$foo;
               print $$ref;        # ok
               $ref = "foo";
               print $$ref;        # runtime error; normally ok

The only way I know of to get around this problem would be:

use strict;
my $f_name = "SomeFunctionName";
{
  no strict 'refs';
  my $myvar = &$f_name($param);
}

Hope this helps
  Tobias
0
 

Author Comment

by:webslider
ID: 1206111
Can you verify your syntax please?

my($f_ptr) = \&SomeFunctionName;
    -OR-
my $fname="somefunctionname";
my($f_ptr) = \$fname;


my($myvar) = &$f_prtr($param);
   -OR-
my($myvar) = &$f_ptr($param);
0
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

 
LVL 5

Expert Comment

by:b2pi
ID: 1206112
f_ptr is a reference to a subroutine
myvar take the result of an invocation of that subroutine.

    my($f_ptr) = \&SomeFunctionName;
    my($myvar) = &$f_prtr($param);

If you prefer, you can use

    my($myvar) = &{$f_prtr}($param);

which may be somewhat clearer.
   
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1206113
b2pi: I think webslider wants you to comment on your typo :-)

      f_prtr vs. f_ptr

0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206114
Ah, well, the vagaries of cut and paste.
0
 

Author Comment

by:webslider
ID: 1206115
This isn't going to work:

      my($f_ptr) = \&SomeFunctionName;

The whole point of doing this is to be able to pass a string representing the function name into a subroutine and calling the function that was requested.  Sure, I could use this if I had a CASE to handle every option but there are too many....so, use this as a bigger picture (keep in mind the following subroutine has much more going on than whats below):

sub CallThisFunction (
   my $function_name = shift;
   my $param = shift;

   my $answer = &$function_name($param);
   return $answer;
}

Thanks for your help so far!
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206116
sub CallThisFunction {
  my($function_reference) = shift;
  my($param) = shift;

# Actually, my($function_reference, $param) = @_;
# is more efficient than the above, as well as easier to maintain

  my($answer) = &{$function_reference}($param);
  return $answer;
}

0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206117
Sorry, I forgot to add that you then call

$answer = CallThisFunction(\&SomeFunction, $param);

rather than

$answer = CallThisFunction("SomeFunctionName", $param);


0
 

Author Comment

by:webslider
ID: 1206118
The above suggestion works great...however there is one more sticky point....
Further in my function that I described before...I need to do this:

sub CallThisFunction (
         my $function_name = shift;
         my $another_function_name = $function_name."2";
         my $param = shift;

         my $answer = &$function_name($param);  
       #*****NEW STUFF***
         if (defined $answer) {
               $answer = &$another_function_name($param);
         }
         return $answer;  
 }

So as you can see, making the first function call is fine as described before, but generating the second function name then calling it is a problem...

Is the only way around this to add another parameter and pass the 2nd function the same as the first?  I'm really hoping not to have to do this.

webslider
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206119
Yes, you need to andd another parameter, _but_ .....

Frankly, I'd probably recode the indirect functions so that they take a parameter to indicate whether they should act like fx 1 or 2

i.e. if you had had:

sub Fx1 {
  my($val) = @_;
   return 24.3 * $val;
}

sub Fx2 {
  my($val) = @_;
   return 48.6 * $val;
}

then I'd change that to:

sub Fx {
  my($parm, $val) = @_;
  if (!defined($parm) or !$parm) {
          return $val*24.3;
  } else {
         return $val * 48.6;
 }
}

Further, if, as it appears, your two functions are sequential, then combine them into one, and have the one return the correct response (rather than relying upon the caller to call them in the right order in the right conditions.

0
 
LVL 84

Expert Comment

by:ozo
ID: 1206120

0
 

Author Comment

by:webslider
ID: 1206121
Actually, I have no control over rewriting the functions so that they are called with one function.  I'm just using functions that are prewritten.

Thanks for all the great help!
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206122
OK, if that's the case, and your two functions that you might want to call are fx and fx2,

then write another function like:

sub AllFx {
   my($switch, $param) = @_;
  if (defined($switch) && $switch) {
    return &fx($param);
  } else {
    return &fx2($param);
  }
}

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

628 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