Perl already handles automatically opening files listed as arguments via ARGV. Much perl code seems to be written by people who do not know that perl does a lot of this work for free. Show 1 more comment. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.
Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions. Checking the arguments Maybe you could check if the number of elements does not exceed the number you expected. It would stop the user from making the above mistake, but what if the user wants to fetch the phone number of John Doe and forgets the quotes: perl a.
That would reduce the possibility for mistakes in this case. That still would not be perfect and certainly not an universal solution: In other applications there might be several parameters with the same constraints. Unfortunately there is not a lot we can do when parsing ARGV "manually".
In another article I'll write about Getopt::Long and similar libraries that make life a bit easier, but let's see another simple case now. In such cases shift defaults to work on ARGV. At least when the code is not in a subroutine. So effectively, this piece of code would check if a value was provided on the command line. If there was no value, the script would die. Minor bug There is one minor bug in the above code.
If the user supplies 0 as the name of the file. It will still be seen as False and the script will refuse to handle such a file. The question though: Does it matter? Can we live with the fact that our script might not handle a file called Complex cases There are a lot of other cases that are much more complex than the above one or two parameter cases.
Useless use of hash element in void context Useless use of private variable in void context readline on closed filehandle in Perl Possible precedence issue with control flow operator Scalar value Formatted printing in Perl using printf and sprintf. Don't Open Files in the old way open. Prev Next. Written by Gabor Szabo. If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.
Comment on this post. Gabor can help refactor your old Perl code-base. He runs the Perl Weekly newsletter. You could do this through opening an ordinary filehandle into each of those files, gradually building up an in-memory array of all the file contents you load this way, and finally sorting and filtering that array when you've run out of files to load.
Or , you could offload all that merging and sorting into your operating system's own sort command by opening a pipe directly into its output, and get to work that much faster. The second argument to open , "- " , makes it a read-pipe into a separate program, rather than an ordinary filehandle into a file. Note that the third argument to open is a string containing the program name sort plus all its arguments: in this case, -u to specify unqiue sort, and then a fileglob specifying the files to sort.
Continuing the previous example, let's say that your program has completed its processing, and the results sit in an array called processed.
You want to print these lines to a file called numbered. Certainly you could write your own code to do this — or, once again, you could kick that work over to another program. In this case, cat , running with its own -n option to activate line numbering, should do the trick:.
We can then use it just as we would a write-only ordinary filehandle, including the basic function of print -ing data to it. This can start to look a little tricky, because that same symbol would have meant something entirely different had it showed it in the second argument to open! But here in the third argument, it's simply part of the shell command that Perl will open the pipe into, and Perl itself doesn't invest any special meaning to it.
For opening pipes, Perl offers the option to call open with a list comprising the desired command and all its own arguments as separate elements, rather than combining them into a single string as in the examples above.
For instance, we could have phrased the open call in the first example like this:. When you call open this way, Perl invokes the given command directly, bypassing the shell. As such, the shell won't try to interpret any special characters within the command's argument list, which might overwise have unwanted effects. This can make for safer, less error-prone open calls, useful in cases such as passing in variables as arguments, or even just referring to filenames with spaces in them. In this case, we have worked around it via Perl's handy glob built-in function, which evaluates its argument into a list of filenames — and we can safely pass that resulting list right into open , as shown above.
0コメント