Perl: processing files from the command line with -e. How is the passed file accessed?

时间:2015-07-29 00:03:29

标签: perl command-line file-access

When I invoke Perl like

Computer(10) Hard Disk (20) Monitor (50)

How is foo accessed?

If I use -i, I can edit foo.txt in place. I suppose foo.txt is associated with some file handle and opened for direct access? What would be the handle? If I use -0777, is the file streamed from STDIN (say) and stored in a scalar string or is it opened and read into such a string? Which string?

I understand this may be trivial but I can't seem to find answers.

Thank you very much for any help.

2 个答案:

答案 0 :(得分:1)

First of all, -i doesn't do anything by itself; you have to actually read from the ARGV filehandle (which empty <?php $pdir = "uploads/photography"; if ($opendir = opendir($pdir)) { while (($file = readdir($opendir)) !== FALSE) { if ($file!="." && $file!="..") { echo "<img height='600px' width='600px' src='$pdir/$file'><br /><br /><br />"; } } } ?> does, or the -n or -p switches).

When you do that, and -i is set, it will open the file on the command line (to the ARGV filehandle) and rename it with the extension you specify (or unlink it if you don't specify an extension) and open a new file with the original name using the ARGVOUT filehandle, selecting that as the default filehandle to print to.

Using -0777 doesn't change anything, other than that when you read from the file, you will get the entire contents, not just a line.

答案 1 :(得分:0)

How is foo accessed?

It's in @ARGV, as usual (specifically $first_Of_Month = date("Y-m-d",strtotime('1 '.$the_Date)); ).

$ARGV[0] doesn't really operate in place. It renames the original file out of the way and creates a new file with the original name. The input handle for the pseudo-file formed by the concatenation of all files in -i is called @ARGV; the output handle for ARGV processing is called -i.

ARGVOUT just sets -0777 (also known as the input record separator) to $/, which causes readline to return the whole file as a single "line".

For more information, see

相关问题