new
[urisagit/Perl-Docs.git] / t / stringify.t
CommitLineData
d54c66dd 1#!perl -T
2
3use strict;
4
5use Test::More;
6use File::Slurp;
7use IO::Handle ;
8use UNIVERSAL ;
9
10plan tests => 3 ;
11
12my $path = "data.txt";
13my $data = "random junk\n";
14
15# create an object with an overloaded path
16
17my $obj = FileObject->new( $path ) ;
18
19isa_ok( $obj, 'FileObject' ) ;
20is( "$obj", $path, "object stringifies to path" );
21
22write_file( $obj, $data ) ;
23
24my $read_text = read_file( $obj ) ;
25is( $data, $read_text, 'read_file of stringified object' ) ;
26
27unlink $path ;
28
29exit ;
30
31# this code creates the object which has a stringified path
32
33package FileObject;
34
35use overload
36 q[""] => \&stringify,
37 fallback => 1 ;
38
39sub new {
40 return bless { path => $_[1] }, $_[0]
41}
42
43sub stringify {
44 return $_[0]->{path}
45}