in reply to Re: I have a perl snippet. And I need help understanding it. Can you help answer these questions.
in thread I have a perl snippet. And I need help understanding it. Can you help answer these questions.

I also now notice that 'new' is not a Perl keyword and it was another subroutine defined (and derived) from the pm file which I fail to notice and see. I should have realized that all Perl keywords are highlighted in blue color in my editor(Notepad++). btw, in case anyone's confused,new is a log parser, it takes a file and a filterlist and puts all the values into a variable.
  • Comment on Re^2: I have a perl snippet. And I need help understanding it. Can you help answer these questions.

Replies are listed 'Best First'.
Re^3: I have a perl snippet. And I need help understanding it. Can you help answer these questions.
by davido (Cardinal) on Nov 08, 2011 at 05:27 UTC

    Correct: new() is not a built-in. I assumed that it existed in the module, and that you just didn't include that portion of the code. Otherwise, the code you posted wouldn't have made much sense. new() is the "constructor" for the object, and it has to be written by the module's author. There's nothing special about the name new() either. DBI uses connect(), and other modules use other names that make sense as constructors for their respective classes. However, unless you have a good reason, new() is usually the most predictable and sensible name to use.

    One thing: The code you posted is really a bad example to learn from. It's considered a bad practice to deal with the object's internals directly from outside the object, and that's what the example code is doing. The class's author should be free to implement the object however he wants internally, and expose its internals through well-defined accessors in a way that abstracts away the details of implementation. ...that's at least my opinion about what code I saw posted. But I understand you've got the hand of cards that some previous developer handed you. Just be aware that when it comes time to actually learn how to write OO in Perl, Intermediate Perl is an excellent resource. And Modern Perl (by chromatic) will get you started with Moose (Perl's newer, more palatable object system).


    Dave