Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Command line options in perl

 We can specify command line options at the command line or


after #! line in the program.
Perl -w file1.pl
Or
#!/usr/bin/perl –w

-c option
 Just compiles the program with out executing it.
 This is used to check for syntactical errors in the program.
perl –c <program name>

-w option
 Turns on the warnings that Perl will give when it finds any
problem in the code.
 This can be replaced using ‘use warnings’ pragma, which is
more flexible.
perl -w <filename>

-d option
 Puts the code into debugging mode.

-e option
 Executes perl commands at the command prompt.
perl –e ‘print “hello world\n”’

-M option
 Allows us to use the module from the command line.
$perl -e ‘use person; $p1=new person; $p1->read()’
Or
$perl –Mperson::read() –e ‘$p1=new person;$p1->read()’

Defining a format in Perl

 Perl uses a template called ‘format’ to output reports. To use


this feature, we must:
1. Define a format
2. Pass the data that will be displayed in the format.
3. Invoke the format.

1. Define a format
format formatname=
Fieldline
Value-one, value-two, value-three
Fieldline
Value-one, value-two
.
 ‘fieldline’ is the specific way the data should be formatted.
 ‘values’ represent the values that will be entered into the
fieldline.
 The format should be ended with a dot.

 ‘fieldline’ contains text or file holders. File holders hold space


for data that will be placed there later.
 A fileholder has the format:
@<<<<: left justified with a space of 5.
@>>>>: right justified
@||||: centered
@####.##: numeric field holder
@*: multi line field holder

format Employee=
===========================
@<<<<<<<<<< @<<
$name $age
@#####.##
$salary
=============================
.

 Here, ‘name’ is left justified with 11 spaces and after that ‘age’
will be written with in 3 spaces.
 After that ‘salary’ will be written.

2. Invoke the format to write data


 We should use ‘write’ keyword to invoke this format
declaration.
write Employee;
 By default, the format name is usually the name of a open file
handle and ‘write’ statement writes the output to the file.
 To send the output onto the screen, we should select STDOUT
as the filehandle, on which the data will be written as:
select(STDOUT)

 We have to associate EMPLOYEE with STDOUT, by setting the


new format name with STDOUT, using the special variable $~.
$~=“EMPLOYEE”;
 When we write now, the data will be sent to STDOUT.
Select(STDOUT);
Write Employee;

Format characters
 @: represents the start of a field holder.
 <: indicates that the field should be left justified.
 >: the field should be right justified.
 |: the field should be centered.
 #: the field should be numeric.
 . à indicates that a decimal point should be used with numeric
fields.
 ^: also represents the start of a field holder.
 ~: indicates that the line should not be written if it is blank.

#!/usr/bin/perl
format album=
====================================
AlbumName=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>>
Price=$@##.##
$album, $artist,
$price
====================================
.
open(FILE, "<albums.dat");
@lines = <FILE>;
close(FILE);
select(STDOUT);
$~=“album”;
foreach (@lines) {
chop;
($album, $artist, $price) = (split(/ /));
$album = "no data" if !defined($album);
$artist = "" if !defined($artist);
$price = 0 if !defined($price);
write();
}

#!/usr/bin/perl
format format1 =
============================
welcome to Perl formats;
===========================
.
$~="format1";
write;
write;

Ex:

#!/usr/bin/perl
format Student=
sno@>>>>> sname@<<<<<<<<<< fees$@####.##
$no $name $fees
.

$~="Student";
$no=1;
$name="siri";
$fees=9000;
write;
write;

#!/usr/bin/perl
format s1=
========================================================
=========
The winning number is @>>>>> The prize amount is $@####.##
$n $prize
========================================================
=========
.
$n=100;
$prize=1000;
$~=s1;
write;

binmode()
 If we want to open a file and read its contents in binary mode,
we can use the following functions:
1. open(): to open the file and set a file handle.
2. binmode(): to set the file handle to binary mode.
3. read(): to read the data from the file handle.
4. close(): to close the file handle.
syntax:
binmode(file_handle);
read(filehandle, buffer, length);

 ‘buffer’ is the scalar variable, where the inputted bytes will be


stored.
 ‘length’ is the no. of bytes to read.

open(F1,"emp.txt");
binmode(F1);
read(F1,$str,20);
print "$str\n";
close(F1);

Debugging in Perl
perl -d <filename>
Invokes perl debugger.
After the debugger is invoked, we can use a set of commands.
 h: prints help information
 l: lists next 10 lines of source file
 b: sets a break point at the current statement
 b line: sets a break point at the specified line
 d: deleting break point at the current statement
 d line: deletes the break point at the specified line
 c: continues execution until the next statement with a break
point.
 q: quit the debugging environment
 p exp: print the value of the expression ‘exp’

Installing modules in Perl

 Any module in Perl can be installed using CPAN i.e.


Comprehensive Perl Archive Network.
 It has nearly 1,36,629 Perl modules written by several authors.
 To fire up CPAN first, change to the ‘root’ user and give the
command:
perl -MCPAN –e shell
 This will display us the CPAN prompt and now installing a
module is very easy as:
 cpan>install module::name
 Ex:
 install HTML::Template
 Or
 We can install a module via cpan at the command line directly
as:
 perl –MCPAN –e ‘install HTML::Template’

Some built in modules of Perl


 File::Basename: parses a path into directory, file name and
extension.
 File::Compare: compares files or file handles
 File::Copy: copies files or file handles
 Math::Trig: trigonometric functions
 Net::FTP: FTP client class
 UNIVERSAL: base class for all classes

Environment variables in Perl


 They are stored in Perl in a hash named %ENV.
 To print all environment variables and their values,
foreach $i(keys %ENV)
{
print "$i-->$ENV{$i}\n";
}

You might also like