summaryrefslogtreecommitdiff
path: root/t/stringify.t
blob: c3809cb753bf42af19f022e13c0f0b66197239d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!perl -T

use strict;

use Test::More;
use File::Slurp;
use IO::Handle ;
use UNIVERSAL ;

plan tests => 3 ;

my $path = "data.txt";
my $data = "random junk\n";

# create an object with an overloaded path

my $obj = FileObject->new( $path ) ;

isa_ok( $obj, 'FileObject' ) ;
is( "$obj", $path, "object stringifies to path" );

write_file( $obj, $data ) ;

my $read_text = read_file( $obj ) ;
is( $data, $read_text, 'read_file of stringified object' ) ;

unlink $path ;

exit ;

# this code creates the object which has a stringified path

package FileObject;

use overload
	q[""]	=> \&stringify,
	fallback => 1 ;

sub new {
	return bless { path => $_[1] }, $_[0]
}

sub stringify {
	return $_[0]->{path}
}