News for hackers. Stuff that matters.
Re:Oh My Glob by lloeki on Tuesday November 19, 2013 @05:39AM (#6760319)
Honest questions since my perl-fu is weak.

Since python's file class already implements the with protocol, I don't see how

    sub do_a_thing {
        my $file = io->file(".bashrc");
        print $file->slurp;
    }
is different from

    def do_a_thing:
        with open(".bashrc") as f:
            print(f.read())
Conversely I don't see how

> Thus objects can carry their teardown around in their class definition (which can be easily modified by overriding or wrapping the method in a new class)

is any different from implementing the with protocol in the class, which is strictly speaking "carry their teardown around in their class definition"

Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @05:54AM (#6760360)
You answered yourself. What about classes that don't implement the with protocol? In Perl an object is guaranteed to be garbage collected even if it doesn't have a DESTROY method and without requiring any special syntax.
Re:Oh My Glob by lucian1900 on Tuesday November 19, 2013 @07:44AM (#6760744)
All Python objects do get destructed at the end of the scope. The only exception are reference cycles, which get broken by a simple GC. Perl doesn't even have a cycle detector, so cyclic references will never get destructed.

Python has a __del__ method that will be called on destruction, but is not guaranteed to be called in the case of cycles. That's what the with statement solves, guaranteeing destruction at the end of the block. It's common to have both __del__ and __exit__ call the same thing.

Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @11:21AM (#6762185)
They get destructed AFTER end of scope, not AT end of scope, regardless of the existence of cycles. See:

http://eli.thegreenplace.net/2009/06/12/safely-using-destruc...