in reply to Re: Re: Re: Re: Code critique XS function for extracting a blessed regex's pattern.
in thread Code critique XS function for extracting a blessed regex's pattern.
Them's pretty strong words you are using there dude.
Strong yes, but I didn't choose them lightly either. Here's an annotated version:
Because it uses an undocumented "feature"(the fact that the thingies returned by qr// maintain their magic after being reblessed into another class is undocumented) of an undocumented quasi-type (the thingies themselves are references blessed into the non-existent Regexp package) to provide functionality of questionable necessity (even if someone chooses to rebless the refs returned by qr//, the need to differentiate them from other types of references should not be common and should be altogether avoidable) to people writing ill-conceived code (using the refs returned by qr// directly is ill-conceived; the benefits are minor and the drawback, code that will break if the underlying implementation of those Regexp thingies changes, is relatively great.)The implementation, IMHO, should change. The more people rely on the current implementation the harder that will be.
If you really think this implementation is the way things should remain, go ahead and submit a doc patch. I suspect it has remained undocumented for almost 5 years(! since 5.005) not because no one had the time or inclination but because no one wants to lock us into the current way of doing things.
Personally I don't think that an improved dumper is ill-conceived
I think you misunderstood. My point was that anyone that can make use of this feature is probably writing ill-conceived code. I can understand adding this functionality to Dumper as a matter of completeness. I could also understand leaving it out of Dumper on the basis that references originating from qr// retaining their regexp magic when reblessed is an undocumented and questionable "feature".
I fail to see why this behaviour is inconsistent. One item is a regex, the other item is not.
What is a "regex?" One item had been a Regexp, but it was reblessed. Both items were blessed into the 'P' package. If we accept what perldoc -f bless tells us then both items were objects in the P package. The P package implemented its own stringification. According to perlop in reference to the binding operator, "if the right argument is an expression rather than a search pattern, substitution, or transliteration, it is interpreted as a search pattern at run time," so a module author should reasonably be able to expect their stringification to act as search pattern at run time when used as one. But that's not how the reference originating from qr// works. So, two references both blessed into the same class can act very differently depending only on their origin. Do you see now how that is inconsistent?
I believe that it is a feature. And one that is exploited too. I think it is extremely unlikely that this behaviour will change, and if it does it will change over several versions as it must be deprecated first, then eliminated.
I don't think of it as a feature. I don't know where it is exploited but I'd be curious to see any examples. I hope the behavior does change; I'd like to see Regexps elevated to a true Perl type (named REGEX per Larry's recommendation.) I don't think that changing it should be a long process requiring anything but a minimal deprecation period as it is essentially an implementation detail whose public exposure has so far remained undocumented.
Precisely the problem I am trying to address. How do I tell a string from a regex?
Ideally, it shouldn't be so difficult to do in the first place, right? You should be able to use code like if ref $r eq 'Regexp' or if UNIVERSAL::isa($r, 'Regexp') and the fact that you can't is a symptom rather than the real problem itself. You've got a work around for the symptom, but the real problem remains.
I dont get it. This is exactly the behaviour I would expect given that it is not documented that you shouldn't write a Regexp package of your own.
It isn't documented that you shouldn't write a Foo package of your own either and yet, if you do, the thingies returned by qr// won't suddenly get access to your Foo methods. As an illustration:
So, you must know about an undocumented feature in order to understand why qr//->bar; actually works in the above code. And that's the behavior you expect? Why on Earth would you expect that?#!/usr/bin/perl -w use strict; package Foo; sub foo { print "foo\n" } package Regexp; sub bar { print "bar\n" } package main; qr//->bar; # Works even though it probably shouldn't. qr//->foo; # Errors as expected.
By the way...
$ perl -MB -le '$r=bless qr//,"P"; print B::svref_2object($r)->MAGIC-> +TYPE' r
-sauoq "My two cents aren't worth a dime.";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Re: Re: Code critique XS function for extracting a blessed regex's pattern.
by demerphq (Chancellor) on Feb 08, 2003 at 14:08 UTC | |
by sauoq (Abbot) on Feb 09, 2003 at 04:51 UTC | |
by ysth (Canon) on Nov 20, 2003 at 00:09 UTC |