[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Emulating AS-like properties in Perl



On Wed, 24 May 2000 18:23:15 +0200, Peter Hartmann wrote:

>I'd like to have something like properties from AppleScript in 
>(Mac)Perl, i.e. variable values that are saved across executions of a 
>script, preferably without having to use a preferences file.
>How should I go about it?

You must be a bit confused. AppleScript *is*¨using a preference file.
Only, the preference file is the script itself.

The official term for this is "persistence".

There are several ways of achieving this. Virtually all of them make use
of an external file. For example, you could use a "database" through a
tied hash. You could also make use of modules like Data::Dumper or
Storable to turn Perl's data structures into plain text perl code, and
save it into a "configuration" script, and which you can "do". Or you
could provide a simple way to store a hash into a text file.

An easy example, saving a simple hash into a tab delimited text file
(warning: untested code):

Write:
    {
	local($\, $,) = ("\n", "\t");
	my %encode = ( "\n" => 'n', "\t" => 't', "\\" => "\\");
	foreach my $key (keys %hash) {
	    my $value = $hash{$key};
	    $value =~ s/([\n\t\\])/\\$encode{$1}/g;
	    print FILE $key, $value;
	}
    }

Read:
    {
	my %decode = ( 'n' => "\n", 't' => "\t", "\\" => "\\");
	while(<FILE>) {
	    chomp;
	    my($key, $value) = split /\t/;
	    $value =~ s/\\([nt\\])/$decode{$1}/g;
	    $hash{$key} = $value;
	}
    }

-- 
	Bart.

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org