Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

calling a subroutine while Initializing new

by opensourcer (Monk)
on Sep 17, 2006 at 01:48 UTC ( [id://573366]=perlquestion: print w/replies, xml ) Need Help??

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

hi,

i'm creating new instance like this:

my $instance = new MYPACKAGE(READ_ONLY => 1, \&subroutine);

could any 1 explain where the subroutine in defined, i mean in the same program or in package called MYPACKAGE. if possible with example plz.

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: calling a subroutine while Initializing new
by chromatic (Archbishop) on Sep 17, 2006 at 02:49 UTC

    Which subroutine?

    If you're talking about the constructor new(), it should be in MYPACKAGE. (I say "should be" because with the syntax you're using there, a lot of weird things could happen. I recommend instead my $instance = MYPACKAGE->new( READ_ONLY => 1, \&subroutine );.)

    If you're talking about subroutine(), it depends. The only necessary condition is that there is a subroutine of that name in the current package. However it is in that package, whether by declaration or export or AUTOLOAD or installation into the symbol table otherwise, it just has to be there.

Re: calling a subroutine while Initializing new
by jkeenan1 (Deacon) on Sep 17, 2006 at 02:55 UTC

    Most typically, you would define the subroutine in the package where the constructor is being called -- probably package main;.

    Note: for the most part, Perl classes, modules and packages (with the exception of main) are generally spelled in upper and lower case: MyPackage or My::Package -- not in all upper case.

    Jim Keenan
Re: calling a subroutine while Initializing new
by neosamuri (Friar) on Sep 17, 2006 at 03:06 UTC

    If you are passing wanting to pass the subroutine to the module(as you are doing above), then the subrutine should be defined in the file that instantiates the object.

    If you are not wanting to pass the subroutine in to the module, then it should be in the package file and called from the new subroutine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://573366]
Approved by jkeenan1
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found