kablamo / mvp.kablamo.org

Minimum Viable Perl
32 stars 15 forks source link

File IO #20

Open Grinnz opened 6 years ago

Grinnz commented 6 years ago

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.

Grinnz commented 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.

kablamo commented 6 years ago

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.

Grinnz commented 6 years ago

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 :)