Open Grinnz opened 6 years ago
One other thing for the "could be mentioned" list: <> is really just the builtin readline() function, except it can also mean glob() based on statically parsing what's between the arrows. Personally I prefer calling readline directly because of this, but since it's a static parsing difference it's pretty hard to get wrong (but for example, you could confuse it with something as simple as < $fh >
or <$hash{fh}>
) and most Perl users prefer the <> syntax because that's what every tutorial uses.
1. About extra modules: I could just take out the "CPAN" part. Examples of File::Temp would be cool but I worry the article is too long already. It might make sense in a different article. I want these articles bite sized. I'd rather have more articles than a really long wall of text which is intimidating to anyone coming in who just wanted to learn some basic file io.
2. Bareword handles: I went back and forth. Again I'm trying to keep it short. But I like your way of explaining it. Maybe I can fit in something abbreviated.
3. <<>>: Yeah definitely a whole other article. This is about files.
4. Read the whole file into memory. It depends if you want a string or an array at the end right? Personally I never slurp into a string because performance rarely matters and also I usually want an array because I'm going to parse something line by line.
5. File test operators: woah! I did not know about that one. Very interesting. That is a crazy name for an identifier. I don't think I want to tell anyone about it though. It makes the code hard to read? I guess though that's an advantage of using a file handle with file test operators more generally? You won't need to re-stat. I never considered that. That might be worth mentioning.
6. IO::Handle: Nice! I know about IO::Handle, but I always wondered why print doesn't take a comma after the filehandle. That's so annoying. By indirect object syntax do you mean that print $fh $foo
is equal to $fh->print($foo)
in the same way that new Foo
is equal to Foo->new()
? Hmm maybe I used to know this at one point.
Unfortunately almost no one uses $fh->print($string)
. So I'm not sure its worth pointing out. Unless its to explain the infuriating lack of a comma. I'll think about it.
7. <>: Interesting. I didn't know that either. I'll think about putting that in too. This kind of stuff is fantastic.
Thanks for all the feedback. I have a feeling this article is going to be delayed another week. Busy time at work + that's a lot of info to think about.
re: file test operators, it's not that you should use filehandles in general, but the _ special filehandle is used specifically for this, to tell it to use the last stat. A nicer way is to just use File::stat (it is core also :)
Overall great article. These are mostly nitpicks or possible additions, depending how much you want this page to help with reading existing code vs. writing new code.
{ local $/; my $contents = <$filehandle>; }
to avoid the overhead of parsing into lines. But the list context return of <>/readline is also worth showing.next unless -e $path; next unless -f _;
use IO::Handle ();
(core) somewhere, you can call all filehandle-specific functions as methods on the handle itself (also works on File::Temp handles). For example:$fh->autoflush; $fh->print('foo'); $fh->close;
This is nice because it completely avoids parsing weirdness with theprint $fh $foo
syntax, which technically speaking is indirect object syntax (for example,print $hash->{fh} $text
will not get parsed correctly); and also because the other way of setting autoflush is really awkward (despite this article, STDERR is actually not buffered by default, you'd use this for other handles). It's also reasonable to assume that one may come across this in existing code by now as well.