Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: localizing handles with anonymous subroutines

by jmcnamara (Monsignor)
on Jun 10, 2002 at 13:30 UTC ( #173118=note: print w/replies, xml ) Need Help??


in reply to localizing handles with anonymous subroutines


Is there an advantage of this method over do?
my @files = do { local *DH; opendir( DH, $dir ) or die 'Cannot open ' . $dir; grep { -f $_ } readdir(DH); };

--
John.

Replies are listed 'Best First'.
Re^2: localizing handles with anonymous subroutines
by particle (Vicar) on Jun 10, 2002 at 16:13 UTC
    yes, there's a huge advantage. do is not a subroutine. it cannot take parameters. just try to do this!

    my %dispatch = ( sub_get_files => sub{ # takes one arg local *DH; opendir( DH, $_[0] ) or die 'Cannot open ' . $_[0]; return grep { -f $_ } readdir(DH) }, ); my @files = $dispatch{sub_get_files}->( $dir );
    do executes immediately, and would spit the results into the hash. icky.<p

    ~Particle *accelerates*

Log In?
Username:
Password:

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

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



    Results (81 votes). Check out past polls.

    Notices?