Solved

"Use Strict"

Posted on 1998-11-10
14
258 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 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Xsl Nested Loop 4 22
Programatically edit Web.config 8 28
error email attachment VB2010 5 28
PHP radio buttons with email not sending 24 89
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now