From grand-rapids-pm-list at pm.org Fri Oct 10 07:52:20 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 10:52:20 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! Message-ID: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> Here's something that tripped me up that I thought I'd share as a Perl Puzzle... Q1: For what value(s) of $a do the following comparisons return different results? $a >= 0 $a ge 0 Q2: Ok, it isn't hard to guess. But, why? The answer reveals something unique about Perl that is very important to keep in mind. Feel free to reply to the list. I'll post the answer later today if no one nails it. -Ed -------------- next part -------------- An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Fri Oct 10 08:00:32 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 08:00:32 -0700 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> Message-ID: <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> use Test::More tests=>2; my $str = "-0"; ok($str>=0); ok($str ge 0); --matt On Fri, Oct 10, 2008 at 7:52 AM, wrote: > Here's something that tripped me up that I thought I'd share as a Perl > Puzzle... > > Q1: For what value(s) of $a do the following comparisons return different > results? > > $a >= 0 > $a ge 0 > > Q2: Ok, it isn't hard to guess. But, why? The answer reveals something > unique about Perl that is very important to keep in mind. > > > Feel free to reply to the list. I'll post the answer later today if no one > nails it. > > -Ed > > _______________________________________________ > grand-rapids-pm-list mailing list > grand-rapids-pm-list at pm.org > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > -- Matthew Heusser, Blog: http://xndev.blogspot.com General, you are listening to a machine. Do the world a favor and don't act like one. -- Dr. Stephen Falken, "War Games", The Movie -------------- next part -------------- An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Fri Oct 10 08:12:31 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 11:12:31 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> Message-ID: <5cb4d9a00810100812r6d123d2arfdb307bdd6ec1204@mail.gmail.com> Or the ever popular true-but-zero value: 0E0 --Jason On Fri, Oct 10, 2008 at 11:00 AM, wrote: > use Test::More tests=>2; > > my $str = "-0"; > ok($str>=0); > ok($str ge 0); > > > --matt > > On Fri, Oct 10, 2008 at 7:52 AM, wrote: >> >> Here's something that tripped me up that I thought I'd share as a Perl >> Puzzle... >> >> Q1: For what value(s) of $a do the following comparisons return different >> results? >> >> $a >= 0 >> $a ge 0 >> >> Q2: Ok, it isn't hard to guess. But, why? The answer reveals something >> unique about Perl that is very important to keep in mind. >> >> >> Feel free to reply to the list. I'll post the answer later today if no one >> nails it. >> >> -Ed >> >> _______________________________________________ >> grand-rapids-pm-list mailing list >> grand-rapids-pm-list at pm.org >> http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > > > > -- > Matthew Heusser, > Blog: http://xndev.blogspot.com > > General, you are listening to a machine. Do the world a favor and don't act > like one. -- Dr. Stephen Falken, "War Games", The Movie > > _______________________________________________ > grand-rapids-pm-list mailing list > grand-rapids-pm-list at pm.org > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > From grand-rapids-pm-list at pm.org Fri Oct 10 08:15:12 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 08:15:12 -0700 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <5cb4d9a00810100812r6d123d2arfdb307bdd6ec1204@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> <5cb4d9a00810100812r6d123d2arfdb307bdd6ec1204@mail.gmail.com> Message-ID: <61dd97c0810100815nde2c71ft1b53b5db0107dcc9@mail.gmail.com> OH, 0E0, I spit at thee ... --matt On Fri, Oct 10, 2008 at 8:12 AM, wrote: > Or the ever popular true-but-zero value: 0E0 > > --Jason > > On Fri, Oct 10, 2008 at 11:00 AM, wrote: > > use Test::More tests=>2; > > > > my $str = "-0"; > > ok($str>=0); > > ok($str ge 0); > > > > > > --matt > > > > On Fri, Oct 10, 2008 at 7:52 AM, wrote: > >> > >> Here's something that tripped me up that I thought I'd share as a Perl > >> Puzzle... > >> > >> Q1: For what value(s) of $a do the following comparisons return > different > >> results? > >> > >> $a >= 0 > >> $a ge 0 > >> > >> Q2: Ok, it isn't hard to guess. But, why? The answer reveals something > >> unique about Perl that is very important to keep in mind. > >> > >> > >> Feel free to reply to the list. I'll post the answer later today if no > one > >> nails it. > >> > >> -Ed > >> > >> _______________________________________________ > >> grand-rapids-pm-list mailing list > >> grand-rapids-pm-list at pm.org > >> http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > > > > > > > > -- > > Matthew Heusser, > > Blog: http://xndev.blogspot.com > > > > General, you are listening to a machine. Do the world a favor and don't > act > > like one. -- Dr. Stephen Falken, "War Games", The Movie > > > > _______________________________________________ > > grand-rapids-pm-list mailing list > > grand-rapids-pm-list at pm.org > > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > > > _______________________________________________ > grand-rapids-pm-list mailing list > grand-rapids-pm-list at pm.org > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > -- Matthew Heusser, Blog: http://xndev.blogspot.com General, you are listening to a machine. Do the world a favor and don't act like one. -- Dr. Stephen Falken, "War Games", The Movie -------------- next part -------------- An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Fri Oct 10 09:45:51 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 12:45:51 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <61dd97c0810100815nde2c71ft1b53b5db0107dcc9@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> <5cb4d9a00810100812r6d123d2arfdb307bdd6ec1204@mail.gmail.com> <61dd97c0810100815nde2c71ft1b53b5db0107dcc9@mail.gmail.com> Message-ID: <48EF86BF.8060200@brucereed.com> An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Fri Oct 10 10:03:47 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 13:03:47 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <48EF86BF.8060200@brucereed.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> <5cb4d9a00810100812r6d123d2arfdb307bdd6ec1204@mail.gmail.com> <61dd97c0810100815nde2c71ft1b53b5db0107dcc9@mail.gmail.com> <48EF86BF.8060200@brucereed.com> Message-ID: <2b5f94b80810101003j77a892fdv6c3b7b0e49e1f985@mail.gmail.com> (Reminder, our mailing list has "From:" address masking turned on. Unless you type your name or include a signature line, we don't know who you are!) -------------- next part -------------- An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Fri Oct 10 10:11:06 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 13:11:06 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <2b5f94b80810101003j77a892fdv6c3b7b0e49e1f985@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> <5cb4d9a00810100812r6d123d2arfdb307bdd6ec1204@mail.gmail.com> <61dd97c0810100815nde2c71ft1b53b5db0107dcc9@mail.gmail.com> <48EF86BF.8060200@brucereed.com> <2b5f94b80810101003j77a892fdv6c3b7b0e49e1f985@mail.gmail.com> Message-ID: <88780896-051E-441C-A260-C819E2AC0D31@stacksys.com> > (Reminder, our mailing list has "From:" address masking turned on. > Unless you type your name or include a signature line, we don't > know who you are!) > ... says the anonymous sender. :) - Joe Smith From grand-rapids-pm-list at pm.org Fri Oct 10 10:13:43 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 13:13:43 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <2b5f94b80810101003j77a892fdv6c3b7b0e49e1f985@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> <5cb4d9a00810100812r6d123d2arfdb307bdd6ec1204@mail.gmail.com> <61dd97c0810100815nde2c71ft1b53b5db0107dcc9@mail.gmail.com> <48EF86BF.8060200@brucereed.com> <2b5f94b80810101003j77a892fdv6c3b7b0e49e1f985@mail.gmail.com> Message-ID: <48EF8D47.9020706@brucereed.com> An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Fri Oct 10 10:53:52 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 13:53:52 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <48EF8D47.9020706@brucereed.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> <5cb4d9a00810100812r6d123d2arfdb307bdd6ec1204@mail.gmail.com> <61dd97c0810100815nde2c71ft1b53b5db0107dcc9@mail.gmail.com> <48EF86BF.8060200@brucereed.com> <2b5f94b80810101003j77a892fdv6c3b7b0e49e1f985@mail.gmail.com> <48EF8D47.9020706@brucereed.com> Message-ID: <5cb4d9a00810101053k4823d4b1u11fb9576444d776b@mail.gmail.com> Oh, *different* results. That's what I get for copying Matt's tests and expecting that I should make them both pass. So, the infamous 0E0 is not an answer to the puzzle, but the empty string is. I won't spoil the answer, but I still don't think Ed has heard the response he was looking for :) Any more guesses? --Jason Porritt On Fri, Oct 10, 2008 at 1:13 PM, wrote: > Forgot how anonymous this list is. > > I posted the reply about symbols and puntuation in the puzzle. > > Bruce in Midland, MI > > > grand-rapids-pm-list at pm.org wrote: > > (Reminder, our mailing list has "From:" address masking turned on. Unless > you type your name or include a signature line, we don't know who you are!) > > ________________________________ > _______________________________________________ > grand-rapids-pm-list mailing list > grand-rapids-pm-list at pm.org > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > > _______________________________________________ > grand-rapids-pm-list mailing list > grand-rapids-pm-list at pm.org > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > From grand-rapids-pm-list at pm.org Fri Oct 10 10:58:10 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 10:58:10 -0700 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <5cb4d9a00810101053k4823d4b1u11fb9576444d776b@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> <61dd97c0810100800kbc381d0x2d152af91761000f@mail.gmail.com> <5cb4d9a00810100812r6d123d2arfdb307bdd6ec1204@mail.gmail.com> <61dd97c0810100815nde2c71ft1b53b5db0107dcc9@mail.gmail.com> <48EF86BF.8060200@brucereed.com> <2b5f94b80810101003j77a892fdv6c3b7b0e49e1f985@mail.gmail.com> <48EF8D47.9020706@brucereed.com> <5cb4d9a00810101053k4823d4b1u11fb9576444d776b@mail.gmail.com> Message-ID: <61dd97c0810101058j5e46fc65l5bf2f22c703d867@mail.gmail.com> Yes. The assumption was that >=0 and gr 0 will be true in all cases. I found a case where you get on ok, one error ... --matt heusser On Fri, Oct 10, 2008 at 10:53 AM, wrote: > Oh, *different* results. That's what I get for copying Matt's tests > and expecting that I should make them both pass. > > So, the infamous 0E0 is not an answer to the puzzle, but the empty > string is. I won't spoil the answer, but I still don't think Ed has > heard the response he was looking for :) > > Any more guesses? > > --Jason Porritt > > On Fri, Oct 10, 2008 at 1:13 PM, wrote: > > Forgot how anonymous this list is. > > > > I posted the reply about symbols and puntuation in the puzzle. > > > > Bruce in Midland, MI > > > > > > grand-rapids-pm-list at pm.org wrote: > > > > (Reminder, our mailing list has "From:" address masking turned on. Unless > > you type your name or include a signature line, we don't know who you > are!) > > > > ________________________________ > > _______________________________________________ > > grand-rapids-pm-list mailing list > > grand-rapids-pm-list at pm.org > > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > > > > _______________________________________________ > > grand-rapids-pm-list mailing list > > grand-rapids-pm-list at pm.org > > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > > > _______________________________________________ > grand-rapids-pm-list mailing list > grand-rapids-pm-list at pm.org > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list > -- Matthew Heusser, Blog: http://xndev.blogspot.com General, you are listening to a machine. Do the world a favor and don't act like one. -- Dr. Stephen Falken, "War Games", The Movie -------------- next part -------------- An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Fri Oct 10 11:25:43 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 14:25:43 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> Message-ID: So.. not sure if the answer was given yet. Someone hinted, i thought, when they mentioned the ascii character set. The 'ge' comparison operator (i think) uses the ascii table to compare strings. On the ascii table, the character 0 (zero) is not the lowest. Most non-alphanumeric characters are below zero on the table. So for example, a right bracket will be lower than zero. It should be noted that, if I am right, i had to look it up. I did not know this off the top of my head. - Brian On Oct 10, 2008, at 10:52 AM, grand-rapids-pm-list at pm.org wrote: > Here's something that tripped me up that I thought I'd share as a > Perl Puzzle... > > Q1: For what value(s) of $a do the following comparisons return > different results? > > $a >= 0 > $a ge 0 > > Q2: Ok, it isn't hard to guess. But, why? The answer reveals > something unique about Perl that is very important to keep in mind. > > > Feel free to reply to the list. I'll post the answer later today if > no one nails it. > > -Ed > _______________________________________________ > grand-rapids-pm-list mailing list > grand-rapids-pm-list at pm.org > http://mail.pm.org/mailman/listinfo/grand-rapids-pm-list Brian Barto 616.856.4551 bbarto at stacksys.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Fri Oct 10 12:53:43 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 15:53:43 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> Message-ID: <2b5f94b80810101253y561a823alf0aabd527fc5e4a1@mail.gmail.com> Thanks for the responses! The exact answer I was looking for was Perl's value (non-value?) for undefined variables: "undef". When used in comparisons, an undefined variable is converted to either zero or the empty string depending on the comparison operator!! So, in the first example, $a (undef) evaluates to the numeric value 0, resulting in the comparison 0>=0 - which is true. In the second example, $a evaluates to the string value '' in the string comparison '' ge 0 - which is false. This chameleon-like behavior holds for other numeric and string operators, too, like '+' and '.' (concatenate), which is what's happening under-the-hood that allows Perl to handle the following with an undefined $a: $b = $a + 10; and $c = 'Hello ' . $a; Remember: For arithmetic operations undef behaves like 0, for sting operations like a zero length string. Related Tips: 1. Use if (defined $a) to test for undef. Never use the term 'undef' itself in comparisons! The following are NEVER true ? contrary to what you might expect! if ( $a == undef ) {...} # $a will always be converted to zero first in numeric comparison if ( $a eq undef ) {...} # $a will always be converted to empty string first in string comparison 2. use warnings! With warnings turned on, you will get a message on stderr like: Use of uninitialized value in string ge at ./test.pl line 18. Sometimes this message can be annoying because you may not really care to check if defined. Also note that the "uninitialized" warnings are only given at runtime. Doing perl ?cW test.pl will not give you any warning (not even for if ($a == undef), which I think would be nice because it is not possible). Like a rumple in the carpet, undef is something easily tripped over again and again! (I know from my own stumblings.) I did a grep through our code base and found a few occurrences of if ($var eq undef). Some further reading: http://www.softpanorama.org/Scripting/Perlbook/Ch02/variables.shtml Ed Eddington Priority Health Grand Rapids, MI -------------- next part -------------- An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Fri Oct 10 13:04:13 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Fri, 10 Oct 2008 16:04:13 -0400 Subject: [grand-rapids-pm-list] Perl Puzzle! In-Reply-To: <2b5f94b80810101253y561a823alf0aabd527fc5e4a1@mail.gmail.com> References: <2b5f94b80810100752m79c41b51p16201a9435328360@mail.gmail.com> <2b5f94b80810101253y561a823alf0aabd527fc5e4a1@mail.gmail.com> Message-ID: <2b5f94b80810101304q3b5fe75eh382e541e7523229@mail.gmail.com> Ok, I wasn't exactly correct on Tip #1. In these examples, I believe BOTH sides of the relation are first converted based on the operator. So, they do "work", but only because both sides are converted to either zero or the empty string. But, logically, the comparison is incorrect (and should thus be avoided! ;-). if ( $a == undef ) {...} # undef converted to zero in numeric comparison if ( $a eq undef ) {...} # undef converted to empty string in string comparison On Fri, Oct 10, 2008 at 3:53 PM, Ed Eddington wrote: > Thanks for the responses! > > The exact answer I was looking for was Perl's value (non-value?) for > undefined variables: "undef". When used in comparisons, an undefined > variable is converted to either zero or the empty string depending on the > comparison operator!! So, in the first example, $a (undef) evaluates to the > numeric value 0, resulting in the comparison 0>=0 - which is true. In the > second example, $a evaluates to the string value '' in the string comparison > '' ge 0 - which is false. > > This chameleon-like behavior holds for other numeric and string operators, > too, like '+' and '.' (concatenate), which is what's happening > under-the-hood that allows Perl to handle the following with an undefined > $a: > > $b = $a + 10; > and > $c = 'Hello ' . $a; > > Remember: For arithmetic operations undef behaves like 0, for sting > operations like a zero length string. > > Related Tips: > > 1. Use if (defined $a) to test for undef. Never use the term 'undef' itself > in comparisons! The following are NEVER true ? contrary to what you might > expect! > > if ( $a == undef ) {...} # $a will always be converted to zero first in > numeric comparison > if ( $a eq undef ) {...} # $a will always be converted to empty string > first in string comparison > > 2. use warnings! With warnings turned on, you will get a message on stderr > like: Use of uninitialized value in string ge at ./test.pl line 18. > Sometimes this message can be annoying because you may not really care to > check if defined. Also note that the "uninitialized" warnings are only given > at runtime. Doing perl ?cW test.pl will not give you any warning (not even > for if ($a == undef), which I think would be nice because it is not > possible). > > Like a rumple in the carpet, undef is something easily tripped over again > and again! (I know from my own stumblings.) I did a grep through our code > base and found a few occurrences of if ($var eq undef). > > Some further reading: > http://www.softpanorama.org/Scripting/Perlbook/Ch02/variables.shtml > > Ed Eddington > Priority Health > Grand Rapids, MI > -------------- next part -------------- An HTML attachment was scrubbed... URL: From grand-rapids-pm-list at pm.org Wed Oct 22 12:11:47 2008 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Wed, 22 Oct 2008 15:11:47 -0400 Subject: [grand-rapids-pm-list] ANN: Perl Mongers Meeting Oct 31 -- Scary Perl Message-ID: <48FF7AF3.9060107@umich.edu> Hello Everybody, Announcing the Grand Rapids Perl Mongers meeting for October 2008: Since it's Halloween, we'll be discussing all things terrifying about Perl! Come prepared to hear (and share) tales of scary, spooky, frightening Perl. Whatever fearsome Perl we can find, from the obscure to the obfuscated (but never ordinary) will be on display in a terrifyingly ghoulish freak show! *Date, Time and Location:* Friday, October 31 11:30 -- 1:00 PM PriorityHealth Conference Center (Room C) 3111 Leonard St NE, Grand Rapids, MI 49525 Map *Pizza:* Please RSVP to Matt Hahnfeld (Matt.Hahnfeld at priorityhealth.com), our Vice President and Pizza Guy, and he will count you towards the free pizza! Thank You, --Sean McMillan, Grand Rapids Perl Mongers Marketing Officer