Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Method calling question...

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

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'.
(tye)Re: Method calling question...
by tye (Sage) on May 01, 2001 at 02: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

      A reply falls below the community's threshold of quality. You may see it by logging in.
          "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")
Re: Method calling question...
by MeowChow (Vicar) on May 01, 2001 at 02: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

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
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found