0% found this document useful (0 votes)
40 views

Perl Scalar1.pl: Practical Extraction and Report Language

The document provides an introduction to the Perl programming language through examples of Perl scripts. It demonstrates basic Perl syntax like variables, arithmetic, strings, loops, conditional statements, opening and reading/writing files. It also shows how to execute Perl programs directly through the Perl interpreter or by making script files executable.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Perl Scalar1.pl: Practical Extraction and Report Language

The document provides an introduction to the Perl programming language through examples of Perl scripts. It demonstrates basic Perl syntax like variables, arithmetic, strings, loops, conditional statements, opening and reading/writing files. It also shows how to execute Perl programs directly through the Perl interpreter or by making script files executable.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Perl

Practical Extraction and Report Language

scalar1.pl
#!/usr/bin/perl -w $a = 3; $b = 5; $rem1 = $a % $b; $rem2 = $b % $a; $a++; $b--; $n1 $n2 $n3 $n4 $n5 = = = = = $a + $b * 2; ($a + $b) * 2; 12 / $a / 2; 12 / ($a / 2); (2*2)**($b-2)**2; # 3 # 2 # 4 # 4 # # # # # 12 16 1.5 6 256

Graham Kemp, Chalmers University of Technology

Graham Kemp, Chalmers University of Technology

hello.pl
#!/usr/bin/perl print "Hello world\n";

Loops in Perl
$i = 1; while ( $i <= 4 ) { print "$i\n"; $i++; } $i = 1; until ( $i > 4 ) { print "$i\n"; $i++; } for ( $i = 1 ; $i <= 4 ; $i++ ) { print "$i\n"; } foreach $i ( (1,2,3,4) ) { print "$i\n"; }

simple.pl
#!/usr/bin/perl $a = 2; $b = 3; $result = $a + $b; print "Result is: $result\n";

Graham Kemp, Chalmers University of Technology

Graham Kemp, Chalmers University of Technology

countdown.pl
#!/usr/bin/perl # # file: # purpose: #

string1.pl
#!/usr/bin/perl $empty = ""; $a = "Bioinformatics"; $b = "\"Perl Programming\"\n"; $me = "Graham\tChalmers\t6475\n"; print "$a $empty $b"; print $me; print "\n";

countdown.pl a 10 second countdown

$countdown = 10; while ( $countdown != 0 ) { print "$countdown...\n"; sleep 1; --$countdown; } print "BOOM!\n";

Bioinformatics "Perl Programming" Graham Chalmers 6475

Graham Kemp, Chalmers University of Technology

Graham Kemp, Chalmers University of Technology

scalar2.pl
#!/usr/bin/perl $str1 = "Merry"; $str2 = "_Christmas! "; $a = $str1 . "_Christmas!_"; $b = $str1 . $str2; $c = "$str1$str2"; $b .= $b; $d = $c x 2; $e = chop($str1); $f = length($str1); $g = lc($str1); $h = uc($str1); $i = substr($a,0,3); $j = substr($a,-4,2); $k = index($a,"m");

string2.pl
#!/usr/bin/perl # # demonstrate single-quoted strings # $empty = ; $a = Bioinformatics; $b = \"Perl Programming\"\n; $me = Graham\tChalmers\t6475\n; print "$a $empty $b"; print $me; print "\n";

# # # # # # # # # # # #

Merry_Christmas!_ Merry_Christmas!_ Merry_Christmas!_ Merry_Christmas!_Merry_Christmas!_ Merry_Christmas!_Merry_Christmas!_ y 4 merr MERR Mer as 12

Bioinformatics

\"Perl Programming\"\nGraham\tChalmers\t6475\n

Graham Kemp, Chalmers University of Technology

Graham Kemp, Chalmers University of Technology

circle.pl
#!/usr/bin/perl -w $pi = 3.1415925; print "Please type in the radius: "; $radius = <STDIN>; chomp($radius); $area = $pi * $radius * $radius; $circ = 2 * $pi * $radius; print "A circle of radius $radius has area $area\n", "and circumference $circ\n";

copyle.pl
#!/usr/bin/perl -w open(SOURCE, "file_A") || die "cannot open file_A: $!"; open(TARGET, ">file_B") || die "cannot open file_B: $!"; while ( $line = <SOURCE> ) { print TARGET $line; } close(SOURCE); close(TARGET); #!/usr/bin/perl -w open(SOURCE, "file_A") || die "cannot open file_A: $!"; open(TARGET, ">file_B") || die "cannot open file_B: $!"; while ( <SOURCE> ) { print TARGET; } close(SOURCE); close(TARGET);

Please type in the radius: 4 A circle of radius 4 has area 50.26548 and circumference 25.13274
Graham Kemp, Chalmers University of Technology

Graham Kemp, Chalmers University of Technology

Opening les
open(SOURCE1, "file1"); open(SOURCE1, "<file2"); open(RESULT1, ">output1"); open(RESULT2, ">>output2"); # reading # reading # writing (create or overwrite) # writing (create or append)

Command line arguments


#!/usr/bin/perl # # file: # purpose: #

arguments.pl prints the command line arguments

open(RESULT3, "+<inoutfile"); # reading/writing open(SOURCE1, "file1") or die "Unable to open file: $!"; open(SOURCE1, "file1") || die "Unable to open file: $!"; close(SOURCE1);

print "Command line arguments are: @ARGV\n"; print "The first argument is: $ARGV[0]\n";

Variables beginning with an @ symbol are array variables. (Scalar) element at position i within an array @a is accessed by $a[i-1]

Graham Kemp, Chalmers University of Technology

Graham Kemp, Chalmers University of Technology

mycat.pl
#!/usr/bin/perl while ( $_ = <ARGV> ) { print $_; }

Comparison operators
Operation equal not equal less than greater than less than or equal greater than or equal What is true? anything except "" and "0" any number except 0 any non-empty array Numeric == != < > <= >= String eq ne lt gt le ge

#!/usr/bin/perl while ( <ARGV> ) { print; }

#!/usr/bin/perl while ( <> ) { print; }

Graham Kemp, Chalmers University of Technology

Graham Kemp, Chalmers University of Technology

Conditional statements
if ( expression ) { # do if true } if ( expression ) { # do if true } else { # do if flase } if ( expression1 ) { # do if expression1 is true

Executing Perl programs


You can invoke the Perl interpreter directly, e.g.
perl program.pl

Or, if the rst line of the program contains "#!" followed by the path of the Perl interpreter, and the program le is executable, you can just type the name of the program le on the command line, e.g.
./program.pl

To make a program le executable, use the chmod command, e.g.


chmod u+x program.pl

} elsif ( expression2 ) { # do if expression1 is false and expression2 is true } else { # do if expression1 is false and expression2 is false }
Graham Kemp, Chalmers University of Technology

Graham Kemp, Chalmers University of Technology

You might also like