Mailing List Archive

Accessing the HASH/ARRAY/SCALAR part of a GLOB
I raised this question sometime back but I did not get much of a response,
and as perl5.002 is looming I thought I would raise it again.

The situation arises when I want to use a reference to a GLOB to
represent a blessed object so that I can use the scalar, array and
hash parts of the GLOB inside the package.

When it comes to accessing these elements I currenly have 2 options

sub some_method {
my $self = shift;
local *me = *$self;

$me{some_element};
}

which can be confusing as bot C<me> and <self> represent the object, C<me>
is used to change to contents and C<self> for calling other methods.

or

sub some_method {
my $self = shift;

print ${%{*$self}}{fred},"\n";
}

which just looks disgusting ;-)

My question is how many people would object to

$ref->{}
$ref->[]
$$ref

automatically de-referenceing to the required part of the GLOB instead of
croaking with a message like

Not a HASH reference at ./tst line 9.

Regards,
Graham.

--
Graham Barr <bodg@tiuk.ti.com>
Texas Instruments Ltd.
Re: Accessing the HASH/ARRAY/SCALAR part of a GLOB [ In reply to ]
OK looking at this a bit closer I was a bit hasty,

Graham Barr <bodg@tiuk.ti.com> writes:
>I raised this question sometime back but I did not get much of a response,
>and as perl5.002 is looming I thought I would raise it again.
>
>The situation arises when I want to use a reference to a GLOB to
>represent a blessed object so that I can use the scalar, array and
>hash parts of the GLOB inside the package.
>
>...
>
> sub some_method {
> my $self = shift;
>
> print ${%{*$self}}{fred},"\n";
> }
>
> which just looks disgusting ;-)

Can be written as

print ${*$self}{fred},"\n";

which is not so bad :-)

>My question is how many people would object to
>
> $ref->{}
> $ref->[]
> $$ref

This would not work for SCALARs as $$ref already does something, eg

$ref = \*Aglob;
print $$ref,"\n"

gives

*main::Aglob

And if it cannot be consisten, ie work for HASH, ARRAY and SCALAR, then
it probably should not be done %~)

Regards,
Graham.

--
Graham Barr <bodg@tiuk.ti.com>
Texas Instruments Ltd.