in reply to question about variables from new perl user

Looking through LWP::Simple, LWP and lwpcook, I couldn't find documentation on accessor methods but found that some methods were available (through experimentation :-) ). I've documented some of them in the code below.
#!/usr/bin/perl use strict; use warnings; use LWP::Simple; my $req = head("http://perlmonks.com"); # request if ($req) { print "protocol:\n\t", $req->protocol, "\n"; print "headers->etag:\n\t", $req->headers->{etag}, "\n"; # a leading underscore is considered 'private' # and probably should not be accessed print "_rc:\n\t", $req->{_rc}, "\n"; print "request->uri:\n\t", $req->request->uri, "\n"; print "request->headers->{'user-agent'}:\n\t", $req->request->headers->{'user-agent'}, "\n"; }
Output was:
protocol: HTTP/1.1 headers->etag: "e047-525cb6d2fef00" _rc: 200 request->uri: http://perlmonks.com request->headers->{'user-agent'}: LWP::Simple/6.00 libwww-perl/6.02

Replies are listed 'Best First'.
Re^2: question about variables from new perl user
by Apero (Scribe) on Dec 01, 2015 at 01:38 UTC

    I couldn't find documentation on accessor methods

    Right, because a True/False boolean value does not have methods. The fact that LWP::Simple::head() returns an object is an undocumented side-effect as the API is described. Do not rely on this to be an object (and thus do not rely on any data in the object, object methods, accessors, and so on.)

    As I commented on earlier, I ventured a guess this class returned an object out of simplicity or convenience. The LWP::Simple class docs, however, are very clear that you get a "TRUE" (sic) value back from the function call. It's highly unwise to use it as a constructor.

    If you want to know what type an object is (and can't otherwise tell from documentation, which is where you should go to look for that fact,) you can do so with the ref() built-in call, like so:

    require LWP::Simple; my $fake_boolean = LWP::Simple::head( "http://google.com" ); printf "fake_boolean variable is a reference to: %s\n", ref($fake_boolean) || "<not-a-reference>";

    Big fat warning: do not use this function's return as an object! This is merely a demonstration of how to determine an object's type. Please refer to the LWP documentation on how the object-oriented class is designed and what constructors return what types of objects to avoid unpleasant surprises later.

      Thank you everybody,
      choroba, it takes a little bit of time to wrap my mind around the idea that an array with with numerical indices and one with alphanumeric indices are different. None of C/C++ and PHP are like that. I will have to get used to that.
      Apero and Grandfather, thank you for the explanation of what is going on , the right way to do things, and the warnings.
      Cristoforo, thank you for your insight. sometimes you learn a lot from breaking the rules. (and sometimes you learn fire is hot and it can burn ;) )
      Thanks again,
      Ray

        Actually C only has indexed arrays. PHP only has associative arrays. C++ has both with indexed arrays as first class citizens (inherited from C) and associative arrays supplied as part of the standard template library. Perl has both as first class citizens.

        Premature optimization is the root of all job security