Prev: Problems with "show tech" using the Net::Telnet Module
Next: getting full URL from relative links
From: Peter J. Holzer on 21 Apr 2010 18:56 On 2010-04-21 15:42, Ben Morrow <ben(a)morrow.me.uk> wrote: > File::Copy (at least, v2.14, which comes with 5.10.1) uses > > my $fh = \do { local *FH }; > > which is how you created a scoped filehandle before 5.6. At least in > modern perls, these handles will also auto-close on scope exit. Ah, nice trick. Too bad I learn about it only now that I don't need it any more :-). hp
From: Uri Guttman on 21 Apr 2010 19:09 >>>>> "PJH" == Peter J Holzer <hjp-usenet2(a)hjp.at> writes: PJH> On 2010-04-21 15:42, Ben Morrow <ben(a)morrow.me.uk> wrote: >> File::Copy (at least, v2.14, which comes with 5.10.1) uses >> >> my $fh = \do { local *FH }; >> >> which is how you created a scoped filehandle before 5.6. At least in >> modern perls, these handles will also auto-close on scope exit. PJH> Ah, nice trick. Too bad I learn about it only now that I don't need it PJH> any more :-). even older or at least the same age is the Symbol module which basically does the same thing. it first increments a string to create a new symbol, then allocates that glob and deletes the symbol. you get back the anon glob which can be used for any handle. the above code does a similar but shorter trick in temporarily allocating a new glob (the local) and then getting a reference to it while the local leaves scope. so no one else has access to the new glob which makes it anon too. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Ben Morrow on 21 Apr 2010 19:34 Quoth "Peter J. Holzer" <hjp-usenet2(a)hjp.at>: > On 2010-04-21 15:42, Ben Morrow <ben(a)morrow.me.uk> wrote: > > File::Copy (at least, v2.14, which comes with 5.10.1) uses > > > > my $fh = \do { local *FH }; > > > > which is how you created a scoped filehandle before 5.6. At least in > > modern perls, these handles will also auto-close on scope exit. > > Ah, nice trick. Too bad I learn about it only now that I don't need it > any more :-). It's still occasionally useful, for instance with IPC::Open3 which can't autoviv handles (because it already had a meaning for undef before 5.6). As Uri says, you can also use the Symbol module; in principle if you're creating lots of filehandles you could use Symbol::geniosym or do { local *FH; *FH{IO} } to save a bit of memory. (An IO ref ought to work as a filehandle everywhere a globref does, and you save the GV+GP+SV that you aren't using.) Ben
First
|
Prev
|
Pages: 1 2 3 Prev: Problems with "show tech" using the Net::Telnet Module Next: getting full URL from relative links |