ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:
To learn Perl classes I start reading perlootut Perl 5 version 22.0 documentation. In that I saw this example. As per my understanding the new works like constructor.
#!/usr/bin/perl use strict; use warnings; package File; my $hostname = File->new( path => '/etc/hostname', content => "foo\n", last_mod_time => 1304974868, ); use Scalar::Util 'blessed'; #print blessed($hash); # undef print blessed($hostname); # File
I wrote this from the example of the documentation. But its showing an error like Can't locate object method "new" via package "File" at package.pl line 9. Whats the problem in this???
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Learning classes in Perl
by 1nickt (Canon) on Feb 19, 2016 at 04:09 UTC | |
Hi ravi45722 You are a little mixed up; you've copied code snippets meant to illustrate different things and patched them together, but the result doesn't make sense. Your package will contain the constructor sub new() and your script that uses the package will call new() to create an instance of the object. Also, even though that tutorial shows package 'File' as an example, it's customary to avoid single-word package names unless they are distinctive; you would be safer with either MyFile or My::File. Update in response to OP's reply: Are you sure you're ready to tackle OOP in Perl? Have you already become familiar with packages in general? Here's a simple example of a package in a module and a script that uses it, download it and try it out:
Usage:
Now if you wanted to do the same thing with OOP, making an object for your day, you might do something like this:
Usage: Hope this is a little clearer now! Update: Changed 'package must return a true value' to 'module must return a true value', thanks Discipulus.
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
by jdporter (Paladin) on Feb 20, 2016 at 01:48 UTC | |
(This is answer is not intended for the OP, as it will no doubt be over his head...) To be computer-linguistically pedantic, new is not a constructor, but a factory class method. That is, it's a class method (which in perl means it is invoked with the class name passed as the first argument, rather than an object reference[1]); and it is a factory method, meaning its purpose is to return objects which probably[2] didn't exist prior to the call. POOP does not have constructors. (I can't speak for any other OO frameworks for perl; one or more of them may support something closer to a classical constructor.) [1] Of course, there are always exceptions. This is Perl, after all. If the sub is written appropriately, it can be both a class method and an object method. Or both of those *plus* support non-method invocation syntax. CGI commits this bletchery, for example. [2] Of course, there are always exceptions. For example, one common pattern is for the factory method of a class to return the same instance always -- the "singleton". One consequence of these factoids is that there is nothing inherently special about "new". Using that as the name of the factory method is mere convention. Another consequence is that any method can be a factory method, if it's written to be one. There are no limitations.
I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.
| [reply] [d/l] |
by Anonymous Monk on Feb 20, 2016 at 02:26 UTC | |
To be computer-linguistically pedantic, new is not a constructor, but a factory class method. Meh, if you use it exactly like a constructor, its a constructor I tells ya | [reply] |
by jdporter (Paladin) on Feb 20, 2016 at 16:48 UTC | |
by ravi45722 (Pilgrim) on Feb 19, 2016 at 04:18 UTC | |
To be frank I don't understand what you said. I am very new to this OOP concept. Please guide to rectify that error. I am taking that example from the http://perldoc.perl.org/ But still it showing error. I dont understand why?? I am in right side to learn OOP ???? | [reply] [d/l] |
by poj (Abbot) on Feb 19, 2016 at 09:38 UTC | |
perlobj should explain this for you. poj | [reply] [d/l] |
|
Re: Learning classes in Perl
by soonix (Chancellor) on Feb 19, 2016 at 09:39 UTC | |
Of course, you could name your constructors differently, e.g. after defining you would use File->File instead of File->new. | [reply] [d/l] [select] |
|
Re: Learning classes in Perl
by GotToBTru (Prior) on Feb 19, 2016 at 13:08 UTC | |
The constructor is not automatically created for you, which is why Perl complains it can't locate the method 'new'. Doing OO in Perl is kind of clunky, which is why most people like to use packages like Moose or Moo or any one of several others. Many of them do things like automatically creating constructors, getters and setters, and handling alot of the overhead issues for you. This comes with a cost of added complexity, but if you're really going to code OO in Perl, it is worth your time to invest in learning. You will get that time back multiplied many times in coding and maintenance. | [reply] |
|
Re: Learning classes in Perl
by perlfan (Parson) on Feb 22, 2016 at 16:33 UTC | |
And while a little dated, there is no modern equivalent. | [reply] |
by Your Mother (Archbishop) on Feb 22, 2016 at 17:05 UTC | |
I second this recommendation with the caveat that the book has some advice that I would consider misplaced (like the inside-out stuff) and none of the advanced OO frameworks in Perl existed when it was written. This means a lot of the otherwise excellent code suggestions are (probably) moot for most applications. The enduring strength of the book for me was that it taught me how to think about complicated programming tasks in a much more sophisticated way than I'd been managing on my own and showed me why some of the critique/FUD of the so called "bolt-on" OO in Perl is unfounded. | [reply] |
|
Re: Learning classes in Perl
by zenadu (Initiate) on Feb 24, 2016 at 19:35 UTC | |
Hi ravi45722, Ignoring the illicit nature of the example, this code is about as basic an example of a complete object oriented implementation from scratch in Perl as I have ever seen. I have taken it from the blog post at <link removed because it was an unapproved site or something>. Edit: Should have also mentioned, don't even think of doing OO from scratch in perl until after you have completely read at least perlootut, perlreftut, and perlref and have a good handle on hashes and references. If you don't have a good handle on hashes, read perldata completely. After that, OO in perl is actually pretty straight forward and nowhere near as nightmarish or hackish as some suggest. Also if you're not going to read the whole thing, at least keep a copy of perlobj around for reference as perlootut mainly only covers doing OO perl using class builders which do the boilerplate of constructors and accessors for you.
| [reply] [d/l] [select] |