kikuchiyo has asked for the wisdom of the Perl Monks concerning the following question:

I had the occasion to look at the source of Net::HTTP::Methods (to debug a nasty, intermittent read timeout, but that's a different story), and I saw that it defines itself like this:

my $self = bless Symbol::gensym(), $class;

And then it has to access its attributes like this:

${*$self}{'http_buf'} = "";

What is the point of this? Does this technique have any advantages over the usual blessed hashref?

Replies are listed 'Best First'.
Re: glob objects - why?
by ikegami (Patriarch) on Feb 24, 2023 at 06:23 UTC

    IO::Socket::IP, IO::Socket::INET6 and IO::Socket::INET objects extend real file handles and thus globs.

    Net::HTTP::Methods objects are also IO::Socket::IP or IO::Socket::INET6 or IO::Socket::INET objects, so they must therefore be globs as well.

Re: glob objects - why?
by LanX (Saint) on Feb 24, 2023 at 10:56 UTC
    The short answer is that
    • you can bless any reference in Perl to get objects - not only hashes like in most other languages
    • type-glob refs exist \*glob
    • *$self is the proper way to deref a glob-ref

    I personally find file-handles and type-globs a bit messy and it always takes me some time to re-understand all the quirks again.

    I think that's mainly - like so often - because of the transit from bare-word handles FH to lexical $fh and the need to keep Perl4 code backwards compatible.

    Once you want to work the internals of IO-objects you need that syntax.

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

    update

  • https://perldoc.perl.org/perlmod#Symbol-Tables
  • https://perldoc.perl.org/perlobj#Method-Calls-on-Filehandles
Re: glob objects - why?
by choroba (Cardinal) on Feb 23, 2023 at 23:47 UTC
    Update: See other answers.

    That's how passing hash references was emulated before real hash references were introduced.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      I have a hard time believing that "bless" pre-dates hashrefs. Do you have a source for that?
        I probably misremembered the details of this stackoverflow answer.
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]