[Israel.pm] What hurts you most in Perl?

Shlomi Fish shlomif at iglu.org.il
Wed Nov 24 11:10:58 PST 2010


On Tuesday 23 November 2010 18:32:35 sawyer x wrote:
> Hi Ronen,
> 
> On Tue, Nov 23, 2010 at 5:26 PM, Ronen Angluster 
<beerholder at gmail.com>wrote:
> > *$object->some_,method_that_does_not_exist();*
> > the only way i'll find out about the problem is in run time!!!!
> 
> Perl indeed doesn't have compile-time checks for methods. This is a design
> feature that other languages share (such as Python), which exists to allow
> certain behavior.

Indeed it is. Most dynamic languages have dynamic method resolution. For 
example:

[shell]
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ cat test.py
#!/usr/bin/env python
class Hello(object):
    def myhello(self):
        print "This is myhello."

h = Hello()
h.myhello()
h.non_existent_method()
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ python test.py
This is myhello.
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    h.non_existent_method()
AttributeError: 'Hello' object has no attribute 'non_existent_method'
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ cat test.rb
#!/usr/bin/env ruby
class Hello
    def myhello
        puts "This is myhello."
    end
end

h = Hello.new
h.myhello()
h.non_existent_method()
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ ruby test.rb
This is myhello.
test.rb:10: undefined method `non_existent_method' for #<Hello:0xb74b4b20> 
(NoMethodError)
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ cat test.bash
#!/bin/bash
myhello()
{
    echo "This is myhello"
}

myhello
non_existent_func
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ bash test.bash
This is myhello
test.bash: line 8: non_existent_func: command not found
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ cat test.scm
(define (myhello)
  (display "This is myhello")
  (newline))

(myhello)
(non_existent_func)
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ guile test.scm
This is myhello
ERROR: Unbound variable: non_existent_func
shlomif:~/progs/perl/snippets/dynamic-method-resolution$ 
[/shell]

So Python, Ruby, Bash and Scheme won't protect you against it. Part of the 
reason is that I can do "h = Hello.new()" and then later on "h = 
Goodbye.new()" and the language cannot statitically tell which type is going 
to be in any given point and which methods it will support.

The situation is better in languages such as C, C++ or Java, but even in C and 
C++ I can assign an arbitrary value to a pointer to a function and cause the 
program to crash at run-time, *after* it was compiled. 

> 
> Take, for example, the Perl Android interface (or even the Python one) and
> you'll see that the code is actually a thin layer that uses Autoloader to
> catch any method run and send it to the JSON RPC server. This is to allow
> you to automatically support _any_ command that is desired without changing
> the code, making the code much more sustainable (instead of volatile and
> risky). Moose (and many other accessors generators) use this ability to
> provide you with automa{t,g}ically-created accessors.
> 
> So, it's important to understand why this is happening and that it is on
> purpose.
> 
> Of course, your issues are still important and should be confronted and I
> don't mean to belittle them in any way (apologies if I gave such an
> impression). I guess what it means is that we should improve the static
> analysis tools that we have (Perl::Critic, Code::Sniff or some other
> PPI-based tool) to be able to spot these issues (if it don't already) and
> allow you to at least statically check for such issues. That is an
> interesting concept and I'm sure many would enjoy such an ability.
> 

Right.

Regards,

	Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/

<rindolf> She's a hot chick. But she smokes.
<go|dfish> She can smoke as long as she's smokin'.

Please reply to list if it's a mailing list post - http://shlom.in/reply .


More information about the Perl mailing list