Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Schwartzian transform of %$self ... help?

28 views
Skip to first unread message

Colin Howarth

unread,
Jul 2, 1996, 3:00:00 AM7/2/96
to

Hi all.

Most of my methods start off with something like:

$self = shift;

my $font = $self->{Font};
my $text = $self->{Text};
...

which wastes toner if you have a lot of members.

What I'd like to do is something like

my( map { ${lc $_} } (keys %$self) ) = (values %$self);

but this complains (how dare it!?) thusly:

Can't use global $_ in "my" at PS.pm line 831.

hmmph. Ok, there IS a $_ inside the my(), but I'm not trying to my the $_ am
I? The following irrelevant snippet is ok though:

$text = "Boo :-)"; $font = "Helvetica"; $size = 20;
print ( map { ${lc $_} } (Text, Font, Size) );

Any ideas?

Thanks,

colin

Matt DiMeo

unread,
Jul 2, 1996, 3:00:00 AM7/2/96
to

In article <4rb8tm$1...@colin.muc.de>, Colin Howarth <co...@muc.de> wrote:
> my $font = $self->{Font};
> my $text = $self->{Text};

I'd construct a string containing all those commands and eval() it.

If you don't want to do this at run time for every method call, you can
construct entire functions as strings, and eval() *them* to define the
functions.

Another (IMHO better but slower [function call overhead]) possibility
would be to create a series of functions font(), text(), etc.,
and use them as methods instead of using $font, etc.

With the short module at the end of this posting, you can say

use Bt::Access_func "Mypackage" qw(Font Text) ;

And get $self->Font and $self->Text as methods in object Mypackage,
basically for free. This module uses the create-functions-as-
strings-and-eval() methodology. With minimal changes, you can make
it generate lowercased method names, if you prefer.

Enjoy,
-m@

# use Bt::Access_func "packagename", qw(field1 field2 field3 ...)
# Generates access routines for the $self->{'field'} entries for another
# object "packagename".

package Bt::Access_func ;

use strict ;
sub import {
my ($mypackage, $targpackage, @kw) = @_ ;
my ($evalstr) = "" ;
my ($word) ;

foreach $word (@kw) {
$evalstr .=<<EOT ;
sub $targpackage\::$word { \$_[0]->{'$word'} }
EOT

}

eval $evalstr ;
}

1;

Randal L. Schwartz

unread,
Jul 2, 1996, 3:00:00 AM7/2/96
to Colin Howarth

>>>>> "Colin" == Colin Howarth <co...@muc.de> writes:

Colin> Hi all.
Colin> Most of my methods start off with something like:

Colin> $self = shift;

Colin> my $font = $self->{Font};
Colin> my $text = $self->{Text};
Colin> ...

Colin> which wastes toner if you have a lot of members.

I'd just do this:

{
package TestDummy;
use Alias; # found in the CPAN
sub new {
bless {
Font => "font".time,
Text => "text".time,
}, shift;
}

sub versive {
my $self = attr shift;
print "for $self, Font is $Font and Text is $Text\n";
}
}
$thing = new TestDummy;
versive $thing;

The CPAN is your friend. Use the CPAN.

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $18,720.69 collected, $172,159.85 spent; just 790 more days
## before I go to *prison* for 90 days; email fu...@stonehenge.com for details
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <mer...@stonehenge.com> Snail: (Call) PGP-Key: (finger mer...@ora.com)
Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

0 new messages