I was reading some old posts and came across TMTOWTDI... and most of them are wrong again. I saw that I disagreed with it at the time, and it got me thinking - how many ways really are there to do something? Thus, this challenge.

How many ways can we come up with to open a file?

I'm purposefully leaving this somewhat vague because there are many aspects to perl and the problems we try to solve. Bonus points for explaining the pros and cons of your approach. And bonus points for obscurity. Part of the excersise is to broaden our minds and escape the boxes we've grown accustomed to. We'll assume that $rfile is a filename relative to the current working directory or $afile is a filename with an absolute path - which you use is up to you.

To start things off, here is the trivial, fairly obvious case:

open my $fh, $rfile, 'r';

We get the advantages of a lexical filehandle, in an explicit mode, and should be able to handle spaces in the filename, or other wierd characters. We can be pretty assured that it will ignore special characters, such as < or | that may have been passed in. On the other hand, we can't handle those special characters transparently, such as when an advanced user wants to actually have us read from a pipe instead of a file.

Or, a more obscure case:

use File::Basename; use Net::FTP; my $ftp = Net::FTP->new('localhost'); $ftp->login($user, $password); $ftp->cwd(dirname($apath)); $ftp->binary(); my $dataconn = $ftp->retr(basename($apath));

The $dataconn object can be used like this:

my $data; while ($dataconn->read(my $buffer, 1024)) { $data .= $buffer; } print $data;

Advantage: none, I'm going for the obscure bonus points here. ;-) Well, that's not entirely true - this was actually quite enlightening to write and test on how to use Net::FTP to grab a file that I do not actually want to save to disk under its current name or even under any name. Disadvantage: really obscure for local files. A lot more code than is needed, plus the overhead of an FTP server that needs to be running.

I haven't covered the vast majority of trivial cases, but will let others do so. Also, if you have comments about pros/cons of any example, especially mine, please add that.


In reply to TIMTOWTDI Challenge: Open a file by Tanktalus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.