crazedrat has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Newbie question under time constraint
by roboticus (Chancellor) on Aug 04, 2019 at 19:37 UTC | |
You'll generally have better luck with your classes if you design your classes further in advance of when they are due.... 8^) Anyway, a few notes to help you on your way: I tend to use a package like Data::Dump for debugging,as it will print the information in an easy format, so I can verify that the data is or isn't what I expect to see. Instead of your main program, I used this:
It looks like you were a bit optimistic. The Employee class looks like it constructs itself correctly, but the print_student method does no such thing:
Here, the problem appears to be a simple naming issue--a method named "print_student" implies that (a) it's actually going to print something, and (b) that it's going to print a student. There's nothing in your Employee class that lets us figure out whether it's actually a student or not, so I'm guessing it's a holdover from a previous assignment. I'd suggest renaming your method to indicate what it's actually going to do, which is to format the employee as a string. I'll just settle for calling it "format" for now and updating the code. After making the appropriate edits, I get:
OK, that went well. Next let's add a couple more Employees, make an EmployeeList, add the Employees to the list and see what we get:
OK, when we run it, we get:
Overall, it's not too bad. We can successfully create the list and add employees to it and fetch it back. The big problem at the moment is that the EmployeeList format() function just stringifies the list, making an ugly ARRAY(0x########) string rather than showing us a list of Employees. To handle this, in your EmployeeList format() function, you'll want to iterate over each Employee in the list and then format each individual item. To start you along, here's how you can iterate in your format function:
Here are a few suggestions on improving the code:
My final bit of advice: Rather than writing the entire program at once, build it up feature by feature. If you have two hundred lines of untested code and have an error, you don't really know where that error is. Finding it can be a problem. But if you have a working program that you understand, and then write 20 lines of code to add a new feature, and then have a problem, it's easier to home in on the location of the error. Don't be afraid to add print statements anywhere in your program to see what's happening--you can take them out later when you don't need them. Even better--try to learn the debugger. It can be *very* educational running your program in the debugger since you can stop the code anywhere you want, examine values to see if they hold what you expected or not. You can even change data values to see what would happen in other cases. ...roboticus When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] [select] |
|
Re: Newbie question under time constraint
by holli (Abbot) on Aug 04, 2019 at 18:49 UTC | |
holli You can lead your users to water, but alas, you cannot drown them. | [reply] [d/l] |
by choroba (Cardinal) on Aug 04, 2019 at 22:01 UTC | |
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] [select] |
by Your Mother (Archbishop) on Aug 04, 2019 at 22:38 UTC | |
My Paypal for the twenty. Please note that a public offer of a reward is legally binding. Yeah, maybe in small claims court. And taking money to do someone’s homework if this is what it smells like is… Well, no penalties for you but the cat could maybe lose a course credit or even be expelled depending on school rules; paying you means his legal name is on record so… Enjoy your $20! | [reply] |
| |
| |
|
Re: Newbie question under time constraint
by 1nickt (Canon) on Aug 04, 2019 at 22:56 UTC | |
Hi, welcome to Perl, the One True Religion. Couple of etiquette notes: Asking for people to help you urgently because you are under a deadline usually has the opposite effect. Offering to pay for code here will usually get you sent to https://jobs.perl.org. But, effort is encouraged and you've shown plenty, good job getting as far as you have. Couple of general programming / asking for technical help notes: Regarding your program my advice is to use one of the many object frameworks Perl already has. Not only because the features that a good framework has that you are replicating by hand, are stable, tested, efficient, but also because a good OOP framework will allow you to expand your app into new levels of complexity as it grows while hiding the majority of the dirty work. Personally I never write anything OOP without Moo. Here's something like what you described. (Note only one file.)
Output:
Hope this helps! Update: added some more methods for fun
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |