Prev: worst concept in computer science [Was: to RG - Lisp lunacyand Perl psychosis]
Next: FAQ 8.2 How come exec() doesn't return?
From: Ben Morrow on 12 Mar 2010 21:38 Quoth John Bokma <john(a)castleamber.com>: > 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". I think I'll take this opportunity to recommend 'autodie' again. ~% perl -Mautodie -e'open my $X, "<", "/not/there"' Can't open '/not/there' for reading: 'No such file or directory' at -e line 1 ~% No thought required :). Ben
From: John Bokma on 12 Mar 2010 21:58 Ben Morrow <ben(a)morrow.me.uk> writes: > Quoth John Bokma <john(a)castleamber.com>: >> 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". > > I think I'll take this opportunity to recommend 'autodie' again. > > ~% perl -Mautodie -e'open my $X, "<", "/not/there"' > Can't open '/not/there' for reading: 'No such file or directory' at > -e line 1 > ~% > > No thought required :). Yup, I am aware of autodie I just have to start using it. Any chance it will be a core module in the near feature (as it replaces Fatal)? -- John Bokma j3b Hacking & Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl & Python Development
From: Ben Morrow on 12 Mar 2010 21:50 Quoth jt(a)toerring.de (Jens Thoms Toerring): > 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;-) Mine too, but it's still in the documentation and I have the sort of memory that keeps details like that while losing things like 'what I'm doing next week' :). > > 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! Sorry, that was badly put. What I meant was: In the simplest case, you pass open an already-existing filehandle as its first argument: open STDIN, "<", "foo" However, if you pass it a glob that doesn't have a filehandle in it yet, or a scalar variable containing undef, it will create a new filehandle for you and either put it in the glob or put a ref to it in the variable. (It's actually slightly more complicated in the 'variable' case: Perl doesn't just create a filehandle, it also creates a glob (with a made-up name) to put the filehandle in. I'm not entirely sure why it doesn't just put an IO ref in the variable instead.) Ben
From: Uri Guttman on 12 Mar 2010 22:37 >>>>> "BM" == Ben Morrow <ben(a)morrow.me.uk> writes: BM> Quoth jt(a)toerring.de (Jens Thoms Toerring): >> 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;-) BM> Mine too, but it's still in the documentation and I have the sort of BM> memory that keeps details like that while losing things like 'what I'm BM> doing next week' :). >> > 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! BM> Sorry, that was badly put. What I meant was: BM> In the simplest case, you pass open an already-existing filehandle BM> as its first argument: BM> open STDIN, "<", "foo" BM> However, if you pass it a glob that doesn't have a filehandle in it BM> yet, or a scalar variable containing undef, it will create a new BM> filehandle for you and either put it in the glob or put a ref to it BM> in the variable. BM> (It's actually slightly more complicated in the 'variable' case: Perl BM> doesn't just create a filehandle, it also creates a glob (with a made-up BM> name) to put the filehandle in. I'm not entirely sure why it doesn't BM> just put an IO ref in the variable instead.) well, underneath that io ref is a glob! why add extra layers you don't need? as for the made up glob name, it actually would be an anon glob which is what the Symbol module does. it allocates a named symbol but lets it leave scope and returns a ref to it. now the glob can't be accessed except by the ref. details to be found in that module. the whole concept of typeglobs and handles is a mess in perl anyhow. 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 13 Mar 2010 09:12
Quoth John Bokma <john(a)castleamber.com>: > Ben Morrow <ben(a)morrow.me.uk> writes: > > > Quoth John Bokma <john(a)castleamber.com>: > >> 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". > > > > I think I'll take this opportunity to recommend 'autodie' again. > > > > ~% perl -Mautodie -e'open my $X, "<", "/not/there"' > > Can't open '/not/there' for reading: 'No such file or directory' at > > -e line 1 > > ~% > > > > No thought required :). > > Yup, I am aware of autodie I just have to start using it. Any chance it > will be a core module in the near feature (as it replaces Fatal)? ~% corelist autodie autodie was first released with perl 5.010001 ~% Ben |