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

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



[Resent, as the <macperl.org> domain dissappeared for a few days,
due to DNS problems, and the original from 29 May 2000 18:11 doesn't
seem to have made it to the list]

On Fri, 26 May 2000 17:54:21 +0200, Peter Hartmann wrote:

>What I liked best so far, but obviously did not get thru to the list is:
>
>At 17:47 Uhr +0100 23.05.2000, Lindsay Davies wrote:
>>[snip]
>>Take a look into tied hashes with DB_File. quick snippet...

Actually, I had mentioned that:

:> For example, you could use a "database" through a tied hash.

Now, not only would you indeed have a preferences file, but it would be
a huge one.

>Bart Lateur wrote:
>
>>You must be a bit confused.
>
>I am sorry, but no, I am not. It seems, though, that it is you who is 
>a bit confused :)
>
>>AppleScript *is*®using a preference file.
>>Only, the preference file is the script itself.
>
>An AppleScript per definition is no preferences file.
>And of course, I know that properties are saved within the script, as 
>is stated in my original post.
>What I try to avoid is, exactly, the use of a preferences file in 
>Perl, if possible.

Well... all pedantry aside, it looks to me like you want a preferences
file, only not a *separate* preferences file. You'd like to use the
droplet (or runtime?) as a preferences file for itself.

Using the part following a __DATA__ line isn't the best of ideas,
because the script itself is stored in a resource in the resource fork.
So, once you go that way, you'd better store the data in a separate
('TEXT'?) resource, and "eval" it.

If I understand correctly from your enthousiasm for the tied hash
solution, what you really really want is to have a tied hash that
automatically loads and stores it's data in a resource. Hmmm... not a
simple project, I would think. But an interesting one. I'll put it in my
agenda.

In the meantime, here is a very simple solution that works *for the
moment*, but it is not garanteed to keep working in the future (i.e.
with newer versions of Perl droplets). Currently, the data fork of
droplets is unused. So you can simply use the file itself as a data
file! Here is a demo script that actually works. Save as droplet, not as
plain text!

The only problem I see is that if the droplet is resaved, poof! goes
your data fork. All preferences will be lost.

  #! perl -w
  use Data::Dumper;
  unless(-s $0) {
      print "First time!\n";
      %data = ( pi => 3.14159, alphabet => [ 'a' .. 'z' ],
        counter => 0 );
  } else {
      do $0; 
      $data{counter}++;
      foreach (sort keys %data) {
         my $value = ref $data{$_}?"\[qw(@{$data{$_}})\]":$data{$_};
         print "$_: $value\n";
      }
  }

  open ME, ">$0";
  print ME Data::Dumper->Dump([\%data], ['*data']);
  close ME;
__END__

-- 
	Bart.

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