From 304d476fbe2f823fff8e35373b57361ee36de8e9 Mon Sep 17 00:00:00 2001 From: dakkar Date: Tue, 7 Feb 2006 16:03:34 +0000 Subject: aggiunte letture da fh, anche lazy git-svn-id: svn://luxion/repos/WebCoso/trunk@155 fcb26f47-9200-0410-b104-b98ab5b095f3 --- lib/WebCoso/Resource.pm | 42 ++++++++++++++++++++++++++++++++++++++++++ t/04-resource.t | 30 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/lib/WebCoso/Resource.pm b/lib/WebCoso/Resource.pm index 077c577..be9a199 100644 --- a/lib/WebCoso/Resource.pm +++ b/lib/WebCoso/Resource.pm @@ -70,6 +70,48 @@ sub get_property { return; } +sub get_property_fh { + my $self=shift; + my $axes_spec= (ref($_[0]) eq 'HASH') ? (shift) : {}; + my ($prop_name)=@_; + + my $prop_value=$self->get_property($axes_spec,$prop_name); + + if (ref($prop_value) eq 'CODE') { + $prop_value=$prop_value->(); + } + + if (ref($prop_value)) { # speriamo filehandle... + open my $fh,'<&',$prop_value; # dup in lettura + seek $fh,0,0; + return $fh; + } + else { # speriamo stringa... + open my $fh,'<',\$prop_value; + return $fh; + } +} + +sub get_property_string { + my $self=shift; + my $axes_spec= (ref($_[0]) eq 'HASH') ? (shift) : {}; + my ($prop_name)=@_; + + my $prop_value=$self->get_property($axes_spec,$prop_name); + + if (ref($prop_value) eq 'CODE') { + $prop_value=$prop_value->(); + } + + if (ref($prop_value)) { # speriamo filehandle... + seek $prop_value,0,0; + return do {local $/;<$prop_value>} + } + else { + return "$prop_value"; + } +} + sub get_collections { my ($self)=@_; diff --git a/t/04-resource.t b/t/04-resource.t index 99e7cf3..808629f 100644 --- a/t/04-resource.t +++ b/t/04-resource.t @@ -52,3 +52,33 @@ is_deeply([sort $r->get_axes()],['a1','a2'],'2 axes'); is_deeply([sort $r->get_axis_values('a1')],[1,2,3],'1st axis, 3 values'); is_deeply([sort $r->get_axis_values('a2')],[2],'2nd axis, 1 value'); +# test sui vari get_property_* + +$r=WebCoso::Resource->new(); + +{ +open my $fh,'<',__FILE__; +$r->set_property(fh=>$fh); +} +{ +my $fh=$r->get_property_fh('fh'); +is((scalar <$fh>),"#!/usr/bin/perl\n",'legge fh'); +} +{ +my $content=$r->get_property_string('fh'); +like($content,qr{^#!/usr/bin/perl\n},'legge string'); +} + +$r->set_property(fh=> + sub { + open my $fh,'<',__FILE__; + return $fh; + }); +{ +my $fh=$r->get_property_fh('fh'); +is((scalar <$fh>),"#!/usr/bin/perl\n",'legge fh lazy'); +} +{ +my $content=$r->get_property_string('fh'); +like($content,qr{^#!/usr/bin/perl\n},'legge string lazy'); +} -- cgit v1.2.3