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

Hi, I'm trying to understand the following script, i got most of them, but would like to know what "->" refers to. Could someone please explain that part? I'm specifically interested in following lines:
## @entries = $mesg->entries; print "dn: " . $entry->dn() . "\n"; @attrs = $entry->attributes(); $entry->get_value($attr)); ##
# Script
#!/usr/bin/perl use Net::LDAP; $ldap = Net::LDAP->new("localhost"); $ldap->bind("cn=admin,dc=leapster,dc=org", password=>"secret"); $mesg = $ldap->search(filter=>"(objectClass=*)", base=>"dc=leapster,dc +=org"); @entries = $mesg->entries; foreach $entry (@entries) { print "dn: " . $entry->dn() . "\n"; @attrs = $entry->attributes(); foreach $attr (@attrs) { printf("\t%s: %s\n", $attr, $entry->get_value($attr)); } }
Something with a generic explanation is also acceptable. I'm a newbie to perl, however at least i should how the data is getting manipulated, so that i can make use of it for future requirement. Thanks, Pamela

Replies are listed 'Best First'.
Re: What does arrow (->) refer to?
by kennethk (Abbot) on Dec 01, 2009 at 17:28 UTC
    -> is the deference operator (see The Arrow Operator). It is used to resolve a value from a reference (a reference is like a pointer, see perlreftut). The lines like @entries = $mesg->entries; are using a blessed reference in an object oriented fashion to call an associated method. You can learn a bit about object oriented programming in Perl in perlboot. If any of this is unlcear, I'd be happy to answer any follow-ups.
      Thanks for explaining me in the logical way. If possible, could you please explain it with an example. Just a brief one with any sample value.
        There's a lot going on with even a simple example, so I'd really recommend reading perlboot and possibly perltoot, but the following is a basic object implementation:

        File: Foo.pm

        package Foo; use strict; use warnings; sub new { return bless {}; } sub method { return "The value returned by method"; } return 1;

        and the script

        #!/usr/bin/perl use strict; use warnings; use Foo; my $object = Foo->new(); print $object->method();
Re: What does arrow (->) refer to?
by biohisham (Priest) on Dec 01, 2009 at 17:59 UTC
    First, reading the links provided to you is going to prove a great deal of benefit in learning new techniques and illustrate the mechanisms and concepts involved..The '->' is known as the infix dereferencer..it comes to play mainly in references, advanced data structures and in object oriented programming when you invoke constructors or call methods...etc. Consider reading this node too for it was very informative for me when I was confused...

    Now let's see a demonstration example of an anonymous hash held in the scalar $hash:

    $hash={ 'name'=>'Anonymous', 'age' =>'mortal' }; print $hash->{'name'},"\n"; print $$hash{'age'} ,"\n";
    Best of luck... :)


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: What does arrow (->) refer to?
by AnomalousMonk (Archbishop) on Dec 02, 2009 at 02:00 UTC

    Specifically to the lines you have asked about:

        @entries = $mesg->entries;
    Invoke the  entries method on the  $mesg object. The method is a member function of the class of which the object is an instance, or is a member function of a class from which the object's class inherits. The method is invoked without arguments. The method is invoked in list context (determined by assignment to the  @entries array) and a list of zero or more elements returned by the method call is assigned to the array.

        print "dn: " . $entry->dn() . "\n";
    Invoke the  dn method on the  $entry object with no arguments. Method is invoked in scalar context as determined by the  . (string concatenation) operator, so a scalar returned by the method call is converted to a string, concatenated with other strings and printed.

        @attrs = $entry->attributes();
    Invoke the  attributes method on the  $entry object with no arguments. Method is invoked in list context; list returned by the method call is assigned to  @attrs array.

        $entry->get_value($attr);
    Invoke the  get_value method on the  $entry object with  $attr as an argument. Method is invoked in list context (as determined by its invocation in the argument list of a  printf function call in a statement in other code). List returned by method call becomes a part of the  printf function call argument list.

    Update: Fixed  <p> tags per GrandFather. Also: minor wording changes.