in reply to Re: How to detect a hash container key from a referenced sub value
in thread How to detect a hash container key from a referenced sub value

So, I get it now, the verdict is that I have to create a module (package) to describe an object. I guess, I should read about objects. I've made attempts to learn it before, but there were few things that stopped me. One that I described in reply to FalseVinylShrub, the inability to use the same script file for describing an object and also, I couldn't understand the purpose of the bless function.
  • Comment on Re^2: How to detect a hash container key from a referenced sub value

Replies are listed 'Best First'.
Re^3: How to detect a hash container key from a referenced sub value
by 7stud (Deacon) on Dec 03, 2009 at 17:38 UTC

    I couldn't understand the purpose of the bless function

    Normally, if you have a hash reference:

    my $hash = { a => 1, b => 2 };

    you can write:

    say $hash->{a};

    but you cannot use a hash reference to call a function, e.g.

    $hash->some_func()

    If you look at the code that creates a class, the new() function creates a variable called $self and assigns it a reference to a hash. bless() makes it so that you can use the hash reference to call functions.

    An object is different from any other data type in Perl in one and only one way: you may dereference it using not merely string or numeric subscripts with simple arrays and hashes...

    $aref->[0]
    $href->{'a'}

    ...but with named subroutine calls...

    $href->some_func()

    In a word, with methods.

    perltoot