Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does [] mean here?

Tags:

perl

$self->[UTF8] = $conf->{utf8};

Never seen such code before.

What does [] mean here?

like image 853
asker Avatar asked Dec 09 '22 06:12

asker


2 Answers

In this case, the object $self is implemented as a blessed array reference rather than the far more common method of using a blessed hash reference. The syntax $foo->[42] accesses a single element from an array reference. Presumably, UTF8 is a constant that returns a numeric index into the array.

You see this idiom sometimes when people become convinced (usually incorrectly) that hash lookups on object attributes result in significant overhead and try to prematurely optimize their code.

like image 130
friedo Avatar answered Jan 05 '23 05:01

friedo


The [] implies that $self is a reference to a list/array (assuming the code works). This looks a bit odd, though, as list indexes should be numeric.

like image 20
cHao Avatar answered Jan 05 '23 05:01

cHao