Reference to functions

V

Vito Corleone

Hi,

I tried to make a call a function by reference. Here is the code:

#!/usr/bin/perl
use strict;

sub show_a {
my ($txt) = @_;
print "show_a: $txt\n\n";
}

sub show_b {
my ($txt) = @_;
print "show_b: $txt\n\n";
}

sub show {
my ($type) = @_;
my $f = "show_" . $type;
$f->($type);
}

show("a");

But there was an error:
Can't use string ("show_a") as a subroutine ref while "strict refs" in
use at ./test.pl line 18.

How do I make this kind of call to function with 'use strict' turned on?

--Vito
 
J

Josef Moellers

Vito said:
Hi,

I tried to make a call a function by reference. Here is the code:

#!/usr/bin/perl
use strict;

sub show_a {
my ($txt) = @_;
print "show_a: $txt\n\n";
}

sub show_b {
my ($txt) = @_;
print "show_b: $txt\n\n";
}

sub show {
my ($type) = @_;
my $f = "show_" . $type;
$f->($type);
}

show("a");

But there was an error:
Can't use string ("show_a") as a subroutine ref while "strict refs" in
use at ./test.pl line 18.

How do I make this kind of call to function with 'use strict' turned on?

Can't you use a hash?

#! /usr/bin/perl
use warnings;
use strict;

my %show = (
"a" => sub {my ($txt) = @_;
print "show_a: $txt\n\n";
},
"b" => sub {my ($txt) = @_;
print "show_b: $txt\n\n";
}
);

sub show {
my ($type) = @_;
$show{$type}($type);
}

show("a");

Josef
 
P

Paul Lalli

Vito Corleone said:
Hi,

I tried to make a call a function by reference. Here is the code:

#!/usr/bin/perl
use strict;

sub show_a {
my ($txt) = @_;
print "show_a: $txt\n\n";
}

sub show_b {
my ($txt) = @_;
print "show_b: $txt\n\n";
}

sub show {
my ($type) = @_;
my $f = "show_" . $type;
$f->($type);
}

show("a");

But there was an error:
Can't use string ("show_a") as a subroutine ref while "strict refs" in
use at ./test.pl line 18.

How do I make this kind of call to function with 'use strict' turned
on?

You don't. That's specifically the kind of thing use strict; exists to
prevent. What you are doing is using a variable as a variable name,
otherwise known as symbolic references. (Although, I grant you, you're
doing it differently than most people who ask this question have
tried...). Read
perldoc -q 'variable name'
for information on why this is A Bad Idea.

Instead, you should create your two subroutines as anonymous coderefs,
stored in a hash. Then your show() routine simply picks the right
coderef to call from the hash:

#!/usr/bin/perl
use strict;
use warnings;
my %show = (
a => sub {
my $txt = shift;
print "Show A: $txt\n";
},
b => sub {
my $txt = shift;
print "Show B: $txt\n";
}
);

sub show {
my $let = shift;
$show{$let}->(shift);
}

print "A:\n";
show('a', 'foo');

print "B:\n";
show('b', 'bar');
__END__


Paul Lalli
 
B

Brian McCauley

Bernard said:
Paul Lalli said:
You don't. That's specifically the kind of thing use strict;
exists to prevent. [...]

It's *one of several* kinds of things use strict exists to prevent.
Not a nitpick. That kind of statement would have confused me to no
end back when I was starting out with Perl. :)

[ Your smiley has no nose, how does it smell?... ]

Not to nitpick but strict prevents 3 kinds of things and I've always
considered "several" to start not lower than 4. :)
 
C

Chris Mattern

Brian said:
[ Your smiley has no nose, how does it smell?... ]

Awful!

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,884
Members
47,419
Latest member
ArturoBres

Latest Threads

Top