Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Create a constructor named the same as your package name!

by merlyn (Sage)
on Jul 01, 2004 at 12:47 UTC ( #371060=snippet: print w/replies, xml ) Need Help??
Description: Why type ->new? Have your package create a function so that the package name itself is the constructor! For example, you can have:
my $e = Example::Module->new(@list);
my $e = Example::Module(@list);
Both are supported by the snippet code! Even works if ->new is inherited, and doesn't disturb any other class methods.
package Example::Module;


sub Example::Module {
  return "Example::Module" unless @_;
  Example::Module::->new(@_); # extra colons are necessary


Replies are listed 'Best First'.
Re: Create a constructor named the same as your package name!
by DrHyde (Prior) on Jul 01, 2004 at 13:03 UTC
    So you've created a subroutine in some other namespace. That's not something I'd want to encourage.
Re: Create a constructor named the same as your package name!
by Solo (Deacon) on Jul 01, 2004 at 13:41 UTC
    Neat idea, but what if a constructor needs no arguments?

    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

      I'm confused, what's wrong with this?

      my $e = Example::Module();
        my $e = Example::Module();
        That would be the same as:
        my $e = "Example::Module";
        So no, it doesn't work. I thought about it for a while... and I can't figure out a way around that corner case that doesn't break other class methods. Oh well... gotta say "new" sometime, I guess.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://371060]
[choroba]: but gurus say they in fact happen left to right, but we understand them wrongly
[marioroy]: bad gurus :)
[choroba]: also, &> redirects both at the same time
[marioroy]: erix are you using csh
Discipulus uh the link was for cmd.exe the bash one
[marioroy]: If yes, for redirection to work, one might do the following
[marioroy]: `bash -c "clang --version 2>/dev/null"`
[choroba]: Sane people don't use csh
[marioroy]: the 1>&2 will work, not shown
[marioroy]: sorry, did not know which if cmd.exe or unix (bash)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2017-03-23 08:21 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (284 votes). Check out past polls.