Perl scares me ...

R

Richard Gration

While trying to work out the syntax for building a dispatch table of
coderefs from strings in a database I had occasion to construct the
following:

#!/usr/bin/perl

use strict;
use warnings;

my $one = "mysub";

sub mysub {
print shift,"\n";
}

#1
{
no strict 'refs';
$one->('scalar');
}

#2
{
'mysub'->('string');
}

#3
{
my $subref = \&$one;
$subref->('subref');
}

This produces the output:

scalar
string
subref

If there was any further proof needed that Perl updates are received in a
back alley somewhere from some dude with red eyes who smells of sulphur
.... I mean, honestly !!! ;-)

And then there's my slight amazement that #1 needs "no strict 'refs'" and
#2 doesn't ...

Rich
 
M

Matija Papec

X-Ftn-To: Richard Gration

Richard Gration said:
If there was any further proof needed that Perl updates are received in a
back alley somewhere from some dude with red eyes who smells of sulphur
... I mean, honestly !!! ;-)

Err, and what was your expected output?
And then there's my slight amazement that #1 needs "no strict 'refs'" and
#2 doesn't ...

References in perl are very well documented, you may want to check "perldoc
perlref"
 
B

Brian McCauley

Richard Gration said:
my $one = "mysub";


And then there's my slight amazement that #1 needs "no strict 'refs'" and
#2 doesn't ...

Yeah, it's a known problem (er.. bug) that strict does not pick-up on
stuff that gets optomised away during compliation. Indeed there seems
to have been a spate of threads about this lately.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
P

pkent

Richard Gration said:
While trying to work out the syntax for building a dispatch table of
coderefs from strings in a database I had occasion to construct the
following:
....
<snip example code>


Obviously I have no idea what you were trying to do, or what your
restrictions were, but... wasn't there a better way? :) My dispatch
tables always look something like:

%foo = (
bar => \&bar,
baz => \&baz,
# etc...
);

# insert more code here

sub bar {
my ($thing, $whatever, $boo) = @_;
# some code here
}


and as luck would have it I'm working on a database-based app right now
at work, too.

P
 
R

Richard Gration

Obviously I have no idea what you were trying to do, or what your
restrictions were, but... wasn't there a better way? :) My dispatch
tables always look something like:

%foo = (
bar => \&bar,
baz => \&baz,
# etc...
);
# insert more code here
sub bar {
my ($thing, $whatever, $boo) = @_;
# some code here
}

Well, mine used to too, but then I was faced with coming up with a way to
make the same code operate the dispatch table for different websites,
each with their own database, hence moving the config into the db and
building the dispatch table from there, rather than having in the code
blocks like

if (host is websiteA) {
dispatch_table is ...
} elsif (host is websiteB) {
dispatch_table is ...
} ...

which I'm trying to get away from.
and as luck would have it I'm working on a database-based app right now
at work, too.

Hmm ... luck would be sitting on a Caribbean beach with a rum based
cocktail in one hand and a babe in the other ;-) But ISWYM :)
 
J

Joe Smith

Richard said:
Well, mine used to too, but then I was faced with coming up with a way to
make the same code operate the dispatch table for different websites,
each with their own database, hence moving the config into the db and
building the dispatch table from there, rather than having in the code
blocks like

if (host is websiteA) {
dispatch_table is ...
} elsif (host is websiteB) {
dispatch_table is ...
} ...

which I'm trying to get away from.

$_ = 'websiteA'
$dispatch_table = $table{$_};

or

$result = $table{$website}->function($args);
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top