Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: More idiomatic

Reply
Thread Tools

Re: More idiomatic

 
 
Tim McDaniel
Guest
Posts: n/a
 
      04-15-2013
In article <(E-Mail Removed)> ,
David Harmon <(E-Mail Removed)> wrote:
>Here is some code I wrote in another newsgroup. It is probably
>obvious that it was written by an old C hacker. If anybody would
>like to suggest how it could be better perl or more idiomatic,
>go ahead.
>
>>>>

>
>use strict; use warnings;


I prefer one per line.

>open my $fin, 't1.txt';


I concur with the other suggestion of

open(my $fin, '<', 't1.txt') or die 'cannot open "t1.txt"';

> if (/Newsgroups: .*,/i) {


Missing caret.
if (/^Newsgroups: .*,/i) {
I don't know header standards: there always going to be a space after
the "Newsgroups:" keyword? In this list, sometimes you require one,
sometimes you don't. I'd be inclined to not try to match the space
just to be more accepting.

> if (/^Content-Type:/i) {


I agree with "elsif".

> if (/^$/) {


While it's an appealing symmetry to use a regexp, I'd still probably
do
if ($_ eq '') {

> ($fromline,$useragent,$hascont) = split '##';


DEFINITELY concur in "my"ing them here.

--
Tim McDaniel, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      04-16-2013
On 2013-04-15 22:31, David Harmon wrote:
> Tim McDaniel:


>> if ($_ eq '') {

>
> Concur. But please, can I write it
> if (eq '') {
> if not, why not?


You can make "if (eq '')" compile as "if ($_ eq '')".

You could also write a sub (like eq_), to do it for you at runtime.

You can even make "if ( LITERAL )" compile as "if ( $_ eq LITERAL )",
but consider how constants like in "if ( DEBUG )" should be treated.

--
Ruud

 
Reply With Quote
 
 
 
 
Eric Pozharski
Guest
Posts: n/a
 
      04-16-2013
with <kkhlmq$i1i$(E-Mail Removed)> Tim McDaniel wrote:
> In article <(E-Mail Removed)> ,
> David Harmon <(E-Mail Removed)> wrote:

*SKIP*
>> if (/Newsgroups: .*,/i) {

>
> Missing caret.
> if (/^Newsgroups: .*,/i) {
> I don't know header standards: there always going to be a space after
> the "Newsgroups:" keyword?


Quoting RFC3977, chapter 3.6, p.24:

The headers of an article consist of one or more header lines.
Each header line consists of a header name, a colon, a space,
the header content, and a CRLF, in that order.

> In this list, sometimes you require one, sometimes you don't.


Lack of symmetry suggests.

> I'd be inclined to not try to match the space just to be more
> accepting.


Agreed. Later in the same paragraph RFC3977 says "The content ...; it
MAY be empty". The name is "one or more printable US-ASCII characters
other then colon". Geez, is it a NNTP header?

.oO(Look-i'm-a-header!):

Nothing about after-colon-space.

*CUT*

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-16-2013
Ben Morrow <(E-Mail Removed)> writes:
> Quoth "Newsgroup only please, address is no longer replyable."
> <(E-Mail Removed)>:

[...]
>> Concur. But please, can I write it
>> if (eq '') {
>> if not, why not?

>
> No, you can't. As for why not: well, you just can't . Some operators
> have optional arguments that default to $_, some do not; none of the
> binary operators are in the set that do, because that would be really
> confusing. (I'm not even sure they would parse unambiguously in the
> general case.)


For example, (+ 1) and (- 1) already have unambiguous meanings, so they
can't be made equivalent to ($_ + 1) and ($_ - 1), respectively --
unless $_ happens to be zero }.

Requiring two explicit operands for all binary operators is simpler and
less confusing than permitting an implicit $_ only for the cases where
it wouldn't be ambiguous.

(And it's not that hard to type $_.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: More idiomatic J. Gleixner Perl Misc 11 04-16-2013 06:32 PM
Re: More idiomatic Rainer Weikusat Perl Misc 1 04-15-2013 07:45 PM
more idiomatic way to avoid errors when calling method on variable that may be nil? Charles Calvert Ruby 33 10-22-2010 11:44 AM
[ANN] Ruby/Odeum 0.2 (More Idiomatic, Better Source Layout) Zed A. Shaw Ruby 2 04-23-2005 05:40 PM
Is there a more idiomatic way to do this? Alan Mead Perl Misc 4 02-12-2005 04:42 AM



Advertisments