variable interpolation of refs to anonymous subroutines

T

Tad McClellan

Next time, I promise, I'll
post the program.

Cool.



Just because I'm ignorant doesn't mean that I'm stupid.


I don't see any implication of stupidity, so I don't know where
that is coming from...

My question may have seemed dumb to you,


Not dumb, just Asked Frequently (and therefore, already answered).

and I'm sorry I offended by
seeming to be obtuse, but I honestly did make a good attempt to solve
this particular problem with the documentation before posting.


But it was _after_ posting that I observed a need to be told
to read the manual.

It was _after_ you were pointed to the FAQ entry that you were
back asking about @{[ ]}, when that FAQ entry led to an
explanation of @{[ ]}.

From that, I concluded that you did not read the appropriate docs,
despite having them pointed out to you.

Anyway, I appreciate the help -- I solved my problem and am feeling
pretty good now, and learned something to boot.


All is as it should be.
 
T

Tad McClellan

And if you still feel like continuing this discussion, let me ask this.
Dereferencing an anonymous array that contains the return values from a
function seems an awfully weird way to get at the behavior.


It is crufty in the extreme.

I never ever use it in my programs.

I take the simple-minded (but much easier to follow) approach:

my $stuff = some_func('some arg');
print "here is some $stuff from a function\n"; # interpolate plain scalar

Might we be able
in the future to interpolate hashes and functions?


With regards to interpolating functions, I hope not.

Strings are _data_, while function calls are code.

I don't want code hiding in the middle of data.

Hide and seek is a game, I'm (usually) not playing a game
when I write programs.
 
B

Ben Morrow

Quoth (e-mail address removed):
[snip]
While this post demonstrates the guidelines for
incorrect output show, desired output shown,
short/complete pasteable, good subject line,
input __DATA__ included, use warnings/strict
it may fail the Social faux pas "..cursory doc search", since
the answer should be clear from line 1158 of the 2008 lines from
perldoc perlop, in the section labeled 's/PATTERN/REPLACEMENT/egimosx'
and tr/a/b/ would be a better choice than s///;

Nice :)

Unfair, perhaps, as that post would probably get a useful (i.e. 'read
the section on "Quote and Quote-like operators' in perlop') response (at
least from me), but still...

Ben
 
B

Ben Morrow

Quoth (e-mail address removed):
[snip]
Okay, I'll try to do better.

Contrition duly noted and appreciated :).
And if you still feel like continuing this discussion, let me ask this.
Dereferencing an anonymous array that contains the return values from a
function seems an awfully weird way to get at the behavior.

Absolutely. I agree, and would not use that construction in real code,
for that reason.
I guess I was thinking more along the lines of Java, where I would
access an object's method to do what I wanted. I see now that I was
entirely on the wrong track with Perl, but I feel a little like Alice
must have felt. Do you know of any particular reason for this, other
than perhaps only scalars and arrays can be variable interpolated?

Yup, that's it. I believe the approriate phrase here is 'dirty hack'.
Might we be able in the future to interpolate hashes and functions?

Perl6 (which may or may not be considered the future, depending on where
you stand; certainly Perl5 is not going away any time soon) has a
generic expression-interpolation construction; indeed, all the
interpolation stuff has been... sorted out... FVOW which include 'vastly
increased in scope' :).

Ben
 
P

Paul Lalli

And if you still feel like continuing this discussion, let me ask this.
Dereferencing an anonymous array that contains the return values from a
function seems an awfully weird way to get at the behavior.

You are absolutely correct. I strongly recommend against it. Note
that this entire discussion was about "how" to do it, not whether or
not one "should" do it.
I guess I
was thinking more along the lines of Java, where I would access an
object's method to do what I wanted.

I haven't programmed in Java in about 8 years. Does it allow you to
include method calls in strings? That seems dangerous and messy to me.
But maybe I'm just Perl-biased.
I see now that I was entirely on
the wrong track with Perl, but I feel a little like Alice must have
felt. Do you know of any particular reason for this, other than perhaps
only scalars and arrays can be variable interpolated?

That is precisely the reason. Variables (specifically scalars, arrays,
array slices, and hash slices) can be interpolated. Function calls
cannot.
Might we be able in the future to interpolate hashes and functions?

How would you interpolate a hash? A hash is unordered. How should the
keys and values of the hash be printed?

Why would you interpolate a function call? A function call is
(effectively) a control structure. Do you want to interpolate foreach
loops and if blocks too?

Assign the return value of a method call to a variable, and interpolate
that variable.

Or, if you really enjoy being masochistic, change perl to do what you
want, either by modifying the source or using a source Filter (see
CPAN's Filter::Simple). :)

Paul Lalli
 
T

Tad McClellan

Experts at what?

Experts at Perl or experts at posting to newsgroups?



Your entire followup is about the "examples" below.

Where is the "more clearly written" suggestions? (or was that it?)

I don't see that any of the below suggestions make it more
accessible to folks that are not experts with newsgroups.

So then, did you mean that you think they make it more
accessible to folks that are not experts with Perl?

The guidelines are not about Perl, they are about posting
to this Perl newsgroup.

What form should "write up your improvements" take,


Any form that serves as a starting point for the discussion.

Actual proposed wording, not should/coulda discussion stuff.

Best would be copied, then edited, from the POD version.

That is the primary version, the other formats are generated
from the POD.

Plain old text, like you have below is good enough for the
discussion. If changes are agreed on, then I'll post the
changed portions in POD before publishing the new version.

OK, I have included them here.

### Example Subject headers, add two lines above "Part of the beauty"
Example BAD Subject: Newbie, urgently need help with simple PERL question
Example GOOD Subject: How do I change all 'a' to 'b' with a regexp?


Did you follow the link in the line above that?

A bunch of examples there. What's wrong with them?

And anyway, an example of "good" or "bad" without explanation
doesn't add much value.

By the time we add in explanation, it's gotten to two more paragraphs,
just because they couldn't follow the link on the line above?

On top of that, the guidelines are already "too big". If we add
two paragraphs we'll need to find 2 paragraphs to delete, or risk
growing it so large that nobody will even attempt to read it all.

I'm not seeing a benefit proportional to the cost on this one...

### Replacement first paragraph of "Ask perl to help you"
You can ask perl itself to help you find
common programming mistakes by doing three things:
use strict; # see perldoc strict
use warnings; # see perldoc warnings
use diagnostics; # see perldoc diag


I like that better than what's currently in there, except for
the diagnostics part, because it does not help you _find_
programming mistakes, it only explains ones that were found
by the "use warnings" one. Plus "use diagnostics" is down
a little further anyway (along _with_ explanation).


So, does anybody object to making it like this?


You can ask perl itself to help you find common programming mistakes
by doing two things:
use strict; # see perldoc strict
use warnings; # see perldoc warnings

### append this example to the end of "Provide enough information" section
Here is an example of a good post:

Subject: How do I change all 'a' to 'b' with a regexp?

I want to change all 'a' to 'b' with a regexp.
but only the first 'a' is getting changed.
This is the incorrect output of my program:
First line of text dbta
What I wanted is this, note dbtb, not dbta.
First line of text dbtb

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


One statement per line. We don't want to corrupt them with bad examples.

while (<DATA>) {
$_ =~ s/a/b/;
print $_; }
__DATA__
First line of text data

While this post demonstrates the guidelines for
incorrect output show, desired output shown,
short/complete pasteable, good subject line,
input __DATA__ included, use warnings/strict


That part is OKish.

Now, all we need to do is find about 20 lines of less valuable
advice to delete so we'll have room for it.

What should we cut in favor of adding an example post?

it may fail the Social faux pas "..cursory doc search", since

OK...


the answer should be clear from line 1158 of the 2008 lines from
perldoc perlop,


Are you being serious?

We have to update the Posting Guidelines everytime perlop.pod
changes line numbers?

That's easy for you to say. :)

The failure here is that the documentation for the s/// operator
answers the question, and you should be reading the docs for
the operators that you use.


If there is a groundswell of support for adding an example post,
let's see it here.

I at least, do not feel that it is worth it.

and tr/a/b/ would be a better choice than s///;


Huh?

These are style guidelines for _posting_, not for programming.

saying s/// instead of tr/// is NOT _any_ kind of failure to
follow the guidelines.
 
J

jgraber

Tad McClellan said:
Experts at Perl or experts at posting to newsgroups?

At Perl. and thanks for starting a new thread.
Your entire followup is about the "examples" below.

Where is the "more clearly written" suggestions? (or was that it?)

That was it, otherwise its very nice.
I don't see that any of the below suggestions make it more
accessible to folks that are not experts with newsgroups.

So then, did you mean that you think they make it more
accessible to folks that are not experts with Perl?
Yes

The guidelines are not about Perl, they are about posting
to this Perl newsgroup.

Yes, but posting and Perl become tangled when you request that
posts contain a short, complete program with input data,
without showing an example of what that means..
Any form that serves as a starting point for the discussion.
Actual proposed wording, not should/coulda discussion stuff.
I agree.
I should have more clearly seperated my commentary from my
actual proposed wording.
Best would be copied, then edited, from the POD version.

That is the primary version, the other formats are generated
from the POD.

Plain old text, like you have below is good enough for the
discussion. If changes are agreed on, then I'll post the
changed portions in POD before publishing the new version.


Did you follow the link in the line above that? Yes

A bunch of examples there. What's wrong with them?
Nothing, they are fantastic.
And anyway, an example of "good" or "bad" without explanation
doesn't add much value.

I disagree. Examples have flavor and set tone without explanation.
By the time we add in explanation, it's gotten to two more paragraphs,
just because they couldn't follow the link on the line above?

Yes, I followed the link, and thought the examples were excellent.
But many people won't follow a link.
I think it is worth two lines to include those examples,
and keep the link for more examples and explanation.
On top of that, the guidelines are already "too big". If we add
two paragraphs we'll need to find 2 paragraphs to delete, or risk
growing it so large that nobody will even attempt to read it all.
I'm not seeing a benefit proportional to the cost on this one...

If my hypothesis (examples better/shorter than syntax) is true,
then these two example lines can replace the three text lines
starting "Do not spend..." which were already an explanation.
+2 lines - 6 lines saves 4 lines
I like that better than what's currently in there, except for
the diagnostics part, because it does not help you _find_
programming mistakes, it only explains ones that were found
by the "use warnings" one. Plus "use diagnostics" is down
a little further anyway (along _with_ explanation).
So, does anybody object to making it like this?
You can ask perl itself to help you find common programming mistakes
by doing two things:
use strict; # see perldoc strict
use warnings; # see perldoc warnings

Thats ok, but if you added
use diagnotics; # see perldoc diagnostics

you could cut the whole paragraph that explains why,
since it is more about perl than about writing a good post.
I see regulars requesting strict and warnings all the time,
but dont remember many requests for use diagnostics.
Or drop both the use diag, and the description paragraph,
if you want a shorter guideline.

....Example deleted... reposted below inside ## start new
That part is OKish.

Now, all we need to do is find about 20 lines of less valuable
advice to delete so we'll have room for it.

What should we cut in favor of adding an example post?

According to my hypothesis,
if we list and demonstrate good posting style with an example,
we don't have to explain it as well.

So here is a suggested cutdown to replace the 53 lines:
/Speak Perl/ .. /from what you are getting./

## start new, 49 lines: saves 4 lines overall
Ask perl to help you
You can ask perl itself to help you find and understand
common programming mistakes by doing three things:
use strict; # see perldoc strict
use warnings; # see perldoc warnings
use diagnostics; # see perldoc diagnostics

Provide enough information
If you post like the example below, you will have an Extremely Good
chance of getting people to try and help you with your problem!
These features help your question win out over all the other posts.

First make a short (less than 20-30 lines) and *complete* program
that illustrates the problem you are having. You will find that
this step often solves your problem or leads you to an answer
more reliably than posting to Usenet.

Do not re-type Perl code
Use copy/paste instead of re-typing Perl code, to avoid typos.
People should be able to run your program
by copy/pasting from your article.

The following article demonstrates at least 8 good guidelines:
* show incorrect output * show desired output
* short/complete * good subject line * Speak Perl
* input __DATA__ * use warnings/strict * cut/pasteable

Subject: How do I change all 'a' to 'b' with a regexp?

I want to change all 'a' to 'b' with a regexp.
but only the first 'a' is getting changed.
This is the incorrect output of my program:
First line of text dbta
What I wanted is this, note dbtb, not dbta.
First line of text dbtb

Here is my program you can copy/paste to run.

#!/usr/local/bin/perl
use warnings;
use strict;
while (<DATA>) {
$_ =~ s/a/b/;
print $_; }
__DATA__
First line of text data

However, the above may fail the faux pas "..cursory doc search",
since the answer, s/a/b/g; is in 'perldoc perlop' near 's/PATTERN/'
## end new
Are you being serious?

We have to update the Posting Guidelines everytime perlop.pod
changes line numbers?

That's easy for you to say. :)
The failure here is that the documentation for the s/// operator
answers the question, and you should be reading the docs for
the operators that you use.

OK, That part was a little tongue in cheek.
We say "read the docs", so I thought I'd try it.
perldoc -f s
returns "s/// The substitution operator. See perlop."

perldoc perlop, as read from the terminal was getting too long,
so I piped it to a file and found the appropriate spot.
Showing the line numbers demonstrates both the depth
of documentation, as well as points out the "too big" issue.

If the posting guidelines are too big,
then a reference "cursory doc search" which means perldoc,
which has a one-liner, pointing to a 2000-liner
may also be unrealistic behavior to expect.
But the organization of perldoc perlop is different topic,
so I won't mention it here.

Since the guidelines request a "cursory doc search",
I put in an example.
If there is a groundswell of support for adding an example post,
let's see it here.

I at least, do not feel that it is worth it.

I have substantially edited my suggestion to
make it less than zero cost in lines of 68 characters or less.
What do you think now?
Huh?

These are style guidelines for _posting_, not for programming.

saying s/// instead of tr/// is NOT _any_ kind of failure to
follow the guidelines.

Agreed. But I think the less specific replacement answer
above is a worthwhile addition.
ie dont given an example problem without an answer,
 
D

David Squire

At Perl. and thanks for starting a new thread.


That was it, otherwise its very nice.

[snip]

I reckon I'm with Joel. A couple of lines here could save many more
lines of "You should have" on the ng

DS
 
T

Tad McClellan

At Perl. and thanks for starting a new thread.


I did not start a new thread.

I changed the Subject header (within the same thread).

But many people won't follow a link.


Those type of people won't care to read posting guidelines in
the first place, so we do not need to take those people into
consideration since they are not in our audience anyway.

So here is a suggested cutdown to replace the 53 lines:
/Speak Perl/ .. /from what you are getting./

## start new, 49 lines: saves 4 lines overall
Ask perl to help you
You can ask perl itself to help you find and understand
common programming mistakes by doing three things:
use strict; # see perldoc strict
use warnings; # see perldoc warnings
use diagnostics; # see perldoc diagnostics

Provide enough information
If you post like the example below, you will have an Extremely Good
chance of getting people to try and help you with your problem!
These features help your question win out over all the other posts.

First make a short (less than 20-30 lines) and *complete* program
that illustrates the problem you are having. You will find that
this step often solves your problem or leads you to an answer
more reliably than posting to Usenet.

Do not re-type Perl code
Use copy/paste instead of re-typing Perl code, to avoid typos.
People should be able to run your program
by copy/pasting from your article.

The following article demonstrates at least 8 good guidelines:
* show incorrect output * show desired output
* short/complete * good subject line * Speak Perl
* input __DATA__ * use warnings/strict * cut/pasteable

Subject: How do I change all 'a' to 'b' with a regexp?

I want to change all 'a' to 'b' with a regexp.
but only the first 'a' is getting changed.
This is the incorrect output of my program:
First line of text dbta
What I wanted is this, note dbtb, not dbta.
First line of text dbtb

Here is my program you can copy/paste to run.

#!/usr/local/bin/perl
use warnings;
use strict;
while (<DATA>) {
$_ =~ s/a/b/;
print $_; }
__DATA__
First line of text data

However, the above may fail the faux pas "..cursory doc search",
since the answer, s/a/b/g; is in 'perldoc perlop' near 's/PATTERN/'
## end new


I am very busy for the next several weeks and don't have time to
give guidelines changes the attention they deserve.

When the fires are put out in 2-4 weeks, I will post the above
change for discussion as a followup to the automated posting
(which is where these discussions are supposed to be so that I
won't miss them.
)

Let's please suspend this discussion until then.

OK, That part was a little tongue in cheek.


Please don't lead me off into the woods like that.

It was in your *proposed wording*.

Don't propose things that you are not seriously proposing.

Joking around in the discussion part is OK of course. :)

We say "read the docs", so I thought I'd try it.
perldoc -f s
returns "s/// The substitution operator. See perlop."


So a conscientious poster would have found their answer
with a cursory search.

No changes needed! :)

Showing the line numbers demonstrates both the depth
of documentation, as well as points out the "too big" issue.


Let me be clear.

We will need to find someone else to maintain the guidelines
if we insert doc line numbers in them.

First, it ties the guidelines to a particular perl version.

Second, it requires the effort to keep the guidelines in sync
with the docs. I do not volunteer for that level of effort.

Adding line numbers is not going to happen unless the group
can shout me down and find a replacement maintainer...

I have substantially edited my suggestion to
make it less than zero cost in lines of 68 characters or less.
What do you think now?


Better. Much better.
 
J

jgraber

Tad McClellan said:
I am very busy for the next several weeks and don't have time to
give guidelines changes the attention they deserve.

When the fires are put out in 2-4 weeks, I will post the above
change for discussion as a followup to the automated posting
(which is where these discussions are supposed to be so that I
won't miss them. )

Let's please suspend this discussion until then.

OK, fair enough.
Support or comments from others are still welcome in the meantime.
 

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top