Professional Documents
Culture Documents
Command Line Options in Perl
Command Line Options in Perl
-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()’
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.
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.
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);
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’