For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2004 > objects and code references









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author objects and code references
Jeff Thies

2004-06-28, 3:56 am

I've created an object with a method like this:

use File::Find;

sub someSub{
my $self=shift;
find(\&wanted,'some_directory');
....
}

I need for the wanted sub to read some of the object properties. I'm unsure
how to do this.

I can't do this:
find(\&$self->wanted,...)

and I can't do this:
find(\&wanted($self->{some_property}),...

So, what do I do?

Can I make a reference to $self->wanted? Looks like I've come to the point
where I need to understand that "&" and references to subs.

Cheers,
Jeff



Tassilo v. Parseval

2004-06-28, 3:56 am

Also sprach Jeff Thies:

> I've created an object with a method like this:
>
> use File::Find;
>
> sub someSub{
> my $self=shift;
> find(\&wanted,'some_directory');
> ...
> }
>
> I need for the wanted sub to read some of the object properties. I'm unsure
> how to do this.
>
> I can't do this:
> find(\&$self->wanted,...)
>
> and I can't do this:
> find(\&wanted($self->{some_property}),...
>
> So, what do I do?


Create an additional sub-wrapper around File::Find's first argument:

sub someSub {
my $self = shift;
find( sub { wanted($self) }, 'directory' );
...
}

Now the wanted function is called with this one additional argument each
time File::Find has found an entry. The variables $_, $File::Find::dir
and $File::Find::name are global and so they are still accessible in
'wanted'.

> Can I make a reference to $self->wanted? Looks like I've come to the point
> where I need to understand that "&" and references to subs.


You can only make references to functions but not to method calls.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{re
htonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
Ben Morrow

2004-06-28, 4:09 pm


Quoth tassilo.parseval@post.rwth-aachen.de:
> Also sprach Jeff Thies:
>
>
> Create an additional sub-wrapper around File::Find's first argument:
>
> sub someSub {
> my $self = shift;
> find( sub { wanted($self) }, 'directory' );


find sub { $self->wanted }, 'directory';

so it's a proper method call and will do inheritance.

Ben

--
Joy and Woe are woven fine,
A Clothing for the Soul divine William Blake
Under every grief and pine 'Auguries of Innocence'
Runs a joy with silken twine. ben@morrow.me.uk
Tassilo v. Parseval

2004-06-28, 8:58 pm

Also sprach Ben Morrow:

> Quoth tassilo.parseval@post.rwth-aachen.de:
>
> find sub { $self->wanted }, 'directory';
>
> so it's a proper method call and will do inheritance.


Only if the object $self has a method 'wanted'. This isn't quite clear
from the original posting.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{re
htonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
Jeff Thies

2004-06-29, 4:07 pm


"Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote in message
news:2kbff6Fco4aU1@uni-berlin.de...
> Also sprach Ben Morrow:
>
unsure
>
> Only if the object $self has a method 'wanted'. This isn't quite clear
> from the original posting.


Thanks, I've used it that way.

It's easy to be unclear when you are not sure how to do something!

Cheers,
Jeff
>
> Tassilo
> --
>

$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
>

pam{rekcahbus})(rekcah{lrePbus})(lreP{re
htonabus})!JAPH!qq(rehtona{tsuJbus#;[col
or=darkred]
>

$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com