Using -mmodule is the equivalent of use module(), which turns off any default imports. If the module has default imports you don’t want imported then you can use -m instead. Notice that like a normal Perl program, all but the last line of code needs to end with a character.Īlthough it is possible to use a -e option to load a module, Perl gives you the -M option to make that easier. $ perl -e 'print "Hello " ' -e 'print "World\n"' You can have as many -e options as you like and they will be run in the order that they appear on the command line. For example, it’s not necessary to write a “Hello World” program in Perl when you can just type this at the command line. The first one, -e, allows you to define Perl code to be executed by the compiler. The next few options I want to look at make it easy to run short Perl programs on the command line. This is also a subject that’s too big for this article, but I recommend you look at “perldoc perldebug” or Richard Foley’s Perl Debugger Pocket Reference. This option puts you into the Perl debugger. A detailed discussion of taint mode would fill an article all by itself so I won’t go into any more details here, but using taint mode is a very good habit to get into – particularly if you are writing programs (like CGI programs) that take unknown input from users.Īctually there’s one other option that belongs in this set and that’s -d. You do this by checking it against a regular expression. In order to use this data in any of these potentially dangerous operations you need to untaint it. The full list of restrictions is given in the perlsec manual page. Tainted data cannot be used in an expression that interacts with the outside world – for example, you can’t use it in a call to system or as the name of a file to open. This option puts Perl into “taint mode.” In this mode, Perl inherently distrusts any data that it receives from outside the program’s source – for example, data passed in on the command line, read from a file, or taken from CGI parameters. In modern versions of Perl (since 5.6.0) the -w option has been replaced by the use warnings pragma, which is more flexible than the command-line option so you shouldn’t use -w in new code. Each of these warnings is a potential bug in your program and should be investigated. This turns on warnings that Perl will then give you if it finds any of a number of problems in your code. It’s far easier to fix problems when you’ve only made a few changes than it is to type in a couple of hundred of lines of code and then try to debug that. This makes sure that the program still compiles. When I’m working on a program I never go more than a few minutes without saving the file and running: $ perl -c This is a great way to ensure that you haven’t introduced any syntax errors while you’ve been editing a program. This option compiles your program without running it. And while they aren’t ever necessary, it’s rare that you’ll find an experienced Perl programmer working without them. There are three options I like to think of as a “safety net,” as they can stop you from making a fool of yourself when you’re doing something particularly clever (or stupid!). In this article we’ll look at some of the most useful of these. Perl has a large number of command-line options that can help to make your programs more concise and open up many new possibilities for one-off command-line scripts using Perl.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |