PERL - #!/usr/bin/perl Options

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seo_depth
    Junior Member
    • Jun 2004
    • 4

    PERL - #!/usr/bin/perl Options

    I'm writing a simple mailing list script.

    I put the -w switch and everything went ok (after correcting some stuff, of
    course). LATER ON I added the T option (as in -wT) but get a

    "Too late for '-T' option" message.

    What's the deal?

    It doesn't mean 'too late into the writing of the script' , does it? (!)

    Is there a difference between

    #!/usr/bin/perl -wT

    and

    #!/usr/bin/perl-wT

    ??
  • kevin
    Love is life
    • Dec 2015
    • 83

    #2
    I have some options for you which can help you! Only chnage at the #!/usr/bin/perl path, I think if you will use #!/usr/bin/perl -x, you will find the script working fine.

    PERL Options

    A single-character option may be combined with the following option, if any. This is particularly useful when invoking a script using the #! construct which only allows one argument. Example:

    #!/usr/bin/perl -spi.bak # same as -s -p -i.bak
    ...

    Options include:

    Usage: perl -0digits
    This specifies the record separator ($/) as an octal number. If there are no digits, the null character is the separator. Other switches may precede or follow the digits. For example, if you have a version of "find" which can print filenames terminated by the null character, you can say this:
    find . -name '*.bak' -print0 | perl -n0e unlink

    The special value 00 will cause Perl to slurp files in paragraph mode. The value 0777 will cause Perl to slurp files whole since there is no legal character with that value.


    Usage: perl -a
    Turns on autosplit mode when used with a -n or -p. An implicit split command to the @F array is done as the first thing inside the implicit while loop produced by the -n or -p.
    perl -ane 'print pop(@F), "\n";'

    is equivalent to
    while (<>) {
    @F = split(' ');
    print pop(@F), "\n";
    }


    Usage: perl -c
    Causes perl to check the syntax of the script and then exit without executing it.


    Usage: perl -d
    Runs the script under the perl debugger. See the section on debugging.


    Usage: perl -D
    Sets debugging flags. To watch how it executes your script, use -D14. (This only works if debugging is compiled into your perl.) Another nice value is -D1024, which lists your compiled syntax tree. And -D512 displays compiled regular expressions.


    Usage: perl -ecommandline
    This may be used to enter one line of script. Multiple -e commands may be given to build up a multi-line script. If -e is given, perl will not look for a script filename in the argument list.


    Usage: perl -iextension
    This specifies that files processed by the <> construct are to be edited in-place. It does this by renaming the input file, opening the output file by the same name, and selecting that output file as the default for print statements. The extension, if supplied, is added to the name of the old file to make a backup copy. If no extension is supplied, no backup is made. For example
    perl -p -i.bak -e "s/foo/bar/;" ...

    This is the same as using the script:
    #!/usr/bin/perl -pi.bak
    s/foo/bar/;

    which is equivalent to
    #!/usr/bin/perl
    while (<>) {
    if ($ARGV ne $oldargv) {
    rename($ARGV, $ARGV . '.bak');
    open(ARGVOUT, ">$ARGV");
    select(ARGVOUT);
    $oldargv = $ARGV;
    }
    s/foo/bar/;
    }
    continue {
    print; # this prints to original filename
    }
    select(STDOUT);

    except that the -i form doesn't need to compare $ARGV to $oldargv to know when the filename has changed. It does, however, use ARGVOUT for the selected filehandle. Note that STDOUT is restored as the default output filehandle after the loop.
    You can use eof to locate the end of each input file, in case you want to append to each file, or reset line numbering. See example under eof



    Usage: perl -Idirectory
    This switch may be used in conjunction with -P to tell the C preprocessor where to look for include files. By default /usr/include and /usr/lib/perl are searched.


    Usage: perl -loctnum
    Enables automatic line-ending processing. It has two effects: first, it automatically chops the line terminator when used with -n or -p, and second, it ***igns $\ to have the value of octnum so that any print statements will have that line terminator added back on. If octnum is omitted, sets $\ to the current value of $/. For instance, to trim lines to 80 columns:
    perl -lpe 'substr($_, 80) = ""'

    Note that the ***ignment $\ = $/ is done when the switch is processed, so the input record separator can be different than the output record separator if the -l switch is followed by a -0 switch:
    gnufind / -print0 | perl -ln0e \
    'print "found $_" if -p'

    This sets $\ to newline and then sets $/ to the null character.


    Usage: perl -n
    This causes perl to ***ume the following loop around your script, which makes it iterate over filename arguments somewhat like "sed -n" or awk:
    while (<>) {
    ... # your script goes here
    }

    Note that the lines are not printed by default. See -p to have lines printed. Here is an efficient way to delete all files older than a week:
    gfind . -mtime +7 -print | perl -nle 'unlink;'

    This is faster than using the -exec switch of find because you don't have to start a process on every filename found.

    Usage: perl -s
    Enables some rudimentary switch parsing for switches on the command line after the script name but before any filename arguments (or before a --). Any switch found there is removed from @ARGV and sets the corresponding variable in the perl script. The following script prints "true" if and only if the script is invoked with a -xyz switch.
    #!/usr/bin/perl -s
    if ($xyz){
    print "true\n";
    }


    Usage: perl -S
    Makes perl use the PATH environment variable to search for the script (unless the name of the script starts with a slash). Typically this is used to emulate #! startup on machines that don't support #!, in the following manner:
    #!/usr/bin/perl
    eval "exec /usr/bin/perl -S $0 $*"
    if $running_under_some_shell;

    The system ignores the first line and feeds the script to /bin/sh, which proceeds to try to execute the perl script as a shell script. The shell executes the second line as a normal shell command, and thus starts up the perl interpreter. On some systems $0 doesn't always contain the full pathname, so the -S tells perl to search for the script if necessary. After perl locates the script, it parses the lines and ignores them because the variable $running_under_some_shell is never true. A better construct than $* would be ${1+"$@"}, which handles embedded spaces and such in the filenames, but doesn't work if the script is being interpreted by csh.

    Usage: perl -u
    This causes perl to dump core after compiling your script. You can then take this core dump and turn it into an executable file by using the undump program (which is not supplied with our system). This speeds startup at the expense of some disk space (which you can minimize by stripping the executable). (Still, a "hello world" executable comes out to about 200K on my machine.) If you are going to run your executable as a set-id program then you should probably compile it using (your own) taintperl rather than normal perl.

    Usage: perl -U
    This allows perl to do unsafe operations. Currently the only "unsafe" operations are the unlinking of directories while running as superuser, and running setuid programs with fatal taint checks turned into warnings.


    Usage: perl -v
    Prints the version and patchlevel of your perl executable.


    Usage: perl -w
    Prints warnings about identifiers that are mentioned only once, and scalar variables that are used before being set. Also warns about redefined subroutines, and references to undefined filehandles or filehandles opened readonly that you are attempting to write on.

    Usage: perl -x
    Tells perl that the script is embedded in a message. Leading garbage will be discarded until the first line that starts with #! and contains the string "perl". Any meaningful switches on that line will be applied (but only one group of switches, as with normal #! processing). If a directory name is specified, Perl will switch to that directory before running the script. The -x switch only controls the the disposal of leading garbage. The script must be terminated with __END__ if there is trailing garbage to be ignored. Note that the script can process any or all of the trailing garbage via the DATA filehandle if desired.



    Complete List is Here Below:-


    Usage: perl [switches] [--] [programfile] [arguments]
    -0[octal] specify record separator (\0, if no argument)
    -a autosplit mode with -n or -p (splits $_ into @F)
    -C enable native wide character system interfaces
    -c check syntax only (runs BEGIN and CHECK blocks)
    -d[:debugger] run program under debugger
    -D[number/list]set debugging flags (argument is a bit mask or alphabets)
    -e 'command' one line of program (several -e's allowed, omit programfile)
    -F/pattern/ split() pattern for -a switch (//'s are optional)
    -i[extension] edit <> files in place (makes backup if extension supplied)
    -Idirectory specify @INC/#include directory (several -I's allowed)
    -l[octal] enable line ending processing, specifies line terminator
    -[mM][-]module execute `use/no module...' before executing program
    -n ***ume 'while (<>) { ... }' loop around program
    -p ***ume loop like -n but print line also, like sed
    -P run program through C preprocessor before compilation
    -s enable rudimentary parsing for switches after programfile
    -S look for programfile using PATH environment variable
    -T enable tainting checks
    -t enable tainting warnings
    -u dump core after parsing program
    -U allow unsafe operations
    -v print version, subversion (includes VERY IMPORTANT perl info)
    -V[:variable] print configuration summary (or a single Config.pm variable)
    -w enable many useful warnings (RECOMMENDED)
    -W enable all warnings
    -X disable all warnings
    -x[directory] strip off text before #!perl line and perhaps cd to directory

    Comment

    Working...
    😀
    😂
    🥰
    😘
    🤢
    😎
    😞
    😡
    👍
    👎