tags:

views:

148

answers:

5

As a non-English native, I used to make a lot of English grammatical and idiomatic mistakes. For example, I would write "I will can climb Mt Everest." instead of "I will be able to climb Mt Everest" and "students learn more knowledge in school" instead of "students lean more in school". Now I'm learning Perl as my first programming language and similarly I've been making a lot of Perl grammatical mistakes along the way. And one thing particularly struck me as odd: with a human language, when grammar is wrong, the meaning still comes across and the talk does not stop there. But when it comes to a computer language, when syntax is botched, everything else is done for. But I'm having a feeling that artificial language grammar may be easier to learn, although Perl is the only such language I know and I know it only superficially.

Well, I've experimented with something like, restructure

while(<>){ y/[A-Z] /[a-z]_/;}

to

y/[A-Z] /[a-z]_/ while(<>);

But when I tried restructuring

while(<>){ if s/ /_/;print lc;}

to

print lc if s/ /_/ while(<>);

I got a syntax error. I then found I could compromise a little and use

s/ /_/ ? print lc : print lc while (<>);

In English, I can put the while ...if...,structure together pretty fine but not in the reversed order. In Perl, I can do it in the reversed order, but looks like I can't have it all my own way. I guess there must be some articles specifically dealing with Perl usage rules or providing guidance on Perl idioms, something like don't put two auxilliary verbs together in some English grammar guide book, something like telling the Perl learner print $_; is redudant, but not something too general like what's on perlfunc manpage.

So can someone kindly recommend me articles, preferably light-weight and interesting, on, I suppose, intermediate-levelish Perl syntax and idioms? Thanks in advance.

+5  A: 

I learned a lot from Perl Best Practices. The funny thing about Perl is that there are different syntaxes for the same thing. For example:

open(OUT, ">file.csv"); does the same thing as:

open my $out, '>', "file.csv";

or

if (x>1) {
    y++;
}

and

y++ if (x>1);
carillonator
@carillonator, thanks for the book recommendation. It looks like that this Perl thing shares some unnecessary characteristics with human language. With a human language, we can express one single idea in many different ways. But there're usually more pragmatic than semantic differences between different ways because we humans are emotional and language conveys feelings and attitudes. Wondering why machine language offers different ways of doing things.
Mike
Tim toady, Mike, tim toady. http://www.perlfoundation.org/perl5/index.cgi?tmtowtdi
outis
@Mike: Perl is a computer language, a programming language and a formal language, but not a "machine language". "Machine language" refers to any language that is executed directly by a processor without need for a compiler or interpreter (http://en.wikipedia.org/wiki/Machine_language). I wish this comment were more funny and less pedantic.
outis
@Mike: Programming languages have varying approaches to syntax. Perl's motto is "There's more than one way to do it." The flexibility of Perl's syntax is by design; it's the result of a conscious effort to provide the developer with the freedom to choose the best way to do things (for some definition of "best"). It also attempts to mimic the syntax of natural language, though probably with a bias towards English. Other programming languages prefer an "orthogonal" syntax that provides one and only one way to do a particular thing. Many Holy Wars have been fought over which approach is better.
Michael Carman
@outis, thanks for the clarification. I thought machine language and computer language were synonymous.I thought they were the opposite of the human language which included natural human language and artificial human language like Esperanto.
Mike
@Michale, your explanation makes sense. Looks like the creator of Perl not only mimicked natural language on the syntax and semantic level but alson on the pragmatic level, in which case he introduced the concept of context.
Mike
@Michael, Being context-sensitive is very un-machinelike. Perl creator is weird.
Mike
@Michael, but it's fun to have this context-sensitive feature :)
Mike
+4  A: 

One of the finest mid-level Perl Books that I have read is: Effective Perl Programming. It has an entire section titled "Idiomatic Perl". Fortran was my first programming language and during my first few years with Perl, I wrote Perl "in Fortan". This book effectively cured me of that habit.

I hear that there is a second edition planned - surely something to look forward to.

Gurunandan
@Gurunandan, thanks for the book recommendation. I will be taking a look at that book :)
Mike
This book will have a new edition this spring, current to Perl 5.12, with even more idiomatic coolness. :)
brian d foy
+6  A: 

The best way is to read lots of code and ask lots of questions. And the best way to do that is to get a job working with other perl developers.

Short of that, you might try browsing through http://www.google.com/search?q=site%3Aperlmonks.org+%22perl+idiom%22

Not specific to idioms, but you will definitely come out knowing how to use perl as a functional programming language if you read http://hop.perl.plover.com.

ysth
@ysth, thanks for the links, the help :) They look like exactly what I was looking for. Thanks!
Mike
+1  A: 

For your

print lc if s/ /_/ while(<>);

how about this instead:

do {print lc if s/ /_/} while(<>)
AmbroseChapel
@Ambrose, this line of your code works :) Thanks for the guidance.
Mike
+1  A: 

Some good articles showing transformations of non-idiomatic Perl to idiomatic Perl were published in the Perl Journal:

Sean McMillan
@Sean, I just took a quick peek at Part I of the linked articles, it looked really useful to me and I'll take another more careful read of all of them. Thanks a lot :)
Mike