Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"Use Strict"

Posted on 1998-11-10
14
Medium Priority
?
299 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
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 85

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

877 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