In this article we will discuss about Variables in PERL.
Before We Begin :
- PERL is abbreviation of Practical Extraction and Report Language
- Every PERL script in Linux/Unix must begin with : #!/usr/bin/perl -w
- Perl is Case-Sensitive, i.e. $c is a completely different variable than $C.
- So highly recommend that you use the directive “use strict;” at the top of all your programs. This forces you to declare all your variables and causes Perl to complain when it sees a variable which has not been declared (often a misspelling).
- It also checks variable scopes in different blocks of the long code.
- # Starts a Comments in PERL
- Every line of code ends with ; (semi-colon)
Where PERL is used in VLSI ?
- Automation to Run Batch Jobs in local or remote machines (LSF).
- QC/QA of Text Outputs of VLSI Tools.
- Data Intensive Comparison between two Different large Output Data files.
- Regression Automation of Testcases.
- Regression Quality Analysis.
- VLSI CAD Infrastructure Building and Maintenance
- Modular/Object Oriented Programming to protect sensitive data.
- And Many more .......
PERL Installations :
You will get the Installation Details of PERL in any of the OS as in
URL : https://www.perl.org/get.html
"Hello World" In PERL :
- Save The Above Code as “hello.pl”
- Give Executable Permission : chmod +x hello.pl
- Execute As : ./hello.pl
- Output is : "Hello World"
- Inline Perl coding : where you do not have write permission or need a very short Perl script :
- perl -e 'print "\n Hello, world \n";'
Defining A Variable(Scalar) in PERL :
- To declare a variable, use the my operator: my $var = 32;
- To declare multiple variables at once : my ($var1, $var2, $var3);
- Perl has a special “undefined” value, often written undef.
- Declared but not assigned variables have the value undef.
- Explicitly a variable can be also defined : $var = undef;
- There is no boolean type as such in Perl. Instead, Perl has a notion of the truth or falsity of any scalar :
- '0' Zero the number itself is false.
- The empty string ’ ’ and the string ’0’ are false.
- undef is false.
- Anything else is true.
- Floating point values include the special values "Inf" and "NaN", for infinity and not-a-number. The infinity can be also negative.
Type Declaration in PERL :
PERL do not have explicit separate data-type declaration such as Integer, Real, Double/Float.
However a scalar can accommodate all the below types as:
- $int = 42; # is an integer
- $pi = 3.14159265; # is a "real" number
- $data = 6.02e23; # exponential notation
- $animal = "Camel"; # is a string
- $sign = "I love my $pet"; # string with interpolation ($pet replaced with "Camel")
- $cost = 'It costs $100'; # string without interpolation
- $num1 = $var2; # another variable value is assigned to $num
- $ab = $a * $b; # an mathematical operation
- $out = `pwd`; # output from a command stored in $out
- $exit = system("vi $x"); # numeric return status of a command
- $apple = new UserDefindClassName "Fruits"; # an object from OOPs in PERL
My / Local / Our Variables :
A my() declares the listed variables to be local (lexical) to the enclosing block, file, or eval. If more than one variable is listed, the list must be placed in parentheses.
A local() modifies the listed variables to be local to the enclosing block, file, or eval. If more than one value is listed, the list must be placed in parentheses.
An our() creates a package variable: It declares an alias for a package variable that will be visible across its entire lexical scope, even across package boundaries.
- package Foo;
- our $bar; # declares $Foo::bar for rest of lexical scope
- $bar = 20;
- package Bar;
- our $bar = 30; # declares $Bar::bar for rest of lexical scope
- print $bar; # prints 30
- our $bar; # emits warning but has no other effect
- print $bar; # still prints 30
Special Variables in Perl :
- $_ The default input and pattern-searching space. It is an global variable.
- The following functions use $_ as a default argument:
- abs, alarm, chomp, chop, chr, chroot, cos, defined, eval, evalbytes, exp, fc, glob, hex, int, lc, lcfirst, length, log, lstat, mkdir, oct, ord, pos, print, printf, quotemeta, readlink, readpipe, ref, require, reverse (in scalar context only), rmdir, say, sin, split (for its second argument), sqrt, stat, study, uc, ucfirst, unlink, unpack.
- The pattern matching operations m//, s/// and tr/// (or, y///) in absence of =~ operator.
- The default iterator variable in a foreach loop if no other variable is supplied.
- The implicit iterator variable in the grep() and map() functions.
- The implicit variable of given().
- The default place to put the next value or input record when a <FILE_HANDLE>, readline, readdir or each operation's result is tested by itself as the sole criterion of a while test. Outside a while test, this will not happen.
- @_ Within a subroutine the array @_ contains the parameters passed to that subroutine. Inside a subroutine, @_ is the default array for the array operators pop and shift.
- $a & $b : Special package variables when using sort(), see "sort" in perlfunc. Because of this specialness $a and $b don't need to be declared (using use vars, or our()) even when using the strict 'vars' pragma. Don't lexicalize them with my $a or my $b if you want to be able to use them in the sort() comparison block or function.
- %ENV : The hash %ENV contains your current system environment variables & their values can be manipulted by this hash/associateive array.
- @INC : The array @INC contains the list of places that the do EXPR, require, or use constructs look for their library files. It initially consists of the arguments to any -I command-line switches, followed by the default Perl library, probably /usr/local/lib/perl.
- @ISA : Each package contains a special array called @ISA which contains a list of that class's parent classes, if any. This array is simply a list of scalars, each of which is a string that corresponds to a package name. The array is examined when Perl does method resolution, which is covered in perlobj.
- $$ The process number of the Perl running your present script itself.
- $0 Contains the name of the Perl program being executed.
- $<digits> ($1, $2, ...) : Contains the sub-pattern from the corresponding set of capturing parentheses from the last successful pattern match, not counting patterns matched in nested blocks that have been exited already.
- $& : The string matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval() enclosed by the current BLOCK).
- $ARGV : Contains the name of the current file when reading from <>.
- @ARGV : The array @ARGV contains the command-line arguments intended for the script.
- Refer the Complete & Exhaustive list : https://perldoc.perl.org/perlvar
Special Literals & Bare-Word :
- The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program.
- __SUB__ gives a reference to the current subroutine.
- Bare-words : A word that has no other interpretation will be treated as a quoted string. These are known as "barewords".
- A bare-word that consists entirely of lowercase letters risks conflict with future reserved words, and if you use the "use warnings" or the -w switch, Perl will warn you about any such words.
- If you use the “use strict”; then any bare-word that would NOT be interpreted as compile-time error. Only bare-words are allowed as Hash key ( NOT the value).