Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
There's more than one way to do things
 
PerlMonks  

Method calling question...

by satchboost (Scribe)
on Apr 30, 2001 at 22:14 UTC ( [id://76788]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

I've got the following setup:

Base.pm: package Base; sub method1 { # Insert code here }; Foo.pm: package Foo; use Base; @ISA = qw(Base); Bar.pm: package Bar; use Base; @ISA = qw(Base); Main.pm ... foreach my $classname qw(Foo Bar) { $classname->method1; }

I know that this code doesn't work. What I'm wondering is what will work for what I'm trying to do?

Replies are listed 'Best First'.
Re: Method calling question...
by MeowChow (Vicar) on Apr 30, 2001 at 22:23 UTC
    I don't see what the problem is:
    package Base; sub method1 { # Insert code here print "Called from class: ", shift, "\n"; }; package Foo; @ISA = qw(Base); package Bar; @ISA = qw(Base); package main; foreach my $classname qw(Foo Bar) { $classname->method1; } ## Output ## Called from class: Foo Called from class: Bar
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      There's a reason I was using four files. If you have everything in one file, of course it works cause method1 is scoped within the file. When you have it across several files, it breaks down. :(
        It's not a scoping issue:
        foreach my $classname qw(Foo Bar Oops) { $classname->method1; } ## output ## Can't locate object method "method1" via package "Oops" at C:\Projects +\GPerl\Test\2.pl line 16. Called from class: Foo Called from class: Bar
           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
(tye)Re: Method calling question...
by tye (Sage) on Apr 30, 2001 at 22:43 UTC

    I can think of a few reasons why this might fail.

    First, I don't see any require Foo nor require Bar (or replace require with use).

    Second, you can replace @ISA= qw(Base) with use base qw(Base) which has some advantages. But I have more important reasons for mentioning that: On file systems that ignore upper- vs. lower-case in file names, use Base could end up loading base.pm instead of Base.pm, which would also break your code.

            - tye (but my friends call me "Tye")
      Second, you can replace @ISA= qw(Base) with use base qw(Base)

      This sounds very interesting. Could you point me to a man page about this (or explain a little more about what Perl mechanism that makes it work)?

      /J

        That'd be described in perldoc base, wouldn't it? Have you looked there first?

        -- Randal L. Schwartz, Perl hacker


        update: This dropped two points immediately after posting, so here's another "personality vote" potential candidate list:
        tilly btrott jcwren lhoward MeowChow BigJoe Beatnik Macphisto jplindstrom jlp LD2 Kanji isotope kandinsky batmonk joee koolade UberGeek prion
        I'll get enough of these and start performing an intersection.
          "On file systems that ignore upper- vs. lower-case in file names, use Base could end up loading base.pm instead of Base.pm..."

      Uhm, no.

      If the file system is case-insensitive, then Base.pm == base.pm.

      -- Dave

        Perhaps you neglect to consider that require and use search more than one directory. If Base.pm is in your current directory, then base.pm from the standard Perl install will be found and used instead of your file.

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://76788]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.