Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Set::Scalar saves you from hash acrobatics

by kabel (Chaplain)
on Oct 07, 2003 at 06:39 UTC ( #297212=note: print w/replies, xml ) Need Help??


in reply to Set::Scalar saves you from hash acrobatics

somebody to remember this article about overloading at perl.com?

i patched Set::Scalar a little bit some time ago to support '{1 2 3}' and '{1 .. 3}' notions. code is broken, but should suffice for some own experiments ... just put it somewhere at the top of it, and off you go. beware to throw code far, far away afterwards ;-) (where no code has gone before)

use overload; my %_const_handlers = ( q => sub { if ($_[0] =~ m/^\{.*\}$/) { my $str = substr $_[0], 1, length ($_[0]) - 2; my @elems = split /\s+/, $str; if (defined $elems[1] and $elems[1] eq "..") { @elems = $elems[0] .. $elems[2]; } return __PACKAGE__->new (@elems) || $_[1]; } else { return $_[1]; } }, ); sub import { overload::constant %_const_handlers; }

kabel@linux:~> perl -w -MSet::Scalar my $some_ints = '{1 .. 4}'; my $other_ints = '{3 .. 8}'; my $all_ints = $some_ints + $other_ints; print '[', join( ', ', sort $all_ints->members ), "]\n"; print $Set::Scalar::VERSION, " ", $], $/; [1, 2, 3, 4, 5, 6, 7, 8] 1.17 5.008 kabel@linux:~>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://297212]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2016-10-01 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (9 votes). Check out past polls.