It was Monday, December 08, 2003 when [EMAIL PROTECTED] took the soap box, saying:
: I did the 'in' function for seeing if one element is inside on list like.
: 
: sub in {
:     my $match = shift;
:     foreach (@_) {
:         return 1 if $match eq $_;
:     }
:     return 0;
: }
: 
: so I'm calling the function like 
: 
: if(in($x => (1,2,3))) {
:    ...
: };
: 
: this seems to be nice. After this I sink: why not to write
: 
: if ($x in (1,2,3)) {
:    ...
: }

You would need a source filter for this, and that would be bad.  I'm
not saying my more OO solution below isn't equally evil, mind, but I
find it fun.  This code could be considered deep magic, and I use grep
to make my life easy for example, so I'm not going to explain it.
I'll leave that to others if they like. :-)

First, the SuperScalar package.

  package SuperScalar;
  require Tie::Scalar;
  @SuperScalar::ISA = qw[Tie::StdScalar];
  use overload
    '""' => sub { ${$_[0]}         },
    '+'  => sub { ${$_[0]} + $_[1] };
  sub in {
      my ($self, @vals) = @_;
      if ( grep { ($$self cmp $_) == 0 } @vals ) {
          return 1;
      }
      return 0;
  }
  sub FETCH { $_[0] }

Next, the calling code.

  use Attribute::Handlers autotie => { SS => 'SuperScalar' };
  my $var :SS(2);
  $\ = "\n";
  print "$var";         # expect: 2                                               
  print $var + $var;    # expect: 4                                               
  print $var->in(1..3); # expect: 1                                               
  print $var->in(3..5); # expect: 0                                               

Enjoy!

  Casey West

-- 
Shooting yourself in the foot with DOS 
You finally found the gun, but can't locate the file with the foot for
the life of you. 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to