Not getting "dereference"

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi All,

In this example Mart gave me

my $ua = LWP::UserAgent->new( timeout => 20 );
my $response = $ua->get(' ');
my $content = $response->content if $response->is_success;

It has a lot of dereferencing (meaning "->" ) going on.
I don't have a clue what is happening.  What is "dereferencing"

For instance:
     $ua->get(' ')

What is going on?  I presume the "get" download the web page,
but why are we dereferencing it in $ua?

Many thanks,

Re: Not getting "dereference"

On 9/9/2015 00:48, T wrote:
Quoted text here. Click to load it

get is a method in LWP::UserAgent and yes, it gets a page.

In your combined man pages using vim, type /^perlobj to get you to
the general area and skim down a few pages to /Method Invocation
(or use perldoc and less which is not as nice IMO).

     Calling a method on an object is written as "$object->method".

Re: Not getting "dereference"

Quoted text here. Click to load it

Accessing the object referred to by a reference. The case for 'a
dereferencing operation' is frequently somewhat weakly defined as the
computer could interfere that an expression was 'meant' to work with the
indirectly accessible object and not the reference itself but
frequently, this is not the case.

Consider the following example:

my $v = -2;
my $rv = $v;

printf("$rv + 1 == %d, $$rv + 1 == %d\n",
       $rv + 1, $$rv + 1);

References can be converted to numbers which is mainly useful because it
implies two references can be compared with ==[*]. But this implies they
can also be used as numbers in any other context a number could appear
in, as in the

$rv + 1

above. This converts the reference $rv to a number and adds 1 to the
value. This very likely completely useless, however, without
understanding the intention of the programmer, the computer has no way
of knowing this for certain. Hence, it just does as told. In contrast to

$$rv + 1

dereferences the reference $rv which yields $v and adds 1 to the value
of that.

[*] According to the ancient law of the land, this is undefined
behaviour and comparing two Perl reference might thus cause a nearby
volcano to turn into a peaceful water fountain. Or vice versa.

Quoted text here. Click to load it

$ua (for 'user agent') is a Perl object (in the OO sense), that is, a
reference associated with a certain package. Leaving inheritance aside,

$ua->get(' ');

(the URL is real)

instructs perl (the Perl runtime) to look for a subroutine named get
in the symbol table of the package $ua is associated with. If such a
subroutine is found, it's invoked as

LWP::UserAgent::get($ua, ' ');

The interesting property of that is that code using an object doesn't
know anything about the package it's associated with and is thus totally
unaware of the actual subroutine which will be called when a


is executed. As a somewhat less silly example than the common ones, one
could thus create a package implementing a get method which logs the URL
to a file and then passes it one to the LWP::UserAgent::get. Code
written to work with objects providing an interface like this would then
just continue to work as it did before with objects of the new kind
except that all 'web accesses' would be transparently logged to a file.

Re: Not getting "dereference"

Quoted text here. Click to load it
$ua and $response contain references to an object; 'dereferencing' it
like this means calling a method on that object. In this example, we are
calling it on $ua because $ua holds the user agent state (like
configuration items like the timeout).

$response is a better example, because that holds more data, like the
request status, the content, and the headers, and we can call methods on
it to get at it (see how I call two different methods on the same


"We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.

Re: Not getting "dereference"

Quoted text here. Click to load it

This isn't really true: The references are the objects, cf

    1.  An object is simply a reference that happens to know which
    class it belongs to.

        [perldoc perlobj]

It is common to use this reference to refer to some 'instance data'
associated with this object but not required. That's a fundamental
difference between 'Perl OO' and most other languages with OO support:
An object is not a record/ structure with some magic associated with
it. It's (almost) possible to make Perl OO look like this more common
and less versatile model but only by covering it with a venerable
mountain of code in order to hide the native mechanism.

This is very popular with "Rotting Perl!" people[*], IOW, people who
think that CPAN is surely such a large archive of freely downloadable
stuff that they will be able to solve any technical problem someone
could ask them to solve by downloading something from CPAN but "Alas!
All that shit is written in Perl! Something needs to be done about

[*] This is a bilingual word-play based on the fact that modernd means
'rotting' in German and that this describe the current state of 'modern
Perl' more accurately than modern ever did.

Re: Not getting "dereference"

On 09/09/2015 12:48 AM, T wrote:
Quoted text here. Click to load it

I am still reading over everyone's responses.  Four times now
trying to get it to stick.  Just when I think I understand ...

I just wanted everyone to know I do appreciate their help and
I am not ignoring them.

Is there specific reason why the author use dereferencing?
Did it help something?  Was he trying to drive
Todd mad?  I don't think he even knows me!


It has been said, that the only reason any of us have
any friends is that they don't truly know us!  :-)

Re: Not getting "dereference"

Quoted text here. Click to load it

The -> is used for method calls. Dereferencing is the act of accessing
whatever it is that a reference points to.

Quoted text here. Click to load it

It is a method call, not a dereference. The notation is similar. get()
is a method defined for the class LWP::UserAgent. $ua->get(...) calls
that method for the object (reference) assigned to $ua. While
executing, the get() method has access to the object referred to (or
stored in) $ua.

Quoted text here. Click to load it

The author of the code is not using dereferencing. He/she is using
object method calls. While there may be dereferencing involved (I don't
know and don't care), the construct $var->sub() is a method call.

If $var is a blessed reference, then it belongs to a Perl class. A
special mechanism is used for method calls (as opposed to normal
subroutine calls). The object reference is added as the first parameter
in the parameter list, thereby giving the method access to the object
itself. Thus, you will see statements like this at the beginning of the
method implementation:

  my $self = shift(@_);

See 'perldoc perlootut' if you want to learn object-oriented Perl
programming. For now, you should just learn enough so that you can
utilize classes written by others, such as LWP::UserAgent. Generally,
the usage is:

1. Create an object of the correct class, usually with a new() class
method (named that by convention) and saved in a variable.

2. Call methods on that object with the variable->method() syntax.

Jim Gibson

Site Timeline