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