From: Ben Morrow on

Quoth jt(a)toerring.de (Jens Thoms Toerring):
> cerr <ron.eggler(a)gmail.com> wrote:
> > I'm just trying to open a text file and print line by line.
> > My Code:
> > my $HANDLE = $filename;
> > open(HANDLE) or die("Could not open GPS source file.");
>
> Because your use of open() is completely broken. No kind of
> open() function I have ever seen (as far as I remember) works
> like that.

It's a Perl 4ism that is never used nowadays, but still documented and
supported. A bare

open HANDLE;

takes the filename from the global $HANDLE in the current package. In
the OP's case it's failing because he sets a lexical $HANDLE instead of
a global.

> All take a file name and return a handle that in
> subsequent calls of functions to read from or write to the
> file etc. is used.

open doesn't return a filehandle (though some things would be more
convenient if it did). It opens an existing filehandle, auto-vivifying
it if necessary. (It is perfectly OK to pass an already-open filehandle
to open, and it will close it first.)

Ben

From: Jens Thoms Toerring on
Ben Morrow <ben(a)morrow.me.uk> wrote:
> Quoth jt(a)toerring.de (Jens Thoms Toerring):
> > cerr <ron.eggler(a)gmail.com> wrote:
> > > I'm just trying to open a text file and print line by line.
> > > My Code:
> > > my $HANDLE = $filename;
> > > open(HANDLE) or die("Could not open GPS source file.");
> >
> > Because your use of open() is completely broken. No kind of
> > open() function I have ever seen (as far as I remember) works
> > like that.

> It's a Perl 4ism that is never used nowadays, but still documented and
> supported. A bare

> open HANDLE;

Uuups, that's from before my time with Perl. And do I feel lucky;-)

> takes the filename from the global $HANDLE in the current package. In
> the OP's case it's failing because he sets a lexical $HANDLE instead of
> a global.

> > All take a file name and return a handle that in
> > subsequent calls of functions to read from or write to the
> > file etc. is used.

> open doesn't return a filehandle (though some things would be more
> convenient if it did). It opens an existing filehandle, auto-vivifying
> it if necessary. (It is perfectly OK to pass an already-open filehandle
> to open, and it will close it first.)

Mmmm, what means auto-vivifying in this context? And what "opens an
existing file handle"? Obviously, not understanding enough I just see
a variable, not an "existing file handle". Since I can't remember you
being wrong on such things I guess there's some kind of magic going on
I didn't grok yet, so a bit more of explanation would be great!

And I also didn't know about a file getting closed automatically when
the file handle is re-used. What happens when you do e.g.

open my $h1, '<', 'filename1.txt';
my $h2 = $h1;
open $h1, '<', 'filename2.txt';

Does that close the first file or does it stay open because of
'$h2' still refering to it?
Best regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: John Bokma on
jt(a)toerring.de (Jens Thoms Toerring) writes:

> open my $handle, '<', $filename or die "Can't open file\n";

To me the acceptable minimum for the rhs of or would be:

die "Can't open '$filename': $!";

this reports and the filename and why it couldn't be opened.


Personally I prefer to add "for reading" since I consider it more clear
compared to just "Can't open".

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
From: Uri Guttman on
>>>>> "JTT" == Jens Thoms Toerring <jt(a)toerring.de> writes:

>> It's a Perl 4ism that is never used nowadays, but still documented and
>> supported. A bare

>> open HANDLE;

JTT> Uuups, that's from before my time with Perl. And do I feel lucky;-)

and it still is supported. nasty old concept. it is used in golfing
sometimes.

>> open doesn't return a filehandle (though some things would be more
>> convenient if it did). It opens an existing filehandle, auto-vivifying
>> it if necessary. (It is perfectly OK to pass an already-open filehandle
>> to open, and it will close it first.)

JTT> Mmmm, what means auto-vivifying in this context? And what "opens an
JTT> existing file handle"? Obviously, not understanding enough I just see
JTT> a variable, not an "existing file handle". Since I can't remember you
JTT> being wrong on such things I guess there's some kind of magic going on
JTT> I didn't grok yet, so a bit more of explanation would be great!

open my $handle will autovivify a handle in the scalar if it was
undef. same as $ref->{foo} will do if $ref is undef. it used to be you
had to have a typeglob or ref to one in the variable so open would
work. the better lazy way is for perl to do it for you.

JTT> And I also didn't know about a file getting closed automatically when
JTT> the file handle is re-used. What happens when you do e.g.

JTT> open my $h1, '<', 'filename1.txt';
JTT> my $h2 = $h1;
JTT> open $h1, '<', 'filename2.txt';

JTT> Does that close the first file or does it stay open because of
JTT> '$h2' still refering to it?

the handle isn't really in the scalar, but is refered to by the scalar
(the handle is actually a typeglob entry). the file itself will be
closed as will the handle itself. the extra copy now refers to a closed
file and will spit out an error if used for i/o.

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: J�rgen Exner on
cerr <ron.eggler(a)gmail.com> wrote:
>I'm just trying to open a text file and print line by line.
>My Code:
> my $HANDLE = $filename;
> open(HANDLE) or die("Could not open GPS source file.");

This style is an anachronism from the dark ages, supported only for
backward compatibility of ancient code.

Today you should use the three-argument form of open() with a lexical
file handle
open ($Handle, '<', $filename)
and in the error message I would at least include the filename and the
reason why the error occured:
or die ("Couldn't open GPS source file '$filename': $!";)

jue