Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: File::Find incompatible with OO perl?

by broquaint (Abbot)
on Feb 09, 2004 at 16:50 UTC ( #327662=note: print w/replies, xml ) Need Help??


in reply to File::Find incompatible with OO perl?

Since find expects a subroutine reference your first example won't work as it'll only get the return of the call to $obj->found. The idiomatic way of passing a method to something that expects a subroutine is to pass an anonymous wrapper function e.g
find(sub { $obj->found }, $directory);
Or if you want the anonymous sub call to disappear from the calling stack
find(sub { unshift @_,$obj; goto &{$obj->can('found')} }, $directory);
However, if you're planning to go the OO route then File::Find::Rule and File::Finder would be betters suited than File::Find.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: File::Find incompatible with OO perl?
by demerphq (Chancellor) on Feb 09, 2004 at 18:28 UTC

    Or if you want the anonymous sub call to disappear from the calling stack

    Ooh, that is just so deliciously evil I must find a justification to use it...


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


Re: Re: File::Find incompatible with OO perl?
by vaevictus (Pilgrim) on Feb 09, 2004 at 17:09 UTC
    Just curious, but why would I "want the anonymous sub call to disappear from the calling stack"?
      In case you croak (or something else that produces a stacktrace) within the the found method e.g
      use Carp 'croak'; sub exec_anon { &{$_[0]} } sub boom { eval { croak "$_[0]\->boom" }; print "trace: $@" } exec_anon sub { main->boom }; exec_anon sub { unshift @_, 'main'; goto &{main->can("boom")} }; __output__ trace: main->boom at - line 4 eval {...} called at - line 4 main::boom('main') called at - line 6 main::__ANON__ called at - line 3 main::exec_anon('CODE(0x8107e64)') called at - line 6 trace: main->boom at - line 4 eval {...} called at - line 4 main::boom called at - line 3 main::exec_anon('main', 'CODE(0x8117378)') called at - line 7
      Not terrifically important, or necessarily desired, but it can be much easier to follow things if wrapper functions are hidden away and it's safer if you're dealing with code that looks up the stack (for whatever reason). Use with caution™.
      HTH

      _________
      broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://327662]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-05-18 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?