develooper Front page | perl.perl6.language | Postings from April 2003

globs?

Thread Next
From:
Paul
Date:
April 8, 2003 13:56
Subject:
globs?
Message ID:
20030408205603.51871.qmail@web41209.mail.yahoo.com

Ok, here's what's been itching my brain.

P5:
===
# bad code used for example:
# version 1
   # $obj->foo(\@attrs,@vals);
   sub foo { 
       my $self   = shift;
       local *bar = $self; # object is hashref
       my $lstref = shift;
       @bar{ @{$lstref} } = @_; # <<== here's the rub
   }

# version 2
   # $obj->foo(%newstuff);
   sub foo { 
       my $self   = shift;
       local *bar = $self; # object is hashref
       my %newstf = @_;
       @bar{ keys %newstf } = values %newstf;  # <<== here
   }

This is obviously ugly code, but I've used stuff like it to bulk set
specific properties on an object without overwriting others.
I realize that such hacks probably won't be *needed* much in P6....
But what really confuses me is.... where did globs go?

Have I completely misread what's being rolled around?
Or missed some apocolyptic pronouncement? Or is it gonna be A8?

Ref's are pretty clean, while globs have been a little more like
pointers... use with caveats, only when needed... but I *like* globs
(and pointers, too, for that matter). 

I'm sure that with P6, self-dereferencing may just absorb that sort of
problem, so that a bulk assignment to object properties becomes simpler
and more efficient and more readable and a slew of other better-nesses,
but I just don't have enough P6 under my belt to envision how I'd
reimplement some of these things.

I know the above could have been done in a loop:

   # $obj->foo(%newstuff);
   sub foo { 
       my $self   = shift;
       my %newstf = @_;
       $self->{$_} = $newstf{$_} for keys %newstf;
   }

I even admit that it might be more readable/maintainable. I suspect it
might be slower, but don't pretend to understand such optimizations
well enough to really know without benchmarking it, which I haven't
done recently. I'm just hoping to learn by some examples.

Generally, this is my take on it....

P6:
===
# version 1
   # $obj->foo(@attrs,@vals);
   my method foo (%me: @a, @b ) { 
       %me{ @a } = @b;
   }

I suspect I've screwed up several things in even so short an example,
but if that's anywhere close, it at least explains why globs went away.
:)

# version 2
   # $obj->foo(%newstuff);
   my method foo (%me: %n ) { 
       %me{ %n.keys } = %n.values;
   }

To me, this is where we want to go. That's still Perl, but @#$%^&!!!
Look at the improvement! No requirement for all the sloppy
localization, it's READABLE, and *very* straightforward. Admittedly,
the old code was *bad* code, but this is so much easier and cleaner
that it would take real work to write code even half as bad. (Of
course, I'm pretty talented there.... ;)

So, as I suspected, "talking it out" is making it make more sense, and
I suspect that people will come back with comments and corrections that
will clarify a lot for me, too. So please, feel free to poke -- that's
why I'm posting. >:O)

Paul

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About