Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8033911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:51:06+00:00 2026-06-05T01:51:06+00:00

I have a program in Perl that uses a package that I got from

  • 0

I have a program in Perl that uses a package that I got from another source. One of the functions of the method returns an object of an unknown class, Is there a way for me to get all the possible methods of an object without looking at its class implementation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:51:08+00:00Added an answer on June 5, 2026 at 1:51 am

    Not really.

    TL;DR:

    • You can find the names of subroutines explicitly declared or placed into the object’s class’s namespace.

    • You can NOT distinguish which of these subroutines are object methods on your object, and which are class or non-object subs (this is the most serious problem/limintation among those listed).

    • You can NOT find the methods inherited by an object in the subclass from the superclass using this method, unless they were already called on your object.

      This can be coded around, by either inspecting @ISA of the class to build up inheritance trees, or using on of proper CPAN modules.

    • You can NOT find the methods that are dynamically added to the class (AUTOLOAD, manual method injection in the code somewhere).

    In detail

    1. You can find all of the subroutines in that class (by combining the fact that the class namespace is a hash so all identifiers in it are keys in that hash; and the UNIVERSAL::can call to separate subroutines).

      Therefore, if you are GUARANTEED (by non-technical contract) that 100% of subroutines in the class are object methods, AND that your class is NOT a subclass, you can find their list.

      package MyClass;
      use vars qw($z5);
      my $x = 11; our $y = 12; $z5 = 14; %z2 = (1=>2); # my, our, globals, hash
      sub new { return bless({}, $_[0]) }; # Constructor
      sub x1 { my $self = shift; print $_[0]; };
      sub y2  { my $self = shift; print $_[0]; };
      ##############################################################################
      package MySubClass;
      use vars qw(@ISA);
      @ISA = ("MyClass");
      sub z3 { return "" };
      ##############################################################################
      package main;
      use strict; use warnings;
      
      my $obj = MyClass->new();
      list_object_methods($obj);
      my $obj2 = MySubClass->new();
      list_object_methods($obj2);
      $obj2->x1();
      list_object_methods($obj2); # Add "x1" to the list!
      
      sub list_object_methods {
          my $obj = shift;
          my $class_name = ref($obj);
          no strict;
          my @identifiers = keys %{"${class_name}::"};
          use strict;
          my @subroutines = grep { UNIVERSAL::can($obj, $_) } @identifiers;
          print "Class: ${class_name}\n";
          print "Subroutines: \n=========\n"
              . join("\n", sort @subroutines) . "\n=========\n";
      }
      

      … prints:

      Class: MyClass
      Subroutines:
      =========
      new
      x1
      y2
      =========
      Class: MySubClass
      Subroutines:
      =========
      new
      z3
      =========
      Class: MySubClass
      Subroutines:
      =========
      new
      x1
      z3
      =========
      

      Please note that the first-time list (for MySubClass) printed new and z3 but NOT x1 or y2 – because new was executed and z3 was declared in the class; but x1 and y2 was neither – they were merely theoretically inherited. BUT, once we executed an inherited x1 method, then the second-time list included it, while still missing inherited y2.


    2. But you can NOT, unfortunately, distinguish a subroutine that is an object method (e.g. treats the first argument it gets as an object), a class method (e.g. treats the first argument it gets as a class name) or a non-OO sub (treats first argument as regular argument).

      To distinguish between the 3, the ONLY way is to actually semantically analyze the code. Otherwise, you can’t tell the difference between:

      sub s_print_obj {
          my ($self, $arg1) = @_;
          $s->{arg1} = $arg1;
          print "$arg1\n";
      }
      # $obj->s_print_obj("XYZ") prints "XYZ" and stores the data in the object
      
      sub s_print_class {
          my ($class, $arg1) = @_; 
          print "Class: $class\n";
          print "$arg1\n";
      }
      # $obj->s_print_class("XYZ") prints "Class: MyClass\nXYZ\n"
      
      sub s_print_static {
          my ($self, $arg1) = @_;
          print "$arg1\n";
      }
      # $obj->s_print_static("XYZ") prints stringified representation of $obj
      

      NOTE: As a matter of fact, some people actually write their class’s methods – those that CAN work this way – to explicitly work in ALL 3 (or first 2) cases, no matter how the method is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl program that is reading html tags from a text file.
I have a Perl program that has a GTK2 GUI (via the Gtk2 package).
We have a Perl program that ran well on all Windows platforms so far.
I have a perl program that takes input and output file arguments, and I'd
I have a Perl script that is calling a program on Windows using backticks.
I'm in the processing of converting a program from Perl to Java. I have
I have a Perl program that I've written that parses SQL-like statements and creates
I have a program that downloads basic historical stock data from yahoo and puts
I'd like to have a perl program that I can call with something like:
I have a plain perl script that can be run from the command-line via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.