Format And Special Variables

 

Format And Special Variables

PERL Format:

  • As we discussed earlier PERL is used for Extracting & reporting the data(most used data :string ).So let see how we can utilize the this script language to write the formatted reports.
  • For the same purpose PERL uses a writing template called a ‘format’ to output reports.
  • To use the format feature of Perl, you have to define a format first and then you can use that format to write formatted data.

How to define a Format:

format FORMATNAME =

fieldline

value_one, value_two,

fieldline

value_one, value_two.

Note:

  • FormatName represents the name of the format.
  • The fieldline is the specific way ,how the data should be formatted
  • The values lines represent the values that will be entered into the field line.
  • Next fieldline can contain any text or fieldholders.
  • The fieldholders hold space for data that will be placed there at a later date.
  • A fieldholder has the format: @<<<<
  • Above fieldholder is left-justified, with a field space of 5.
  • You must count the @ sign and the < signs to know the number of spaces in the field.

 

 

PERL Format :

Symbol Explanation
@>>>> right-justified
@<<< left-justified
@|||| centered
@####.## numeric field holder
@* multiline field holder

 

Using the Format:

Step 1: Invoke format using write keyword.

write EMPLOYEE;

Step 2: Select your output handle using keyword select.

select(STDOUT);

Step3: Link format to output handle using a operator $~.

 

Example:

#!/usr/bin/perl

format EMPLOYEE =

===============================

@<<<<<<<<<<<<<<<<<<<<<< @<<

$name $age

@#####.## $salary

===============================

.

select(STDOUT);

$~ = EMPLOYEE;

@name=(“Jason”,”William”,”Jack”);

@age=(32,22,25);

@salary=(40000,50000,29000); $i = 0;

foreach (@n){

$name = $_;

$age = $a[$i];

$salary = $s[$i++];

write;

}

 

Report Header

  • Apart from defining a template ,if you would like to define a header and assign it to $^ or $FORMAT_TOP_NAME variable:

#!/usr/bin/perl

format EMPLOYEE =

===============================

@<<<<<<<<<<<<<<<<<<<<<< @<< $name $age

@#####.## $salary

===============================

format EMPLOYEE_TOP =

===============================

Name age

===============================

select(STDOUT); $~ = EMPLOYEE;

$^ = EMPLOYEE_TOP;

@name=(“Jason”,”William”,”Jack”);

@age=(32,22,25);

@salary=(40000,50000,29000); $i = 0;

foreach (@n){

$name = $_;

$age = $a[$i];

$salary = $s[$i++];

write;

}

Report Footer:

  • If you have a fixed-size footer, you can get footers by checking variable $- before each write() and print the footer yourself if necessary using another format defined as follows:

format EMPLOYEE_BOTTOM =

End of Page

@< $%

.

 

Conceptual Script To do same task:

Example: Creating table.

$NL = “\n “;

$DC = “=”; #character for top and bottom lines of table

$TW = 55; #Table width

$DL = sprintf( “%55s”, $NL . $DC x $TW); #top and bottom lines

$COLAHEAD = “Element”;

$COLBHEAD = “Atomic Weight”;

printf(“$DL”);

printf(“\n %-15s %-15s”, $COLAHEAD , $COLBHEAD);

printf(“$DL”);

%ElementsAtomicWeights = (Oxygen, 15.9994, Fluorine,18.9984032,

Neon, 20.1797, Sodium, 22.989770, Magnesium, 24.3050, Aluminum, 26.981538,Silicon, 28.0855);

#Assign the elements to an array

@ElementNames = keys(%ElementsAtomicWeights);

#Use an inner foreach loop and a selection statement

#to retrieve the names of elements

foreach $EName (@ElementNames){

printf(“\n %-15s %-15s”,$EName, $ElementsAtomicWeights{$EName});

}

 

PERL special variable:

  • Perl is having some predefined special variable . Those variable use punctuation characters after the usual variable indicator ($, @, or %), such as $_ .
  • The most commonly used special variable is $_, which contains the default input and pattern-searching string.

Example:

#!/usr/bin/perl

foreach (“Jason”,”william”,”Jack”)

{

print $_; print “\n”;

}

Result:

Jason

William

Jack

 

List of special variable are given below:

  • Global Scalar Special Variables.
  • Global Array Special Variables.
  • Global Hash Special Variables.
  • Global Special Filehandles.
  • Global Special Constants.
  • Regular Expression Special Variables.
  • File handle Special Variables.

 

Global Scalar Special Variables:

Variable Description
$_ OR $ARG The default input and pattern-searching space.
$. OR $NR The current input line number of the last filehandle that was read. An
explicit close on the filehandle resets the line number.
$, The output field separator for the print operator.
$RS OR $/ The input record separator; newline by default. If set to the null string, it
treats blank lines as delimiters.
$\ The output record separator for the print operator.
$: The current set of characters after which a string may be broken to fill
continuation fields (starting with ^) in a format. Default is “\n””.

 

Global Array Special Variables:

Variable Description
@ARGV The array containing the command-line arguments intended for
the script.
@INC The array containing the list of places to look for Perl scripts to
be evaluated by the do, require, or use constructs.
@F The array into which the input lines are split when the -a
command-line switch is given.

 

Global Hash Special Variables :

Variable Description
%INC The hash containing entries for the filename of each file that
has been included via do or require.
%ENV The hash containing your current environment.
%SIG The hash used to set signal handlers for various signals.

 

Global Special Filehandles:

Variable Description
ARGV The special filehandle that iterates over command line filenames in
@ARGV. Usually written as the null filehandle in <>.
STDERR The special filehandle for standard error in any package.
STDIN The special filehandle for standard input in any package.
STDOUT The special filehandle for standard output in any package.
_ The special filehandle used to cache the information from the last
stat, lstat, or file test operator.

 

Leave a Reply

Your email address will not be published. Required fields are marked *