Monday, 7 February 2011

Perl Course help!

I need some help as to whether or not to do include something in a beginners perl course I am writing to run at work.

Courses I went on and textbooks I have read (including the excellent Modern Perl) mention the idiom '0 but true', to have a true 0.

Everyone in my office when I asked either went 'what?', 'why?' or 'just something not needed and is confusing'.

Personally, I agree with the 'why?' and 'confusing', so I'm inclined to leave it out. However, what do the community at large think? One of my colleagues has been programming Perl for 15+years, and has never seen it. Is this generally the case?

Thanks to any responses in advance that support either side of the argument, particularly with good reasoning.

Just to confirm my 4 reasons for leaving it out:

1) Never seen it used
2) Too confusing to explain to people experienced in Perl, let alone Newbies
3) Too confusing to read in code
4) Won't be automatically assigned to a variable when answer is 0 (4-4), so better to use defined

Andy

5 comments:

brian d foy said...

There are a lot of things that people using Perl for many years have never seen. In some cases, they just haven't used enough Perl.

There are five values that are false - the number 0, the string '0', the empty list, the empty string, and undef. The undef really turns into one of the other values. Everything else is true. That's the concept to teach people.

To get a '0 but true', it depends on what you are doing with it. That literal string, '0 but true', turns into the number 0 but is still true because it isn't one of those five values. It's the same with the string '00', or the string '0E'.

LeoNerd said...

The main point of "0 but true" is that it is numerically zero, yet boolean true.

DBI uses this, for example, when returning the number of rows affected by a transaction. If a real error happens it returns false. To distinguish the case where no error happened, but no rows happened to match (e.g. because of a WHERE clause), the value "0 but true" is returned.

my $rows = $dbh->....

if( $rows ) {
printf "Affected %d rows\n", $rows;
}
else {
printf STDERR "An error occurred - %s\n", $dbh->errstr;
}

Andy Brown - SetitesUK said...

Thanks for your responses.

I hadn't realised (although I probably should have had an inkling with DBI) that it was being used under the hood of (probably) many modules (but I suppose that the sign of a good API - I don't need to know)

I think I shall leave out the '0 but true' concept in this course. The students I am expecting will probably find four days of any language daunting to start with, and an extra concept like this will be too much. However, I will ensure they know the 5 false values.

Joshua Smith said...

Very interesting review. Thank you for sharing with helpful info. It is simple to find cheap homeowners insurance quotes online from top home insurance companies by zip code.

Avantika Singh said...

Delphi Development OTS Solutions : ISO 9001 & Microsoft Certified Software Outsourcing & Development service provider company offering software Application development , IT outsourcing services to its clients across the globe.