develooper Front page | perl.perl5.porters | Postings from August 2010

Re: mauve::reftype()

Thread Previous | Thread Next
From:
Zsbán Ambrus
Date:
August 30, 2010 11:05
Subject:
Re: mauve::reftype()
Message ID:
AANLkTinJuV9KAR6AWa-MOe+uHny=g2sU8zLXoq9Qwj+v@mail.gmail.com
On Mon, Aug 30, 2010 at 5:28 PM, demerphq <demerphq@gmail.com> wrote:
> On 30 August 2010 17:13, Zsbán Ambrus <ambrus@math.bme.hu> wrote:
>> On Mon, Aug 30, 2010 at 5:04 PM, demerphq <demerphq@gmail.com> wrote:
>>> Essentially a REGEXP is a special case of a blessed string.
>>> basically "$qr" and $$qr are the same thing.
>>
>> I don't think so:
>>
>> $ ./perl -we $'$x = qr/foo/; $y = "$x"; $$z= $y; warn mauve::reftype($z);'
>> SCALAR at -e line 1.
>> $ ./perl -we $'$x = qr/foo/; $y = ${$x}; $$z= $y; warn mauve::reftype($z);'
>> REGEXP at -e line 1.
>> $
>
> I dont understand what you think you are proving here. Well except
> that the second example is pretty bizarre stuff...

I think I'm proving that "$qr" and $$qr are not the same thing,
because that's the only diff between the two lines and they don't
print the same.

> And actually, i really dont think that $$z= ...; in your example does
> what you think it does.

I don't know what it does.  Really.  I don't even know what $y
contains.  All I want to prove is that it's not a plain
stringification of the regex, and definitely not the same as "$x"
unlike what you stated in the quote above.

One thing I know is that the $$z is not a symbolic reference, and nor
is the $$x, because the examples work with strict refs.

>> On Mon, Aug 30, 2010 at 5:01 PM, demerphq <demerphq@gmail.com> wrote:
>>> Yes, but it [a ref to glob] still needs to be dereferenced with a * as I recall.
>>> (havent verified).
>>
>> No.  It can be referenced with $ fine.
>>
>> $ ./perl -we $'$x = \*_y; warn $$x'
>> *main::_y at -e line 1.
>
> But that wont give you access to the glob slots, only the name of the glob:
>
> $ ./perl -le'our @foo=(1..2); my $gref=\*foo; print $$gref{ARRAY}'
> Not a HASH reference at -e line 1.
>
> If you want access to the glob you have to use glob dereferencing:
>
> $ ./perl -le'our @foo=(1..2); my $gref=\*foo; print *$gref{ARRAY}'
> ARRAY(0x85e37a8)

The *FOO{THING} syntax only works with a splat, $$gref{ARRAY} is a
hash deref that will never get you a glob slot.  That *$gref{ARRAY}
works and $$gref{ARRAY} does not is not at all related to whether
*$gref and $$gref are the same; just like how if $href = \%foo then
the correct deref syntax are %$href but $$href{$v}.

As for giving only a name, no, $$gref gives a glob the same as *$gref
does.  To test this:

$ ./perl -Ilib -wle 'use strict "refs"; our @m = "good"; my $r = \*m;
my $g = $$r; print \@m == *$r{ARRAY}; print \@m == \@$g'
1
1

The same works if you instead write my $g = *$m.  If you instead write
my $g = "m"; that would also work but the last statement breaks strict
refs.

(Incidentally, note that the *FOO{THING} notation accepts both a glob
or a globref.  In your code, *$gref{ARRAY} uses a globref, but in my
tests I use a glob.)

Ambrus

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About