Home > Archive > PERL Modules > May 2004 > RFC: instance::vars
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
RFC: instance::vars
|
|
| Kevin Michael Vail 2004-05-23, 1:30 am |
| I've put together something that I don't know what to call. My working
name is "instance::vars", in lowercase because it's similar to the "use
vars" pragma. However, instead of defining package variables, it works
on lexicals within a class. It works with "use fields" to allow
something like this:
package Foo;
use strict;
use instance::vars qw($scalar @array %hash);
sub new {
shift->new_with_instance_vars; ## calls fields::new
}
sub method1 {
instance vars => $self;
my ($param1, $param2) = @_;
$scalar = 3;
$array[2] = 'two';
$hash{whatever} = 'bar';
$self->method2($param1);
}
Each declared variable is created with an appropriate initial value:
undef for scalars, [] for arrays, {} for hashes.
Compare this with
$self->{scalar} = 3;
$self->{array}[2] = 'two';
$self->{hash}{whatever} = 'bar';
In other words, it lets you define variables that look and behave like
normal lexicals, but are actually aliases to items within the object. It
uses "fields" internally to create the variables (via a source filter),
so it should work even after pseudohashes go away. You get compile-time
checking of the variables, and there's even a 'strict' option that
requires you to specify the list of instance variables being referenced
in each method:
sub method2 {
instance vars => $self : @array; ## only aliases $self->{array}
my ($param1) = @_;
$array[3] = $param1;
$scalar = 4; ## generates error
}
The "instance vars" thing that appears first in the example methods can
actually be in one of several different forms (and I don't really like
the syntax I'm using, but it's easy to handle via a source filter--it
has to be something that can't appear in Perl otherwise):
"instance vars => $self;" generates a "my __PACKAGE__ $self = shift;"
and then aliases the instance variables via $self (which can actually be
any identifier).
"instance vars <= $self;" doesn't declare $self, assuming it's already
been declared, but still aliases the instance variables through it.
"instance vars;" doesn't declare or use an explicit object reference,
but aliases the instance variables via $_[0].
Each of the above forms can be followed by a colon and an explicit list
of instance variables, and then only those variables are aliased. If
you put the word ':strict' anywhere in the list of variables that were
created, you must use this form.
Comments? I have an alpha version which I can email if anyone wants it.
I can upload it to CPAN if I can decide on an acceptable name. Maybe
"vars::instance" would be better, since there actually *is* a "vars"
namespace, or maybe it shouldn't look like a pragma even though it sort
of acts like one.
--
boss, sometimes i think | kevin michael vail
that our friend mehitabel | kevin@vaildc.net
is a trifle too gay |
-- archy | wotthehell wotthehell
| |
| Alex Francis 2004-05-27, 12:31 pm |
| Kevin Michael Vail <kevin@vaildc.net> wrote in message news:<kevin-3B19B8.00395023052004@news.verizon.net>...
> I've put together something that I don't know what to call. My working
> name is "instance::vars", in lowercase because it's similar to the "use
> vars" pragma. However, instead of defining package variables, it works
> on lexicals within a class. It works with "use fields" to allow
> something like this:
>
> package Foo;
> use strict;
>
> use instance::vars qw($scalar @array %hash);
>
(snipped)
Hi. I see you got no comments, so here's mine for what it's worth :-)
Sounds like fun. I guess
use vars::as_instance_fields qw($scalar @array %hash);
makes sense within the vars:: namespace, but looks less nice than instance::vars.
Please do announce when it's uploaded, it would be good to take a look at it.
Cheers
Alex
| |
| Kevin Michael Vail 2004-05-28, 3:31 am |
| In article <93da5bda.0405270725.1f972098@posting.google.com>,
alexanderjfrancis@yahoo.co.uk (Alex Francis) wrote:
> Kevin Michael Vail <kevin@vaildc.net> wrote in message
> news:<kevin-3B19B8.00395023052004@news.verizon.net>...
> (snipped)
>
> Hi. I see you got no comments, so here's mine for what it's worth :-)
Thanks, I was beginning to feel like my post hadn't gone anywhere!
> Sounds like fun. I guess
>
> use vars::as_instance_fields qw($scalar @array %hash);
>
> makes sense within the vars:: namespace, but looks less nice than
> instance::vars.
What I think I'm currently leaning toward is fields::aliased, since
that's pretty much what it is:
use fields::aliased qw($scalar @array %hash);
and then use fields::aliased::new to create the object and its fields.
> Please do announce when it's uploaded, it would be good to take a look at it.
I certainly will!
/Kevin
--
Kevin Michael Vail | a billion stars go spinning through the night,
kevin@vaildc.net | blazing high above your head.
. . . . . . . . . | But _in_ you is the presence that
. . . . . . . . | will be, when all the stars are dead.
. . . . . . . . . | (Rainer Maria Rilke)
|
|
|
|
|