[Melbourne-pm] returning hashes and arrays, source filters considered harmful?

Alfie John alfiejohn at gmail.com
Fri Feb 19 07:31:04 PST 2010


Hi folks,

I had to scratch that itch...

I've just uploaded it to CPAN now, so it might take a while to replicate.
For a sneak peak:

  http://h4c.kr/Scalar-Array-0.02.tar.gz

Scalar::Array will turn any arrayref into an iterator by simply using
the arrayref itself. Only reading is currently implemented (for now).

-- 8< --

SYNOPSIS

  use Scalar::Array;

  my $rr_ref = [ 1, 2, 3, 4, 5 ];

  round_robin( $rr_ref );
  print sa_length( $rr_ref ); # prints 5

  print $rr_ref,"\n"; # prints 1
  print $rr_ref,"\n"; # prints 2
  print $rr_ref,"\n"; # prints 3
  print $rr_ref,"\n"; # prints 4
  print $rr_ref,"\n"; # prints 5
  print $rr_ref,"\n"; # prints 1
  print $rr_ref,"\n"; # prints 2
  print $rr_ref,"\n"; # prints 3
  ...

  my $s_ref = [ 1, 2, 3, 4, 5 ];

  shrink( $s_ref );
  print sa_length( $s_ref ); # prints 5

  print $s_ref,"\n"; # prints 1
  print $s_ref,"\n"; # prints 2
  print $s_ref,"\n"; # prints 3
  print $s_ref,"\n"; # prints 4
  print $s_ref,"\n"; # prints 5
  print $s_ref,"\n"; # undef
  print $s_ref,"\n"; # undef
  print $s_ref,"\n"; # undef

-- >8 --

I would be interested in how this benchmarks compared to the previous
threads but it's now 2:30am so I'm going to bed :)

Alfie

On Fri, Feb 19, 2010 at 10:38 PM, Shlomi Fish <shlomif at iglu.org.il> wrote:

> On Friday 19 Feb 2010 10:04:35 Jacinta Richardson wrote:
> > Sam Watkins wrote:
> > > Also, is there any way to make like an alias @foo
> >
> > > for @$foo, so you can treat an array reference as a normal array
> without
> > > writing @$foo all the time?  (and also for hashes)
> >
> > Not in Perl 5.  Yes, in Perl 6.
> >
>
> Actually, it is possible in Perl 5 - using tie:
>
> -------- CODE ------
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> package Tie::ArrayIndirect;
>
> use base 'Tie::Array';
>
> sub TIEARRAY
> {
>    my ($class, $ref) = @_;
>
>    return bless {'ref' => $ref} , $class;
> }
>
> sub FETCH
> {
>    my ($self, $index) = @_;
>    return $self->{'ref'}->[$index];
> }
>
> sub FETCHSIZE
> {
>    my ($self) = @_;
>
>    return scalar(@{$self->{'ref'}});
> }
>
> sub STORE
> {
>    my ($self, $index, $val) = @_;
>
>    return ($self->{'ref'}->[$index] = $val);
> }
>
> sub EXISTS
> {
>    my ($self, $index) = @_;
>
>    return exists($self->{'ref'}->[$index]);
> }
>
> sub DELETE
> {
>    my ($self, $index) = @_;
>
>    return delete($self->{'ref'}->[$index]);
> }
>
> package main;
>
> sub return_ref
> {
>    return [0,1,22,303];
> }
>
> my $ref = return_ref();
>
> my @array;
>
> tie @array, 'Tie::ArrayIndirect', $ref;
>
> print "array[2] = " . $array[2] . "\n";
>
> push @array, 4444.4;
>
> print "ref->[4] = ", $ref->[4], "\n";
>
> ----- END CODE -----
>
> Regards,
>
>        Shlomi Fish
>
> --
> -----------------------------------------------------------------
> Shlomi Fish       http://www.shlomifish.org/
> What Makes Software Apps High Quality -  http://shlom.in/sw-quality
>
> Deletionists delete Wikipedia articles that they consider lame.
> Chuck Norris deletes deletionists whom he considers lame.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20100220/f9631405/attachment-0001.html>


More information about the Melbourne-pm mailing list