From robin.berjon at expway.fr Wed Apr 20 05:01:12 2005 From: robin.berjon at expway.fr (Robin Berjon) Date: Wed Apr 20 05:01:26 2005 Subject: [Dahut-pm] QNames and Loaded Magic Message-ID: <42664488.3060103@expway.fr> DAHUT! While chatting with ubu it turned out that there would be interest in being able to call $obj->foo:bar(...) where foo:bar is a method that corresponds to a QName (for XML, RDF, etc.). Unfortunately, ':' is reserved there and can't be used. But other characters, which are illegal in NCNames, can be used there with a little trickery. In this case I picked '^'. It could be '|' or a host of others. The former seemed less intrusive, the latter has stronger 'or' connotations but has the advantage of being the namespace separator used by CSS. The code uses a bit of AUTOLOAD and overload. Tell me what you think, is that a nice way of hacking qnames into Perl? The code is a bit long but almost all of it is meant to be hidden. # this is the main package # we use an AUTOLOAD directly but it could have been installed by # a module being use'd (which could look to see if there is already # one and give up). # The idea is that one can call $obj->foo^bar() where foo^bar represents # the foo:bar QName (mapping to IRIs is left as an exercize to the reader) # Downsides are that an AUTOLOAD cannot exist, and that the QName must have # a () even if it doesn't have arguments. use strict; use vars qw/$AUTOLOAD/; my $obj = Object->new; $obj->foo^bar('dahut', 'Bender'); sub AUTOLOAD { my @args = @_; my $ln = ($AUTOLOAD =~ /([^:]+)$/)[0]; return if $ln eq 'DESTROY'; print "AUTOLOAD main: $ln\n"; return LocalName->new( name => $ln, args => \@args ); } # any method on foo, being incomplete (the part before the ^) # returns a Prefix object. Nothing special. package Object; use vars qw/$AUTOLOAD/; sub new { return bless []; } sub AUTOLOAD { my $self = shift; my $pfx = ($AUTOLOAD =~ /([^:]+)$/)[0]; return if $pfx eq 'DESTROY'; print "AUTOLOAD: $pfx\n"; return Prefix->new( prefix => $pfx, object => $self ); } # LocalName objects deal with the rhs of the ^. They save the # local name and the arguments so that they can be accessed later. package LocalName; sub new { shift; my %opts = @_; return bless \%opts; } # Prefix is the lhs of the ^, and it knows both which prefix was # used and which object it was called on. # It uses overloading so that the bitwise ^ operator calls # makeMeth. What the latter does is up to you. This one # dumps the info to show what you get, which should be enough to # call a method of your own. package Prefix; use overload '^' => \&makeMeth; use Data::Dumper; sub new { shift; my %opts = @_; return bless \%opts; } sub makeMeth { my $pfx = shift; my $ln = shift; # you figure out what to do here print Dumper({ pfx_context => $pfx, ln_params => $ln }); } -- Robin Berjon Research Scientist Expway, http://expway.com/ From geoff at modperlcookbook.org Wed Apr 20 07:55:50 2005 From: geoff at modperlcookbook.org (Geoffrey Young) Date: Wed Apr 20 07:56:20 2005 Subject: [Dahut-pm] QNames and Loaded Magic In-Reply-To: <42664488.3060103@expway.fr> References: <42664488.3060103@expway.fr> Message-ID: <42666D76.5070606@modperlcookbook.org> Robin Berjon wrote: > DAHUT! > > While chatting with ubu it turned out that there would be interest in > being able to call $obj->foo:bar(...) where foo:bar is a method that > corresponds to a QName (for XML, RDF, etc.). Unfortunately, ':' is > reserved there and can't be used. But other characters, which are > illegal in NCNames, can be used there with a little trickery. you might be interested in something chromatic did lately - allow spaces in subroutine names: http://use.perl.org/~chromatic/journal/22689 --Geoff From mike at nachbaur.com Wed Apr 20 10:12:48 2005 From: mike at nachbaur.com (Michael Nachbaur) Date: Wed Apr 20 10:13:04 2005 Subject: [Dahut-pm] QNames and Loaded Magic In-Reply-To: <42664488.3060103@expway.fr> References: <42664488.3060103@expway.fr> Message-ID: <26efa935e2863b5568aba626ff1a8354@nachbaur.com> I've read the chat log and your code blow. I think this would totally rock for RDF munging work, especially given the darobin/ubu idea of lazily following resource URIs to get nested data. So, very neat. I want! :-) I think the ^ is an acceptable work-around for ":" being reserved. If anyone needs help with this (ubu?) I'd be glad to pitch in. I'm doing a lot of RDF work in both Perl and Mozilla/XPCOM these days, so this is very much on my mind. On Apr 20, 2005, at 5:01 AM, Robin Berjon wrote: > DAHUT! > > While chatting with ubu it turned out that there would be interest in > being able to call $obj->foo:bar(...) where foo:bar is a method that > corresponds to a QName (for XML, RDF, etc.). Unfortunately, ':' is > reserved there and can't be used. But other characters, which are > illegal in NCNames, can be used there with a little trickery. > > In this case I picked '^'. It could be '|' or a host of others. The > former seemed less intrusive, the latter has stronger 'or' > connotations but has the advantage of being the namespace separator > used by CSS. > > The code uses a bit of AUTOLOAD and overload. Tell me what you think, > is that a nice way of hacking qnames into Perl? The code is a bit long > but almost all of it is meant to be hidden. > > > # this is the main package > # we use an AUTOLOAD directly but it could have been installed by > # a module being use'd (which could look to see if there is already > # one and give up). > # The idea is that one can call $obj->foo^bar() where foo^bar > represents > # the foo:bar QName (mapping to IRIs is left as an exercize to the > reader) > # Downsides are that an AUTOLOAD cannot exist, and that the QName must > have > # a () even if it doesn't have arguments. > > use strict; > use vars qw/$AUTOLOAD/; > > my $obj = Object->new; > $obj->foo^bar('dahut', 'Bender'); > > sub AUTOLOAD { > my @args = @_; > my $ln = ($AUTOLOAD =~ /([^:]+)$/)[0]; > return if $ln eq 'DESTROY'; > print "AUTOLOAD main: $ln\n"; > return LocalName->new( name => $ln, args => \@args ); > } > > # any method on foo, being incomplete (the part before the ^) > # returns a Prefix object. Nothing special. > > package Object; > use vars qw/$AUTOLOAD/; > sub new { return bless []; } > sub AUTOLOAD { > my $self = shift; > my $pfx = ($AUTOLOAD =~ /([^:]+)$/)[0]; > return if $pfx eq 'DESTROY'; > print "AUTOLOAD: $pfx\n"; > return Prefix->new( prefix => $pfx, object => $self ); > } > > # LocalName objects deal with the rhs of the ^. They save the > # local name and the arguments so that they can be accessed later. > > package LocalName; > sub new { > shift; > my %opts = @_; > return bless \%opts; > } > > # Prefix is the lhs of the ^, and it knows both which prefix was > # used and which object it was called on. > # It uses overloading so that the bitwise ^ operator calls > # makeMeth. What the latter does is up to you. This one > # dumps the info to show what you get, which should be enough to > # call a method of your own. > > package Prefix; > use overload '^' => \&makeMeth; > use Data::Dumper; > sub new { > shift; > my %opts = @_; > return bless \%opts; > } > > sub makeMeth { > my $pfx = shift; > my $ln = shift; > # you figure out what to do here > print Dumper({ pfx_context => $pfx, ln_params => $ln }); > } > > > > > > -- > Robin Berjon > Research Scientist > Expway, http://expway.com/ > > > _______________________________________________ > Dahut-pm mailing list > Dahut-pm@pm.org > http://mail.pm.org/mailman/listinfo/dahut-pm > -- Michael Nachbaur http://nachbaur.com/pgpkey.asc From khampton at totalcinema.com Wed Apr 20 12:12:03 2005 From: khampton at totalcinema.com (Kip Hampton) Date: Wed Apr 20 12:12:20 2005 Subject: [Dahut-pm] QNames and Loaded Magic In-Reply-To: <26efa935e2863b5568aba626ff1a8354@nachbaur.com> References: <42664488.3060103@expway.fr> <26efa935e2863b5568aba626ff1a8354@nachbaur.com> Message-ID: At 10:12 AM -0700 4/20/05, Michael Nachbaur wrote: >I've read the chat log and your code blow. HEY! Keep your editorial comments about the quality of darobin's code to yourself. Sheesh... (silly Freudian thai poux) > I think this would totally rock for RDF munging work, especially >given the darobin/ubu idea of lazily following resource URIs to get >nested data. Well, I think I'm going to keep the default to automatically follow but I'll add support for callback examination. Seems like a nice feature. Anyone have any requests/thoughts about how you'd like to see that work? > >So, very neat. I want! :-) I think the ^ is an acceptable >work-around for ":" being reserved. Well, to clarify: in the current app I'm working on I have RDF-backed Perl objects and I prefer methods over direct data manipulation. Hence, I'd like $obj->foo:bar($value) rather than $hashref->{foo:bar} = $value . (note: the latter works fine in the current RDF::Helper). Its not my intention at this point to add $obj->foo:bar() (or foo^bar) support to the Helper itself as it would entail a whole lot of added strangeness to the code that doesn't really need to be there for the majority of cases. > >If anyone needs help with this (ubu?) I'd be glad to pitch in. I'm >doing a lot of RDF work in both Perl and Mozilla/XPCOM these days, >so this is very much on my mind. Yup. RDF::Helper will be on its way to sourceforge and CPAN "soon". I want to make sure the foundation is good and that it does everything i need it to do, first. Cheers, -ubu From dgwilson1 at cox.net Wed Apr 20 20:18:23 2005 From: dgwilson1 at cox.net (Douglas Wilson) Date: Wed Apr 20 20:15:42 2005 Subject: [Dahut-pm] QNames and Loaded Magic In-Reply-To: <42664488.3060103@expway.fr> References: <42664488.3060103@expway.fr> Message-ID: <42671B7F.3000106@cox.net> Robin Berjon wrote: > DAHUT! > > While chatting with ubu it turned out that there would be interest in > being able to call $obj->foo:bar(...) where foo:bar is a method that > corresponds to a QName (for XML, RDF, etc.). Unfortunately, ':' is > reserved there and can't be used. But other characters, which are > illegal in NCNames, can be used there with a little trickery. I did this a while ago, after I got a little too excited learning about source filters (opinions concerning source filters notwithstanding): http://use.perl.org/~runrig/journal/9892