You may run all of these programs from your own account by changing directory
to ~downeyt/public_html/cop3832/perl
Alternatively, you may execute them via the web from this link:
Run
Examples.
#!/usr/local/bin/perl
#!/usr/local/bin/perl -w
#!/usr/local/bin/perl -T
#!/usr/local/bin/perl
use strict;
#!/usr/local/bin/perl -wT
use strict;
use strict;
my $line;
$line = <STDIN>;
chomp ($line);
print "This is some output\n";
print "This ", "is ", "output ","too\n";
else { if (...) ... }
can be combined as
elsif (...) {...}
With elsif | Without elsif |
if ($x == 1) { |
if ($x == 1) {
|
foreach loops are similar to for..in loops in JavaScript but
use ( ) instead of in. Also in Perl, the values of the array are returned,
not the indexes.
JavaScript | Perl | Perl mimicking JavaScript |
for (pos in list) { |
for $val (@list) { |
for $pos (0..@list) { |
JavaScript | Perl |
function fun(a, b, c) { |
sub fun { |
Functions are called the same way in JavaScript and in Perl.
fun (\@list);
sub fun {
my ($ref_list) = @_;
@{$ref_list} = (10,20,30);
${$ref_list}[2] = 40;
my $length = @{$ref_list}
;
sub fun {
...
return ("hello", 1);
}
($string, $number) = fun();
Opening for reading | Opening for writing | Opening for appending |
local *FILE; |
local *FILE; |
local *FILE; |
if (defined *FILE) {
...
}
if ($line =~ m/pattern/) {
($match1, $match2) = $line =~ m/(pattern1)(pattern2)/;
$line =~ s/pattern1/pattern2/;
It is possible to change the / charcter to any other character, in case you need to match the /. Just use any other character in place of the /.
String Operators | Numeric Operators | |
eq ne gt ge lt le . (concatenation) |
== != > >= < <= + (addition) |
equal not equal greater than greater than or equal less than less than or equal |