Perl6 Object Oriented Cookbook (v0.2.1)  
Section 6: Context and Transformation  
 
Recipe 6.8: Reading an Object from a URI, File, or Stream
Last Updated: Sep 8, 2003
Status: Draft
      Previous Page   Next Page

How important is this problem to you?
  (Login to Vote)
5.00 Rating, 2 Votes  

How acceptable is the proposed solution?
  (Login to Vote)
0.00 Rating, 0 Votes  

Problem:  

You have a file, a stream, or a URI pointing to a serialized, stored object. You want to read the data and instantiate that object.

Solution:  

Use from or as:

class MyClass {
    from IOStream { ... }
}
my $obj = URI('ftp://serialized.txt') as MyClass;

Discussion:

Transformations exist between the URI, File and IOStream classes. If you want your class to be be instantiated via any one of these other classes, therefore, just put a from IOStream declaration in your class.

Issue: Having a one-liner for this is very, very cool. One catch: if MyClass has a transformer from string (quite possible), then it would interpret the URI as a string, and not do the longer URI --> IOStream --> MyClass transformation path. That needs a fix.


Log In to Comment


Login / Edit User Info -- Copyright © 2002 Cognitivity -- Previous Page   Next Page