Re: More idiomatic

Discussion in 'Perl Misc' started by Tim McDaniel, Apr 15, 2013.

  1. Tim McDaniel

    Tim McDaniel Guest

    In article <>,
    David Harmon <> 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,
    Tim McDaniel, Apr 15, 2013
    #1
    1. Advertising

  2. Tim McDaniel

    Dr.Ruud Guest

    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
    Dr.Ruud, Apr 16, 2013
    #2
    1. Advertising

  3. with <kkhlmq$i1i$> Tim McDaniel wrote:
    > In article <>,
    > David Harmon <> 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
    Eric Pozharski, Apr 16, 2013
    #3
  4. Ben Morrow <> writes:
    > Quoth "Newsgroup only please, address is no longer replyable."
    > <>:

    [...]
    >> 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 :cool:}.

    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) <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"
    Keith Thompson, Apr 16, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zed A. Shaw
    Replies:
    2
    Views:
    107
    Zed A. Shaw
    Apr 23, 2005
  2. Charles Calvert
    Replies:
    33
    Views:
    356
    w_a_x_man
    Oct 22, 2010
  3. Alan Mead
    Replies:
    4
    Views:
    139
    Alan Mead
    Feb 12, 2005
  4. J. Gleixner

    Re: More idiomatic

    J. Gleixner, Apr 15, 2013, in forum: Perl Misc
    Replies:
    11
    Views:
    270
    Rainer Weikusat
    Apr 16, 2013
  5. Rainer Weikusat

    Re: More idiomatic

    Rainer Weikusat, Apr 15, 2013, in forum: Perl Misc
    Replies:
    1
    Views:
    154
    Rainer Weikusat
    Apr 15, 2013
Loading...

Share This Page