Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

There's more than one way to do it . . . by accident

by Ovid (Cardinal)
on Aug 28, 2000 at 08:18 UTC ( [id://29952]=perlmeditation: print w/replies, xml ) Need Help??

A friend of mine who has a Java background was getting excited about some of the things that he can do in Perl. Because of his background, he is used to using the "dot" syntax to access methods and properties in objects.

I was looking at one of his scripts and he was asking why a particular hack of his worked. Seems he was trying to access the length "property" of an array, but had an interesting workaround to get it right:

my @stuff = qw(one two three); my $length = (@stuff.length)/10; print $length;
He was a little put out when I couldn't stop lauging.

Cheers,
Ovid

A little hint for monks who don't get what's going on right off the bat: the dot in this context acts as a concatenation operator.

Replies are listed 'Best First'.
RE: There's more than one way to do it . . . by accident
by gaspodethewonderdog (Monk) on Aug 28, 2000 at 16:21 UTC
    That is too funny... and proves once again that no matter how good a programmer you are you can always manage hacks to do the thing you want and rationalize it.

    I hope he's never had to divide by ten to get the proper length of a string in Java... and it really ought to make sense that you don't have to do it in Perl as well.

    I've had my fair share of goof ups myself. I remember back when I was writing VAX assembly I had several routines that I made more efficient by using side effects of instructions... when parts of the system got updated I had basically shot myself in the foot because I didn't no idea why the code was not working.

    And doesn't Hall & Schwartz in "Effective Perl Programming" say something like 'Never take anything for granted'... if you can't see an obvious explanation for why something works don't try and make something up to explain it away... figure out what the real reason is.

RE: There's more than one way to do it . . . by accident
by Adam (Vicar) on Aug 28, 2000 at 20:46 UTC
    That's amusing, but it's also a good example of why you should use -w. Your friend would have gotten the warning, "Use of uninitialized value in length at -e line 2."
RE: There's more than one way to do it . . . by accident
by jeorgen (Pilgrim) on Aug 29, 2000 at 02:00 UTC
    Well, I was guilty of doing something similar about a year ago. A friend asked one evening if it was possible to calculate the compound rate of interest in perl, and I whipped up a program that output a plausible result after 30 years given a certain rate of interest. (Actually I think it was in reverse, given a certain end sum, but anyway)

    I had however never used the exponentiation operator in perl (**) and accidentally used the one in Hypertalk (^).

    This meant that my program made a bit-wise XOR instead of a rate of interest for each year. It alll become clear when we printed the increase in wealth per annum, which was kind of jerky, to say the least :-)

    /jeorgen

RE: There's more than one way to do it . . . by accident
by Anonymous Monk on Aug 28, 2000 at 17:52 UTC
    Thats the !$#@ funniest thing I've heard all day. Bill
RE: There's more than one way to do it . . . by accident
by xdb19 (Sexton) on Aug 29, 2000 at 19:02 UTC
    That's one of the funnier things i've heard today... although, we were all newbies once, and we al made mistakes... funny ones at that. so we cannot criticize your friend too much Ovid.
    -Have Fun, XDB19

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://29952]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-28 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found