Perl-PHP Rosetta Stone

This Perl-PHP rosetta stone will detail how to do various tasks both in Perl and PHP. If one of the entries is missing I either haven't had to do it, or just haven't bothered filling it in.

Please contact me at brad.marshall@member.sage-au.org.au with suggestions or corrections.

Function Perl PHP
Write to a file
$filename = "/path/to/file";
open(FILEH, ">$file") or die "Can't open $filename: $!\n";
print FILEH "Boo!"
close(FILEH);
$filename = "/path/to/file";
if (is_writable($file)) {
    $file = fopen($file,"w");
    $string = "This is a test string";
    if (!fwrite($file, $string)) {
        echo "Error writing file
"; } } fclose($file);
Read in a file
$filename = "/path/to/file";
open(FILEH, "<$file") or die "Can't open $filename: $!\n";
while() {
    print;
}
close(FILEH);
if (is_readable($filename)) {
    $contents = file($filename);
}
fclose($file);

Read from a command
$filename = "/path/to/file";
open(FILEH, "$file |") or die "Can't open $filename: $!\n";
while() {
    print;
}
close(FILEH);
$command = "/path/to/command";
$fp = popen ($command, "r");
while (!feof($fp)){
      $read = fgets($fp, 2096);
      $read = rtrim($read) ;
      if ( strlen($read)>0 ) {
             $results[] = $read;
      }
}
pclose($fp) ;
Write to a command
$filename = "/path/to/file";
open(FILEH, "| $file") or die "Can't open $filename: $!\n";
print FILEH "Boo";
close(FILEH);
exec($command, $results)
Read all files in a directory
opendir(DIR, $dir) or die "cannot opendir $dir: $!";
while (defined($file = readdir(DIR))){
    print "$file\n";
}
closedir(DIR);
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
             # blah
        }
        closedir($dh);
    }
}
Loop thru all elements of an array
foreach $blah (@array) {
    print "$blah\n";
}
foreach ($array as $blah) {
    print "$blah\n";
}
OR
while ($ent = current($array)) {
   echo key($array).'='.$ent.'
' if $debug; next($array); }
OR
for ($i = 0; $i < sizeof($array); $i++) {
    echo $array[$i]
}
Join an array together as a string
$str = join(":", @array);
$str = implode(":", $array);
OR
$str = join(":", $array);
Split a string into an array
@array = split(":", $string);
$array = explode(":", $string);
OR
$array = split(":", $string);
Sort an array
@newary = sort(@array);
Renumber key:
sort($contents);
reset($contents);
Keep keys:
ksort($contents);
reset($contents);
Replace strings One match:
$string =~ s/se(ar)ch/replace-$1/;
Globally:
$string =~ s/se(ar)ch/replace-$1/g;
One match:
$str = preg_replace("/se(ar)ch/","foo-\$1-bar",$string,1);
Globally:
$str = preg_replace("/se(ar)ch/","foo-\$1-bar",$string);
Matching strings
if ($string = /^(.*)@(.*)$/) {
    $first = $1;
    $second = $2;
}
preg_match("/^(.*)@(.*)$/", $string, $match)
First match is $match[1], second is $match[2], etc
Initialising an array
@array = ($val1, $val2, $val3);
$array = array($val1, $val2, $val3);
Assigning elements to array
push(@array, $value);
OR
$array[++$#array] = $value;
$array[] = $value;
OR
array_push($array, "value");
<<Back to Brad's Publications