Command Line

G

Gunnar Hjalmarsson

Uri said:
you are assuming the chronological and emotional and technical ages
of coders are related where i say they are not.

Nope. I'm just saying that if you tell a grown-up: "you can do it this
way, or you can do it that way, but you should better do it this way
because...", then the grown-up is typically able to make a
well-founded decision based on that info.
i have seen all sorts, kiddies who got it right off, long time
coders who never did get it

There are always exceptions. In the programming field as well as in
the rest of this world. Neither you or I can do much about that.

But the fact that there are exceptions with a bad judge does not
justify a general disparagement.
 
M

Matt Garrish

At least we can rest easy knowing that Uri does his thunking with his
brane...
I consider me to be a newbie that happens to have an idea of what you
are talking about. If I had been totally clueless, I would have been
either insulted by your comment above, or at least you'd have aroused
my curiosity. In any case, the effect of your comment would have been
the opposite of what you intended.

It's not worth pointing out to him. I'm convinced now that he just doesn't
realize that the tone he uses is offensive and contemptful. It's sad that a
number of the more civil and knowledgable posters seem to have left, but
there still is useful information to gleaned, even occasionally from Uri.
You just have to remember that if you're trying to find meaningful
relationships you should be using lavalife, not usenet...

Matt
 
U

Uri Guttman

GH> Nope. I'm just saying that if you tell a grown-up: "you can do it this
GH> way, or you can do it that way, but you should better do it this way
GH> because...", then the grown-up is typically able to make a
GH> well-founded decision based on that info.

GH> There are always exceptions. In the programming field as well as in
GH> the rest of this world. Neither you or I can do much about that.

GH> But the fact that there are exceptions with a bad judge does not
GH> justify a general disparagement.

this is where we differ. i have seen coders of all skills and ages and
their age has no connection to their skill. so your so-called grownup is
meaningless. this is not a common sense thing like playing in the
street. this is a technical decision that takes experience and skills to
understand. it is much easier to understand how to use symrefs and eval
for these kinds of problems than it is to understand why they are bad
solutions. the general newbie will not know enough to make a wise
decision. this is my experience over 30 years of coding. coding is too
easy to learn and much harder to do well. look at at the book learning
perl, it doesn't even cover references. it is written to hide trickier
things from newbie. later when they have a firmer grounding they can
learn them. we have seen too many newbies ask the very FAQ i wrote, how
do i access a hash/array/scalar by name? this means it needs to be
addressed and properly which is to describe data structures and to steer
them from symrefs and eval.

uri
 
U

Uri Guttman

MG> It's not worth pointing out to him. I'm convinced now that he just
MG> doesn't realize that the tone he uses is offensive and
MG> contemptful. It's sad that a number of the more civil and
MG> knowledgable posters seem to have left, but there still is useful
MG> information to gleaned, even occasionally from Uri. You just have
MG> to remember that if you're trying to find meaningful relationships
MG> you should be using lavalife, not usenet...

you haven't gotten it. i am not trying to be offensive to newbies, but i
AM trying to be offensive to those who answer with symref/eval solutions
without caveats or other proper solutions. those people think they are
being helpful and are only spreading the mad perl code disease.
i hit hard and fast when i see those posts since they can infect all too
fast. as i said, the OP has said it works and has gone away. think he
will stop using symrefs on his own? it works, it is simple. wait until
he shoots his head off with symrefs and can't debug it for weeks. i
would rather hurt the feelings of someone than have newbies thinking
that is a good coding solution. think of the tradeoff there. hurt
someone one for a little while or save a coder from sure madness later
on. such an easy choice! :)

uri
 
B

Ben Morrow

Matt Garrish said:
At least we can rest easy knowing that Uri does his thunking with his
brane...
:)

It's not worth pointing out to him. I'm convinced now that he just doesn't
realize that the tone he uses is offensive and contemptful. It's sad that a
number of the more civil and knowledgable posters seem to have left, but
there still is useful information to gleaned, even occasionally from Uri.

That almost (but not quite) earned you a killfile entry. I, at least,
have learned much from Uri; and his basic point that newbies should be
encouraged to stay away from dangerous constructions at all costs is
valid. Anyone capable of using such things in the situations where
they are useful is more than capable of finding them out for
themselves (I would put myself-as-of-six-months-ago in the 'capable of
finding out for myself but not of realising there were safer ways to
do it' category).

As for Uri's tone: yes, it is somewhat contemptuous of newbies-in-
general, yes, it is cynical; this is Usenet: what did you expect?
Anyone who comes to Usenet without realising that they will be treated
with contempt (probably unjustifiably) at some point is in for a very
hard time.

Ben
 
U

Uri Guttman

BM> As for Uri's tone: yes, it is somewhat contemptuous of newbies-in-
BM> general, yes, it is cynical; this is Usenet: what did you expect?
BM> Anyone who comes to Usenet without realising that they will be treated
BM> with contempt (probably unjustifiably) at some point is in for a very
BM> hard time.

but you did the same thing that matt did, misread my post. i was annoyed
at the followup with the evil stuff. i never even answered the
newbie. my tone was aimed at the bad answer, not the naive question. and
if you look carefully i have helped many newbies. i just don't brook the
'help me but i didn't even think about this first or do any research or
read the docs' newbie attitudes. that is not all newbies (but too many
to my taste :).

uri
 
B

Ben Morrow

Uri Guttman said:
if you look carefully i have helped many newbies.

and I wrote earlier:
:)

i just don't brook the 'help me but i didn't even think about this
first or do any research or read the docs' newbie attitudes.

No. Nor, I think, does anyone for more than about five minutes unless
they're getting paid for it.

Ben
 
W

Walter Roberson

:> i just don't brook the 'help me but i didn't even think about this
:> first or do any research or read the docs' newbie attitudes.

:No. Nor, I think, does anyone for more than about five minutes unless
:they're getting paid for it.

If someone asks politely and at least bothers to phrase the question
in their own words, I might help... but not if they just post a question
obviously copied from an assignment and expect to have it answered and
explained to them.
 
G

George Kinley

It seems I was not very clear in my question though the reply from Sherm
Pendley worked, I did replied with thanks , but sorry for Misguiding some
you with different name, I use a shared computer, and we each have our own
News group Client, and I replied with a wrong one,

--

I am reading all the conversation , and trying to follow this dispatch
table, and if I do I use it , but my actual requirement was some thing like
this ,

My perl program was self sufficient in all the manners, for my own hack I
needed this option of calling individual Subroutines
 
G

Gunnar Hjalmarsson

Uri said:
GH> But the fact that there are exceptions with a bad judge does
GH> not justify a general disparagement.

this is where we differ.

Do we? Do you think that people with a bad judge justify a disparaging
attitude in general?

I understand that you react when somebody answers along 'the wrong
route' without caveats. Also Sherm gave you right on that, btw.
the general newbie will not know enough to make a wise decision.

That's true only if he/she only gets to know about the 'bad' solution.
look at at the book learning perl, it doesn't even cover
references. it is written to hide trickier things from newbie.
later when they have a firmer grounding they can learn them.

I want to believe that the _reason_ for that is not that the readers
are not considered able to 'handle' the knowledge that that technique
exists. I want to believe that it's simply out of pedagogical
considerations.
we have seen too many newbies ask the very FAQ i wrote, how do i
access a hash/array/scalar by name? this means it needs to be
addressed and properly which is to describe data structures and to
steer them from symrefs and eval.

Agreed.

In a previous post in this sub-thread you also said that "the regulars
have always encouraged dispatch tables over symrefs/eval". Encouraging
is fine. Advising is fine.

But "hiding" methods that are obviously a part of the programming
language is bad. You'd better not treat grown-up people that way. It's
patronizing. It's insulting. And it's counteractive to the mission:
Making the beginners adopt a good coding style.
 
A

Anno Siegel

David K. Wall said:
[...]
Beginning programmers are confused and frustrated by the different
behaviors a computer shows them, once they look behind the unifying UI.
One of the significant differences is the machine's response to program
text, as opposed to other data. "It *knows* the variable name, so why
can't I type in the name and have it print the value?" is a legitimate
question at some stage, and the short answer is, "Because what you type
is data, and the variable is part of the program."

/me plays devil's advocate, or maybe argumentative newbie...

Yeah, so? That's not an answer, that's a restatement of the problem.
Data, program, it's all in the computer and I should be able to access any
of it. Don't give me this crap about them being different. If I tell the
computer I want foo, then it should give me what's in $foo! (that's NOT
supposed to be $foo factorial)

Yes, it's absolutely natural to expect that, isn't it?

In a fundamental way, the argumentative newbie is right. On a von
Neumann machine there no primary difference between program and data.
Compilers and interpreters *make* the difference because their designers
have decided that it is a good thing.

That's also why the short answer "one is data, the other is program"
is unsatisfactory. It isn't an explanation in terms of fundamental
facts, but it is a statement about "best practice" in the craft.
It has emerged from experience, often painful, and there is mostly
consensus about it, but we can't point to a fact and say, it can't
be any other way because of *this*.

Anno
 
B

Ben Morrow

Gunnar Hjalmarsson said:
That's true only if he/she only gets to know about the 'bad'
solution.

Not true. While I (obviously) cannot speak for anyone in particular,
your average newbie, when presented with two solutions, will take the
one that looks immediately easiest. They will not understand (and
often not be interested in even reading) explanations of why the other
may be better in the long run.

OTOH, I sometimes wonder if anyone can learn how to program properly
except by getting burned... in which case it makes little difference,
in the long run.
But "hiding" methods that are obviously a part of the programming
language is bad. You'd better not treat grown-up people that way. It's
patronizing. It's insulting. And it's counteractive to the mission:
Making the beginners adopt a good coding style.

As Uri said (and I am *not* talking about anyone in particular here),
there are many people who, although grown-up in years, do not have the
understanding of the technical issues involved or the experience to
realise the value of good coding practices, and who do not have the
humility (which is a form of emotional grown-up-ness) to accept that
someone like Uri probably knows better than they do.

Ben
 
G

Gunnar Hjalmarsson

Ben said:
As Uri said (and I am *not* talking about anyone in particular
here), there are many people who, although grown-up in years, do
not have the understanding of the technical issues involved or the
experience to realise the value of good coding practices, and who
do not have the humility (which is a form of emotional
grown-up-ness) to accept that someone like Uri probably knows
better than they do.

You say "many" people, I'd prefer "some". Irrespective of how many
they are, patronizing grown-up people in general can never be
justified.
 
M

Matt Garrish

Uri Guttman said:
you haven't gotten it. i am not trying to be offensive to newbies, but i
AM trying to be offensive to those who answer with symref/eval solutions
without caveats or other proper solutions. those people think they are
being helpful and are only spreading the mad perl code disease.
i hit hard and fast when i see those posts since they can infect all too
fast. as i said, the OP has said it works and has gone away. think he
will stop using symrefs on his own? it works, it is simple. wait until
he shoots his head off with symrefs and can't debug it for weeks. i
would rather hurt the feelings of someone than have newbies thinking
that is a good coding solution. think of the tradeoff there. hurt
someone one for a little while or save a coder from sure madness later
on. such an easy choice! :)

I understand where you're coming from, which was actually the point of my
response. I've gone that route with you of trying to pick on your tone and I
was just trying to relate to Gunnar that it's a pointless argument to start.
It's just the way you write at times. I don't agree that's it's the most
effective way to deal with people (it just leads to animosity), but being
insulted *will* probably make the person do their homework in the hopes of
coming back and giving it to you in kind.TMTOWTDI also applies to responding
to posts to usenet...

Matt
 
D

David K. Wall

Uri Guttman said:
the guidelines don't contain comments on perl coding. but the regulars
have always encouraged dispatch tables over symrefs/eval. this is
definitely material for an FAQ (or several). what is the actual question
being asked here, that symrefs/eval provide a solution?.

how about:

how do i call a sub when i have its name?

and

how do i get an array or hash when i have its name?

those capture the essence of newbies wanting symrefs/eval. both can be
answered with hashes.

Ok, how about a rough draft that can be criticized/expanded/revised? The
following is not a definitive answer: someone with more knowledge,
experience, and writing ability could doubtless do better, but at least
it's a start. If nothing else, maybe it will encourage someone else to
write something better from scratch.



How do I call a subroutine or get the value of a variable when I have the
name of the subroutine or variable?

Use a dispatch table. A dispatch table is implemented with a hash, which
allows you to associate a string value with a reference. (see L<perlref>)
Since references can refer to (nearly) anything, this permits great
flexibility.

For example, here is a dispatch table using subroutine references:

my %dispatch = (
update => \&update,
add => \&add,
error => sub { die "Error: @_" }
default => \&do_something,
);

If we have a desired name in the scalar C<$input>, the associated
subroutine can be called using C<$dispatch{$input}->()>. The subroutines
do not have to be written by you; you can import functions from a module
and use them in your dispatch table.

The code should always handle the case where the input value is not a key
of C<%dispatch>, so we might implement the call like this:

if ( exists $dispatch{$input} ) {
$dispatch{$input}->(@arguments);
}
else {
$dispatch{'default'}->();
# or perhaps an error message... TMTOWTDI
}

or perhaps like this:

my $routine = $dispatch{$input} || die "Cannot find code for $input";
$routine->(@arguments);

The hash entries can be references to any data structure. Here is an
example using arrays:

my %dispatch = (
default => \@default,
1 => \@data1,
2 => \@data2,
other => \@other
);

Now if we want to print the values in @data1 with commas between the
elements, we could say

print join ',', @{$dispatch{1}};

[Maybe a note here about common practical uses of dispatch tables? Or
should that be at the beginning?]
 
U

Uri Guttman

DKW> Ok, how about a rough draft that can be
DKW> criticized/expanded/revised? The following is not a definitive
DKW> answer: someone with more knowledge, experience, and writing
DKW> ability could doubtless do better, but at least it's a start. If
DKW> nothing else, maybe it will encourage someone else to write
DKW> something better from scratch.

good start.


DKW> How do I call a subroutine or get the value of a variable when I
DKW> have the name of the subroutine or variable?

DKW> Use a dispatch table. A dispatch table is implemented with a
DKW> hash, which allows you to associate a string value with a
DKW> reference. (see L<perlref>) Since references can refer to
DKW> (nearly) anything, this permits great flexibility.

a dispatch table is the special name for a hash with code refs. this
should be separated from the lookup var by name stuff.

DKW> For example, here is a dispatch table using subroutine references:

DKW> my %dispatch = (
DKW> update => \&update,
DKW> add => \&add,
DKW> error => sub { die "Error: @_" }
DKW> default => \&do_something,
DKW> );

DKW> If we have a desired name in the scalar C<$input>, the associated
DKW> subroutine can be called using C<$dispatch{$input}->()>. The
DKW> subroutines do not have to be written by you; you can import
DKW> functions from a module and use them in your dispatch table.

import is not needed either. fully qualified sub names are ok.

better to say that the values of the dispatch table are CODE
references. they can be refs to named subs in (or imported into) this
module, refs to fully qualified sub names, and also anonymous CODE refs.

DKW> The code should always handle the case where the input value is
DKW> not a key of C<%dispatch>, so we might implement the call like
DKW> this:

DKW> if ( exists $dispatch{$input} ) {
DKW> $dispatch{$input}->(@arguments);
DKW> }
DKW> else {
DKW> $dispatch{'default'}->();

missing @arguments here

DKW> # or perhaps an error message... TMTOWTDI
DKW> }

or merging the default into the main case:

($dispatch{$input} || $dispatch{'default'})->(@arguments);

($dispatch{$input} || $default_sub)->(@arguments);

point to make: put the creation of the dispatch table outside of any sub
that uses it so it is created at compile time and not recreated in each
call to the sub that does the dispatching. of course unless there is a
reason to create the table dynamically (and i haven't seen many of those).


DKW> or perhaps like this:

DKW> my $routine = $dispatch{$input} || die "Cannot find code for $input";
DKW> $routine->(@arguments);

or:

DKW> The hash entries can be references to any data structure. Here is an
DKW> example using arrays:

DKW> my %dispatch = (
DKW> default => \@default,
DKW> 1 => \@data1,
DKW> 2 => \@data2,
DKW> other => \@other
DKW> );

DKW> Now if we want to print the values in @data1 with commas between the
DKW> elements, we could say

DKW> print join ',', @{$dispatch{1}};

dispatch is only for sub calling hashes. you dispatch through a sub. the
name and concept predates perl (i did this in assembly decades
ago). this is just basic data structure stuff. the reader should be
refered to perlreftut, perlref, perldsc and perllol

DKW> [Maybe a note here about common practical uses of dispatch tables? Or
DKW> should that be at the beginning?]

good idea.

uri
 
U

Uri Guttman

BM> As Uri said (and I am *not* talking about anyone in particular
BM> here), there are many people who, although grown-up in years, do
BM> not have the understanding of the technical issues involved or the
BM> experience to realise the value of good coding practices, and who
BM> do not have the humility (which is a form of emotional
BM> grown-up-ness) to accept that someone like Uri probably knows
BM> better than they do.

hell, i am chronologically mature and i don't have the humilty to accept
that i know better than i do. :)

uri
 
U

Uri Guttman

GK> It seems I was not very clear in my question though the reply from Sherm
GK> Pendley worked, I did replied with thanks , but sorry for Misguiding some
GK> you with different name, I use a shared computer, and we each have our own
GK> News group Client, and I replied with a wrong one,

have you read the rest of the thread that stated how evil was sherm's
answer? using synrefs or eval for your problem is very bad perl
especially for newbies who don't understand why it is bad perl. read the
rest of the thread and fix your code to use a dispatch table.

uri
 
D

David K. Wall

[critique of a discussion of dispatch tables]

Rather than address each point separately, the points you made are merged
into what follows. It's shorter now, and I think it reads better.



How do I call a subroutine when I have the name of the subroutine?

Use a dispatch table. A dispatch table is just a special name for a hash
with code references. (see L<perlref>) The code references can be from any
source, for example, references to named subroutines in or imported into a
module, references to fully qualified subroutine names, or anonymous code
references.

Here is an example of a dispatch table:

my %dispatch = (
update => \&update,
add => \&add,
other => \&Foo::bar,
error => sub { die "Error: @_" }
default => \&do_something,
);

Put the creation of the dispatch table outside of any subroutine that uses
it so it (the dispatch table) is created at compile time and not recreated
in each call to the subroutine that does the dispatching.

If we have a desired name in the scalar C<$input>, the associated
subroutine can be called using C<$dispatch{$input}->()>.

The code should always handle the case where the input value is not a key
of C<%dispatch>, so we might implement the call like this:

($dispatch{$input} || $dispatch{'default'})->(@arguments);

Hash entries can be references to any data structure; this entry only
discusses the special case of dispatch tables. For more on references and
data structures, see L<perlreftut>, L<perlref>, L<perldsc>, and L<perllol>.

[Insert note about common practical uses of dispatch tables. (to be filled
in, preferably by someone with more experience)]
 
B

Brian McCauley

Uri Guttman said:
BM> I am not part of 'we' on this. I think it is better to tell
BM> people about the dangers of doing what it is that they think they
BM> want to do.

i didn't object to yours as much as sherm's since you did put a caveat
with it. i just would have written a more viscious caveat or just not
mentioned it when really appropriate.

Freudian slip? :)
speaking of which, i would love to see a list of posts here where
using symrefs or eval were the right solutions to problems.

Oddly enough I was thinking of to a talk on that at
YAPC::Europe::2004. Or really to explain the argument of code/data
separation and stability v. convience. Of course I know that you and
I draw the line in a different place so you'll probably attack me like
crazy in the Q&A.

Of course the one place symrefs are the right solution is method refs.
(Yes, I know you choose to call those by another name but as I've
pointed out before[1] renaming doesn't actually make them _be_
something else and it certainly doesn't make them any less dangerous).
BM> If we don't mention that they can simply do what they said then
BM> sooner or later they'll discover that they can. When this happens
BM> they'll think they've found a better solution that we didn't know
BM> about.

the docs for them need to state this message. use these only when they
are the proper solution, not when something safer can be used.

Yes I agree.
BM> Anyhow this CS-centric view of Perl irritates me as much as it does
BM> MJD. (You did catch his rant at YAPC::Europe::2003 didn't you, Uri).

and i ranted about this in my talk there. :)

Sorry, I missed your talk.

[1] http://groups.google.com/[email protected]

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

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top