| Author |
Can't use string as a subroutine ref while "strict refs"
|
|
|
| use strict;
use warnings;
use CGI;
my $q = new CGI;
my $do = $q->param('do') || 'main'';
if ($do) {
&$do;
}
sub main {
blah blah
}
========================================
=
Trying to call the subroutine main from variable $do but I am gettin' error:
Can't use string ("main") as a subroutine ref while "strict refs.
But somehow I don't want to remove the 'use Strict;'
Any way out?
Sara.
| |
| Mumia W. 2006-11-09, 6:55 pm |
| On 11/09/2006 06:56 AM, Sara wrote:
> use strict;
> use warnings;
> use CGI;
>
> my $q = new CGI;
>
> my $do = $q->param('do') || 'main'';
>
my $do = $q->param('do') || main();
> if ($do) {
> &$do;
> }
>
> sub main {
> blah blah
> }
> ========================================
=
> Trying to call the subroutine main from variable $do but I am gettin' error:
> Can't use string ("main") as a subroutine ref while "strict refs.
> But somehow I don't want to remove the 'use Strict;'
>
> Any way out?
> [...]
Yes.
Start->Run->"perldoc perlsyn"
Start->Run->"perldoc perlsub"
| |
| Paul Lalli 2006-11-09, 6:55 pm |
| Sara wrote:
> use strict;
> use warnings;
> use CGI;
>
> my $q = new CGI;
>
> my $do = $q->param('do') || 'main'';
>
> if ($do) {
> &$do;
> }
>
> sub main {
> blah blah
> }
> ========================================
=
> Trying to call the subroutine main from variable $do but I am gettin' error:
> Can't use string ("main") as a subroutine ref while "strict refs.
> But somehow I don't want to remove the 'use Strict;'
>
> Any way out?
What you say you want to do is *precisely* what strict prevents you
from doing. So why do you think you want to do that? What are you
expecting $do to be? It's a string. Worse, it's a string given by the
user of your CGI script. Why are you letting the user have that much
control over what functions in your internal code are being called?
perldoc -q "variable name"
Make a dispatch table. That is, a hash whose keys are strings you
expect the user to enter, and the values are references to functions
that should be called for each of those strings.
my %fnct_for = (
alpha => \&run_this,
beta => \&parse_file,
# etc ...
);
my $do = param('do');
if (exists ($fnct_for{$do}) {
$fnct_for{$do}->();
} else {
main();
}
Paul Lalli
| |
| John W. Krahn 2006-11-11, 6:55 pm |
| Sara wrote:
> use strict;
> use warnings;
> use CGI;
>
> my $q = new CGI;
>
> my $do = $q->param('do') || 'main'';
>
> if ($do) {
> &$do;
> }
>
> sub main {
> blah blah
> }
> ========================================
=
> Trying to call the subroutine main from variable $do but I am gettin' error:
> Can't use string ("main") as a subroutine ref while "strict refs.
> But somehow I don't want to remove the 'use Strict;'
>
> Any way out?
my $q = new CGI;
my $do = $q->param( 'do' ) || \&main;
if ( ref $do eq 'CODE' ) {
$do->();
}
sub main {
blah blah
}
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Paul Lalli 2006-11-13, 6:55 pm |
| John W. Krahn wrote:
> my $q = new CGI;
>
> my $do = $q->param( 'do' ) || \&main;
>
> if ( ref $do eq 'CODE' ) {
> $do->();
> }
>
> sub main {
> blah blah
> }
Er. How exactly would $q->param('do') ever return a code ref?
Paul Lalli
| |
| Brad Lhotsky 2006-11-13, 6:55 pm |
| Use a hash dispatch.
my %dispatcher = (
something => \&somethingElse,
default => \&main,
);
my $do = exists $dispatcher{ $q->param('do') }
? $dispatcher{ $q->param('do') }
: $dispatcher{default};
$do->();
On Thu, Nov 09, 2006 at 05:56:33PM +0500, Sara wrote:
> use strict;
> use warnings;
> use CGI;
>
> my $q = new CGI;
>
> my $do = $q->param('do') || 'main'';
>
> if ($do) {
> &$do;
> }
>
> sub main {
> blah blah
> }
> ========================================
=
> Trying to call the subroutine main from variable $do but I am gettin' error:
> Can't use string ("main") as a subroutine ref while "strict refs.
> But somehow I don't want to remove the 'use Strict;'
>
> Any way out?
>
> Sara.
>
>
--
Brad Lhotsky
|
|
|
|