UserTalk and MacPerl
Ok, this page is the result of a post on the MacPerl mailing list. I've been a Frontier user, and proficient enough in UserTalk for a few years now, but as far as MacPerl (and in a bigger sense, Perl) I'm still a bit of a newbie. So this is my attempt at documenting how the same thing is done using both languages. I've actually kept things very 'Perl' and not MacPerl, as that's the direction I'm heading...
Disclaimer: I'm not advocating one language or environment over another, this is for educational purposes only.
Comments are welcome...
And thanks to those who have commented already!
UserTalk |
MacPerl |
Send simple a message
|
msg ("Hello World")
|
print "Hello World";
|
Open and read a file
|
s = file.readWholeFile (path)
|
open (FILE, $path);
local ($/);
$s = <FILE>;
close (FILE);
|
Create a file
|
file.new (path)
file.close (path)
|
open (FILE, ">$path");
close (FILE);
|
Open a file, write to a file, close a file
|
file.open (path)
file.write (path, value)
file.close (path)
|
open (FILE, ">$path");
print FILE, $value;
close (FILE);
|
Delete a file
|
file.delete (path)
|
unlink $path;
|
Read a directory into an array/list
|
files = {};
fileloop (f in path)
files = files + f
|
opendir (PATH, $path);
@files = readdir (PATH);
closedir (PATH);
|
Create a new folder
|
file.newFolder (path)
|
mkdir $path, r;
|
Is a variable defined?
|
if defined (variable)
msg ("variable exists")
|
if (defined ($variable))
{
print '$variable exists';
}
|
String replacement
|
s = string.replace (s, searchfor, replacewith)
|
$s =~ s/$searchfor/$replacewith/;
|
String replacement globally
|
s = string.replaceAll (s, searchfor, replacewith)
|
$s =~ s/$searchfor/$replacewith/g;
|
String replacement globally and case insensitively
|
s = string.replaceAll (string.upper(s), string.upper(searchfor), replacewith)
|
$s =~ s/$searchfor/$replacewith/gi;
|
Make a string lower case
|
s = string.lower (s)
|
$s =~ tr/A-Z/a-z/;
|
Make a string upper case
|
s = string.upper (s)
|
$s =~ tr/a-z/A-Z/;
|
Strip trailing characters
|
s = string.popTrailing (s, char);
|
$s =~ s/[$char]+$//;
|
Strip leading characters
|
s = string.popLeading (s, char);
|
$s =~ s/^[$char]+//;
|
Get the date and time
|
datetime = clock.now()
|
$datetime = localtime;
|
This page is a work in progress, there's more to come...
|