First-Class Filehandle Trick in 5.8

S

Scott McGerik

I am using the First-Class Filehandle Trick, described at
http://perl.plover.com/local.html#3_The_First_Class_Filehandle_Tr.

In a particular program, I had this line:

$INFILE = do { local *FH }; $OUTFILE = do { local *FH };

I then proceed to open $INFILE for reading and $OUTFILE for writing with

unless ( open $INFILE, '<', $infile ) {

and

unless ( open $OUTFILE, '>', $outfile ) {


This worked in 5.6.1, but when I do the same under 5.8.0, I get the
following error message:

Filehandle FH opened only for output at ...

I find this confusing because I thought that

do { local *FH }

would return an unnamed glob. But from the error message, it appears
that it returned the glob for FH.

I looked through the perldelta file, at
http://www.perldoc.com/perl5.8.0/pod/perldelta.html, for 5.8, but I did
not find any hints. Pointers and explanations are appreciated.

Scott McGerik
 
B

Brian McCauley

Scott McGerik said:
I am using the First-Class Filehandle Trick, described at
http://perl.plover.com/local.html#3_The_First_Class_Filehandle_Tr.

In a particular program, I had this line:

$INFILE = do { local *FH }; $OUTFILE = do { local *FH };

I then proceed to open $INFILE for reading and $OUTFILE for writing with

unless ( open $INFILE, '<', $infile ) {

and

unless ( open $OUTFILE, '>', $outfile ) {


This worked in 5.6.1, but when I do the same under 5.8.0, I get the
following error message:

Filehandle FH opened only for output at ...

I find this confusing because I thought that

do { local *FH }

would return an unnamed glob. But from the error message, it appears
that it returned the glob for FH.

I looked through the perldelta file, at
http://www.perldoc.com/perl5.8.0/pod/perldelta.html, for 5.8, but I did
not find any hints. Pointers and explanations are appreciated.

I must admit that my gut feeling is that the semantics of do { local
*FH } would not be something I'd expect to be stable.

Indeed anything to do with putting bare GLOBs in to scalars as opposed
to GLOBrefs makes my head (and my guts) spin.

I suggest you ask MJD what are the 'definite disadvantages' he
perceives in using GLOBrefs like everybody else does.

(Note that as of 5.6.1[*] you have autovification of GLOBrefs in open()
et al).

All that said, I just tried MJD's hack in 5.8.0 and it worked just
fine. So this brings us back to the advice is the posting guidelines:
"First make a short (less than 20-30 lines) and /complete/ program
that illustrates the problem you are having. People should be able to
run your program by copy/pasting the code from your article."

[*] The autovification functionality appears in 5.6.1 but didn't get
into the POD in that version.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Brian McCauley
Indeed anything to do with putting bare GLOBs in to scalars as opposed
to GLOBrefs makes my head (and my guts) spin.

I suggest you ask MJD what are the 'definite disadvantages' he
perceives in using GLOBrefs like everybody else does.

I know only one such "defdis": for the target discussed above
(creation of anonymous unopened filehandles) the references "do not
work". Maybe it is fixed now, but at the time of 5.005 you could not
create a reference to an anonymous (and properly garbage-collected)
glob.

Thus the abomination used above.

Hope this helps,
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top