Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

running sub via string while using strict. 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Hi,

I'm trying to dynamically call a subroutine, only i get an error when using
Code:
&{'page'.$id.'_data'}();

Saying not while strict is on, if I turn strict off, the command runs as desired.

Is there any other way of dynamically calling a sub while using strict?

Thanks,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I checked out the strict pragma and found I can turn off strict refs with
Code:
no strict "refs";

Can someone advise what else I'm now stopping from showing as an error apart from the dynamic routine call, I have always worked under full strict , so am concerned what other potential 'bugs' this may introduce.

Cheers,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
This reminds me on similar topic discussed back in 2006 in this thread:

The final aspect of the use strict pragma is the disabling of soft references (or symbolic references).
(see here:
Here is the example for using soft references:
soft_ref.pl
Code:
[COLOR=#0000ff]# Example of using soft refernces[/color]
[COLOR=#0000ff]# works only without strict-pragma[/color]

[COLOR=#0000ff]#use strict;[/color]
[COLOR=#804040][b]use warnings[/b][/color];

[COLOR=#804040][b]sub[/b][/color][COLOR=#008080] [/color][COLOR=#008080]addition[/color]{
  [COLOR=#804040][b]my[/b][/color] ([COLOR=#008080]$a[/color], [COLOR=#008080]$b[/color]) = [COLOR=#008080]@_[/color];
  [COLOR=#804040][b]return[/b][/color] ([COLOR=#008080]$a[/color]+[COLOR=#008080]$b[/color]);
}

[COLOR=#804040][b]sub[/b][/color][COLOR=#008080] [/color][COLOR=#008080]substraction[/color]{
  [COLOR=#804040][b]my[/b][/color] ([COLOR=#008080]$a[/color], [COLOR=#008080]$b[/color]) = [COLOR=#008080]@_[/color];
  [COLOR=#804040][b]return[/b][/color] ([COLOR=#008080]$a[/color]-[COLOR=#008080]$b[/color]);
}

[COLOR=#804040][b]my[/b][/color] ([COLOR=#008080]$foo[/color], [COLOR=#008080]$bar[/color]);

[COLOR=#008080]$a[/color] = [COLOR=#ff00ff]1[/color]; [COLOR=#008080]$b[/color] = [COLOR=#ff00ff]2[/color];
[COLOR=#0000ff]#[/color]
[COLOR=#008080]$foo[/color]=[COLOR=#ff00ff]'[/color][COLOR=#ff00ff]addition[/color][COLOR=#ff00ff]'[/color];
[COLOR=#008080]$bar[/color]=[COLOR=#008080]&$foo[/color]([COLOR=#008080]$a[/color],[COLOR=#008080]$b[/color]);
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]bar=[/color][COLOR=#008080]$bar[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
[COLOR=#0000ff]#[/color]
[COLOR=#008080]$foo[/color]=[COLOR=#ff00ff]'[/color][COLOR=#ff00ff]substraction[/color][COLOR=#ff00ff]'[/color];
[COLOR=#008080]$bar[/color]=[COLOR=#008080]&$foo[/color]([COLOR=#008080]$a[/color],[COLOR=#008080]$b[/color]);
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]bar=[/color][COLOR=#008080]$bar[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
As mentioned above it works only without strict-pragma:
Code:
C:\Users\Roman\Work>perl soft_ref.pl
$bar=3
$bar=-1
If you want to use strict-pragma then use hard references.
I would create a hash of references on given functions - something like this:
hard_ref.pl
Code:
[COLOR=#0000ff]# Example for using hard refernces[/color]
[COLOR=#0000ff]# works with strict-pragma too[/color]

[COLOR=#804040][b]use strict[/b][/color];
[COLOR=#804040][b]use warnings[/b][/color];

[COLOR=#804040][b]sub[/b][/color][COLOR=#008080] [/color][COLOR=#008080]addition[/color]{
  [COLOR=#804040][b]my[/b][/color] ([COLOR=#008080]$a[/color], [COLOR=#008080]$b[/color]) = [COLOR=#008080]@_[/color];
  [COLOR=#804040][b]return[/b][/color] ([COLOR=#008080]$a[/color]+[COLOR=#008080]$b[/color]);
}

[COLOR=#804040][b]sub[/b][/color][COLOR=#008080] [/color][COLOR=#008080]substraction[/color]{
  [COLOR=#804040][b]my[/b][/color] ([COLOR=#008080]$a[/color], [COLOR=#008080]$b[/color]) = [COLOR=#008080]@_[/color];
  [COLOR=#804040][b]return[/b][/color] ([COLOR=#008080]$a[/color]-[COLOR=#008080]$b[/color]);
}


[COLOR=#804040][b]my[/b][/color] ([COLOR=#008080]%foo_hash[/color], [COLOR=#008080]$operation[/color], [COLOR=#008080]$bar[/color]);

[COLOR=#0000ff]# create hash of function references[/color]
[COLOR=#008080]$foo_hash[/color]{[COLOR=#ff00ff]'[/color][COLOR=#ff00ff]addition[/color][COLOR=#ff00ff]'[/color]} = [COLOR=#008080]\&addition[/color];
[COLOR=#008080]$foo_hash[/color]{[COLOR=#ff00ff]'[/color][COLOR=#ff00ff]substraction[/color][COLOR=#ff00ff]'[/color]} = [COLOR=#008080]\&substraction[/color];

[COLOR=#008080]$a[/color] = [COLOR=#ff00ff]1[/color]; [COLOR=#008080]$b[/color] = [COLOR=#ff00ff]2[/color];
[COLOR=#0000ff]#[/color]
[COLOR=#008080]$operation[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]addition[/color][COLOR=#ff00ff]'[/color];
[COLOR=#008080]$bar[/color]=[COLOR=#008080]$foo_hash[/color]{[COLOR=#008080]$operation[/color]}([COLOR=#008080]$a[/color],[COLOR=#008080]$b[/color]);
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]bar=[/color][COLOR=#008080]$bar[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
[COLOR=#0000ff]#[/color]
[COLOR=#008080]$operation[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]substraction[/color][COLOR=#ff00ff]'[/color];
[COLOR=#008080]$bar[/color]=[COLOR=#008080]$foo_hash[/color]{[COLOR=#008080]$operation[/color]}([COLOR=#008080]$a[/color],[COLOR=#008080]$b[/color]);
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]bar=[/color][COLOR=#008080]$bar[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
Code:
C:\Users\Roman\Work>perl hard_ref.pl
$bar=3
$bar=-1
But I wonder why this derefencing simply works
Code:
$bar=$foo_hash{$operation}($a,$b);
because IMHO it should be more cryptic - so:
Code:
$bar=&{$foo_hash{$operation}}($a,$b);
 
I forgot to say, that both dereferences work:
Code:
$bar=$foo_hash{$operation}($a,$b);
Code:
$bar=&{$foo_hash{$operation}}($a,$b);
 
lol - man I can't even remember taking part.

Amazing how much you forget when you don't use it all the time, but hey it was 4 years ago!

Thanks for finding the old thread and giving me a deserved kick up the behind!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top