Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: How do I create new filehandles? (and an unrelated bug) Message-ID: <8814@jpl-devvax.JPL.NASA.GOV> Date: 20 Jul 90 21:35:56 GMT References: <1593@kuling.UUCP> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 64 In article <1593@kuling.UUCP> jand@kuling.UUCP (Jan Dj{rv) writes: : I want to make a sub which opens a file and returns a filehandle. : : Here's my naive solution: : : sub new_file { : local($file) = @_; : : open(F, "> $file") || die "Can't open $file\n"; : return *F; : } : : (The real application is a bit more involved...:-). : : However, this doesn't work, since perl uses the same fd for F in all calls : to new_file. : : *FIRST = &new_file('file_one'); : print fileno(FIRST), "\n"; : *SECOND = &new_file('file_two'); : print fileno(SECOND), "\n"; : exit 0; : : gives prints 3 two times. What is the proper way to do this? package newfile; $fh = 'fh00'; sub main'new_file { local($file) = @_; $fh++; open($fh, "> $file") || die "Can't open $file\n"; return $fh; } package main; $FIRST = &new_file('file_one'); print fileno($FIRST), "\n"; $SECOND = &new_file('file_two'); print fileno($SECOND), "\n"; exit 0; : With perl PL 18: : % perl -e 'print time;' : 648311529% : % perl -e 'print time, "\n";' : No comma allowed after filehandle at /tmp/perl-ea01682 line 1. : % perl -e 'print "", time, "\n";' : 648311550 : % : : The same thing seems to happens for any print statement that has a builtin : with no arguments as its first argument. : Like in : perl -e '$_ = 3.141;print cos, "\n";' : perl -e 'print eof, "\n";' : : and so on. : : A bug surely? Yes. Larry