How the shell passes variables to Perl scripts

Source: Internet
Author: User
Tags perl interpreter

Method One: In the shell using export to output a variable, and in Perl using the special variable%env to get shell ' s variables.
i.e.:
---shell box---
$/bin/ksh
# Export X=foo
# perl-e ' Print $ENV {' x '} '
-------------
Method Two: Perl, like C, also has array @argv that store command-line arguments, which can be used to handle individual command-line arguments; unlike C, $ARGV [0] is the first argument, not the program name itself.
$var = $ARGV [0]; # first parameter
$numargs = @ARGV; # Number of parameters
The,<> operator in Perl is actually an implied reference to an array of @argv, which works as follows:
1. When the Perl interpreter first sees <>, open the file with $argv[0] as filename;
2, carry out action shift (@ARGV); That is, the elements that @argv the array are moved forward one, and the number of elements is reduced by one.
3. The <> operator reads all the rows in the file opened in the first step.
4, after reading, the interpreter returned to the first step repeat.
Cases:
@ARGV = ("Myfile1", "myfile2"); #Actually assigned by command line arguments
while ($line = <>) {
Print ($line);
}
The contents of the file Myfile1 and Myfile2 will be printed out.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.