Solved

"Use Strict"

Posted on 1998-11-10
14
279 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
Industry Leaders: 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!

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

749 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