Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to get "<$myclass_instance>" to work?

Reply
Thread Tools

How to get "<$myclass_instance>" to work?

 
 
kj
Guest
Posts: n/a
 
      08-16-2007



I want to write a class whose instances are iterators that will
behave similarly to (readonly) file handles. I.e. I want to be
able to write:

my $thingie = Thingie->new( @args );
while ( <$thingie> ) {
# do stuff with $_
}

Can someone point me to a good example of a module in CPAN that
does this, for me to study? I'm looking specifically for a module
that achieves this functionality via standard Perl subclassing (as
opposed to tied variables, which I find too magic and mysterious).

TIA!

kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-16-2007
kj <(E-Mail Removed)> wrote:
> I want to write a class whose instances are iterators that will
> behave similarly to (readonly) file handles. I.e. I want to be
> able to write:
>
> my $thingie = Thingie->new( @args );
> while ( <$thingie> ) {
> # do stuff with $_
> }
>
> Can someone point me to a good example of a module in CPAN that
> does this, for me to study? I'm looking specifically for a module
> that achieves this functionality via standard Perl subclassing (as
> opposed to tied variables, which I find too magic and mysterious).


Have you already looked at the Iterator section of perldoc overload?
It isn't exactly an example, but it should be pretty informative if
you haven't already read it.

Surprisingly, I can't find any installed modules that use this feature of
overload. Maybe I just can't formulate the grep correctly to find them.

fgrep --include=\*.pm -r -C 4 'overload' /tools/perl/ | fgrep '<>'

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 11:34 am, kj <(E-Mail Removed)> wrote:
> I want to write a class whose instances are iterators that will
> behave similarly to (readonly) file handles. I.e. I want to be
> able to write:
>
> my $thingie = Thingie->new( @args );
> while ( <$thingie> ) {
> # do stuff with $_
> }
>
> Can someone point me to a good example of a module in CPAN that
> does this, for me to study?


I don't know about a CPAN module explicitly, but it's pretty easy for
me to create an example. <> is one of the overloadable operators with
the 'overload' pragma:

package MyClass;
use strict;
use warnings;
use overload
'<>' => \&iter,
;

sub new {
my $class = shift;
my $ref = { count => -1, things => [ @_ ] };
bless $ref, $class;
}

sub iter {
my $obj = shift;
my $count = ++$obj->{count};
if ($count == @{$obj->{things}}){
$obj->{count} = -1;
return;
}
return $obj->{things}[$count];
}

1;


> I'm looking specifically for a module
> that achieves this functionality via standard Perl subclassing (as
> opposed to tied variables, which I find too magic and mysterious).


You certainly could do it with tied variables. I think you'd want to
tie your object as a filehandle. But I agree, not much point in that
level of indirection in this case. Overloading the operator is
easier.

Here's a main file for you to test the above class on:

#!/usr/bin/perl
use strict;
use warnings;
use MyClass;

my $mc = MyClass->new(qw/foo bar baz/);

print "First\n";
while (my $thing = <$mc>) {
print "$thing\n";
}
print "Second\n";
while (my $thing = <$mc>) {
print "$thing\n";
}
__END__

(I iterated twice simply to prove that the overloaded <> operator
correctly returns undef when the list is exhausted, and resets the
counter correctly.)

Hope this helps,
Paul Lalli

 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      08-16-2007
In <(E-Mail Removed) om> Paul Lalli <(E-Mail Removed)> writes:

>On Aug 16, 11:34 am, kj <(E-Mail Removed)> wrote:
>I don't know about a CPAN module explicitly, but it's pretty easy for
>me to create an example. <> is one of the overloadable operators with
>the 'overload' pragma:


<snip>

Thank you very much, and also to Xho. I hadn't even *thought* of
using overload.

kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-16-2007
>>>>> "x" == xhoster <(E-Mail Removed)> writes:

x> kj <(E-Mail Removed)> wrote:
>> I want to write a class whose instances are iterators that will
>> behave similarly to (readonly) file handles. I.e. I want to be
>> able to write:
>>
>> my $thingie = Thingie->new( @args );
>> while ( <$thingie> ) {
>> # do stuff with $_
>> }


x> Surprisingly, I can't find any installed modules that use this feature of
x> overload. Maybe I just can't formulate the grep correctly to find them.

it isn't overload you want but tied. look at File::ReadBackwards for a
simple use of tying (or procedural).

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-16-2007
>>>>> "k" == kj <(E-Mail Removed)> writes:

k> In <(E-Mail Removed) om> Paul Lalli <(E-Mail Removed)> writes:
>> On Aug 16, 11:34 am, kj <(E-Mail Removed)> wrote:
>> I don't know about a CPAN module explicitly, but it's pretty easy for
>> me to create an example. <> is one of the overloadable operators with
>> the 'overload' pragma:


k> <snip>

k> Thank you very much, and also to Xho. I hadn't even *thought* of
k> using overload.

see my other post. tying is probably even easier than overloading. you
just need to create 2 methods that handle the tied interface (a
constructor and a readline). see File::ReadBackwards for a simple
working example.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 2:29 pm, Uri Guttman <(E-Mail Removed)> wrote:
> >>>>> "k" == kj <(E-Mail Removed)> writes:

>
> k> In <(E-Mail Removed) om> Paul Lalli <(E-Mail Removed)> writes:
> >> On Aug 16, 11:34 am, kj <(E-Mail Removed)> wrote:
> >> I don't know about a CPAN module explicitly, but it's pretty easy for
> >> me to create an example. <> is one of the overloadable operators with
> >> the 'overload' pragma:

>
> k> <snip>
>
> k> Thank you very much, and also to Xho. I hadn't even *thought* of
> k> using overload.
>
> see my other post. tying is probably even easier than overloading. you
> just need to create 2 methods that handle the tied interface (a
> constructor and a readline).


.... uh-huh, and with using overload, you only need the normal
constructor you were going to have anyway, and ONE method to handle
the operator.

(plus, the OP very specifically said he didn't want to mess with
tying)

Paul Lalli

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-16-2007
>>>>> "PL" == Paul Lalli <(E-Mail Removed)> writes:

PL> On Aug 16, 2:29 pm, Uri Guttman <(E-Mail Removed)> wrote:
>> >>>>> "k" == kj <(E-Mail Removed)> writes:


>> see my other post. tying is probably even easier than overloading. you
>> just need to create 2 methods that handle the tied interface (a
>> constructor and a readline).


PL> ... uh-huh, and with using overload, you only need the normal
PL> constructor you were going to have anyway, and ONE method to handle
PL> the operator.

well, we are counting the same things, a single constuctor (which is a
class method) and a single operator or readline method. i added tie
support to File::ReadBackwards by putting in two typeglob assignments to
alias the open and readline methods to the names needed by tied
handles. not much work at all and i would say less than if i did
overloading as that needs a mapping structure.

oops, i just checked and it has 5 method aliases. i started with 2 but a
bug/feature report requested eof, close and tell (those also needed new
short methods).

PL> (plus, the OP very specifically said he didn't want to mess with
PL> tying)

that i didn't see. but as i think it is less work, tying should be on
the table. maybe he was scared by what he thought it needed.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      08-17-2007
In <(E-Mail Removed). com> Paul Lalli <(E-Mail Removed)> writes:

>On Aug 16, 2:29 pm, Uri Guttman <(E-Mail Removed)> wrote:
>> >>>>> "k" == kj <(E-Mail Removed)> writes:

>>
>> k> In <(E-Mail Removed) om> Paul Lalli <(E-Mail Removed)> writes:
>> >> On Aug 16, 11:34 am, kj <(E-Mail Removed)> wrote:
>> >> I don't know about a CPAN module explicitly, but it's pretty easy for
>> >> me to create an example. <> is one of the overloadable operators with
>> >> the 'overload' pragma:

>>
>> k> <snip>
>>
>> k> Thank you very much, and also to Xho. I hadn't even *thought* of
>> k> using overload.
>>
>> see my other post. tying is probably even easier than overloading. you
>> just need to create 2 methods that handle the tied interface (a
>> constructor and a readline).


>... uh-huh, and with using overload, you only need the normal
>constructor you were going to have anyway, and ONE method to handle
>the operator.


Actually, as I just discovered, there's another problem with the
overload approach, and that is that the overloaded <> does not
transmit context information properly to the implementing routine.
There's mention of this in the docs. E.g. the following (incorrectly)
prints "0\n":

# in package 'main'
use overload '<>' => sub { print wantarray ? 1 : 0, "\n" };
my $x = bless +{};
my @x = <$x>;
__END__
0

(This may be have been fixed in more recent versions of overload
I'm using version 1.02).

kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      08-17-2007
In <(E-Mail Removed)> Uri Guttman <(E-Mail Removed)> writes:

>that i didn't see. but as i think it is less work, tying should be on
>the table. maybe he was scared by what he thought it needed.


No, actually, it's not the amount of work that scares, but rather
the fact that I've never gotten the hang of tied variables. For
me programming with them is always an exercise in trial and error;
I never quite know what to expect. Too much magic for my little
brain, I guess.

But I will study File::ReadBackwards. Thanks for the pointer.

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Serious cache problem - Page_Load get never get called !!! Ryan Park ASP .Net 2 01-03-2005 05:52 PM
Re: use anonymous but get get an authentication box Curt_C [MVP] ASP .Net 2 09-21-2004 06:39 PM
use anonymous but get get an authentication box =?Utf-8?B?U2hhcmlm?= ASP .Net 0 09-21-2004 01:01 PM



Advertisments