Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 15th, 2008, 03:56 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 8,313 Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 4 Days 4 h 23 m 47 sec
Reputation Power: 2529
Perl puzzle of the week/month/year

Ok, we haven't done one of these in a while. I'll keep this one short and sweet.
Code:
#!/usr/bin/perl -w

$foo = "0 but true";
$bar = $foo + 5;
print "$bar\n";

Run this and it'll run fine:
Code:
mb@motorhead:~/perl> ./funny_perl
5


Now change the $foo line to:
Code:
#!/usr/bin/perl -w

$foo = "1 but true";
$bar = $foo + 5;
print "$bar\n";

and run it and you'll see a warning:
Code:
mb@motorhead:~/perl> ./funny_perl
Argument "1 but true" isn't numeric in addition (+) at ./funny_perl line 5.


Now change the $foo line to:
Code:
#!/usr/bin/perl -w

$foo = "0 but false";
$bar = $foo + 5;
print "$bar\n";

and run it and you'll see a warning:
Code:
mb@motorhead:~/perl> ./funny_perl
Argument "0 but false" isn't numeric in addition (+) at ./funny_perl line 5.


So your question is: Why does the first program not emit a warning when the other two do?
EXTRA BONUS: Is there a reason behind this madness?

Usual puzzle rules apply --
* Answer will be supplied next Friday, if no one can solve it by then.
* Winner will be determined by me and the person(s) replying with the first correct answer will win fame by having their name in green letters in my signature until I decide what the next contest is.
* Judge's (namely me) decision is final .
* There is no fight club.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Fishmonger, superior perl programmer of the month

Last edited by Scorpions4ever : August 15th, 2008 at 03:59 PM.

Reply With Quote
  #2  
Old August 15th, 2008, 04:25 PM
Matt1776's Avatar
Matt1776 Matt1776 is offline
Recovering Intellectual
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Orange County, CA
Posts: 1,170 Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level)Matt1776 User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 6 Days 2 h 40 m 41 sec
Reputation Power: 620
Send a message via ICQ to Matt1776 Send a message via Skype to Matt1776
Easy.

There is a bug in the -w functionality of the perl interpreter.

Pfft.
__________________
Bugs that go away by themselves come back by themselves
Never take life seriously, Nobody gets out alive anyway.

Reply With Quote
  #3  
Old August 15th, 2008, 04:46 PM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 3,470 keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 16 h 3 m 25 sec
Reputation Power: 1123
If a string starts with a number, perl evaluates it as a number (when used in a numeric context) up to the first character that doesn't make sense as a number. A warning was still generated to show there may be something more in the string to be concerned about. Doesn't stop program execution though.

However, the string "0 but true" is a special case built into perl.
Code:
#!/usr/bin/perl -w
use strict;

my $r = '0 but true';

print "r is true\n" if $r;
print "r is zero\n" if $r == 0;

You could return that string from a routine to show that while there were no results, there was also no error.

Similar to how the DBI module returns "0E0", which acts in the same way. Means there were no rows affected, but evaluates as 'true'; no error occurred.
Comments on this post
derelict agrees: dammit -- only one name in green letters this month, you left no room for further explanation! ')e)

Last edited by keath : August 15th, 2008 at 04:54 PM. Reason: oops, I changed my variable names

Reply With Quote
  #4  
Old August 15th, 2008, 08:23 PM
KevinADC's Avatar
KevinADC KevinADC is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2004
Location: Sunny Southern California
Posts: 2,687 KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 12 h 54 m 48 sec
Reputation Power: 857
Keaths answer explains a lot, but not quite why the first program does not issue a warning, the answer to that is:

Quoted from perldoc:

Quote:
The special string "0 but true" is exempt from -w complaints about improper numeric conversions.


Am I splitting hairs? Definetly.

Reply With Quote
  #5  
Old August 20th, 2008, 07:12 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 8,313 Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 4 Days 4 h 23 m 47 sec
Reputation Power: 2529
Boy, you guys are good.

Also, the reason for "0 but true" is because of a few *nix functions (such as ioctl() and fcntl()). When Larry wrapped these around perl built-ins, he had a problem because 0 is a valid return value for these C functions (they return -1 on failure and 0 or +ve values on success). Now you couldn't do this in perl anymore, if the functions returned 0:
Code:
ioctl($fd, $what, $value) or die "ioctl() no workie\n";

So he made these functions return "0 but true" when the underlying C code returned 0, so code like the above would work.

BTW this type of issue is called the semi-predicate problem. I first saw the "0e0" in DBI, but Mark Jason Dominus' book also mentions it as a way around the problem. Larry just used "0 but true" to work around it instead of "0e0". Then he hacked perl so that it would specifically NOT warn if you tried to do arithmetic with "0 but true". It is an ugly hack, but no one said that Larry was sane.

Full marks to Keath and KevinADC for getting the answer correctly.

Reply With Quote
  #6  
Old August 22nd, 2008, 10:39 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 3,470 keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level)keath User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 16 h 3 m 25 sec
Reputation Power: 1123
Thanks for posting. I enjoy your puzzles and hope you'll continue.

Reply With Quote
  #7  
Old August 9th, 2009, 11:44 AM
Axweildr's Avatar
Axweildr Axweildr is offline
'fie' on me, allege-dly
Dev Shed God 15th Plane (12000 - 12499 posts)
 
Join Date: Mar 2003
Location: in da kitchen ...
Posts: 12,277 Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)  Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 1 Week 3 Days 13 h 14 m 41 sec
Reputation Power: 4721
Send a message via Google Talk to Axweildr
Orkut
time for a new one?
__________________
--Ax
without exception, there is no rule ...
Heavy Haulage Ireland
Targeted Advertising Cookie Optout (TACO) extension for Firefox
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


09 F9 11 02
9D 74 E3 5B
D8 41 56 C5
63 56 88 C0
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
-- Jamie Zawinski
Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...

Reply With Quote
  #8  
Old August 9th, 2009, 02:46 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 8,313 Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 4 Days 4 h 23 m 47 sec
Reputation Power: 2529
Sounds like a plan. I'll post a new one tomorrow, when I get to work.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Perl puzzle of the week/month/year


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
Stay green...Green IT