Perl OO: First attempt, why doesn't this code work?
I have been through O'Reilly's "Learning Perl References, Objects and Modules", 1st edition. I thought I had a pretty good handle on it, but I am stumped by this first attempt. Why do I keep getting the "blessed scalar reference as a string" (section 9.8) back?
Code:
#!/usr/bin/perl -w Code:
Here is the hash entry at HASH_1: THIS IS A TEST Code:
Here is the hash entry at HASH_1: TESTOO=HASH(0x8320d54)->sub_A |
Perhaps this is more like what you want:
Code:
#!/usr/bin/perl -w --- rod. |
You do not need, and ought not have, the enclosing braces { ... }.
Customarily, you would put the package in (say...) TESTOO.pm, with the package statement as the first line and always ending the file with this: Code:
1; However, you can have more than one package statement in the same file ... (Perl is very agreeable) ... for example: package main; ... and you could, if you wanted to, just write the test-code right there in the same package. One more thing: do this... use strict; use warnings;. I suggest that you get to know http://www.perlmonks.org. Funny, Perl was one of the most recent languages that I picked-up, but, danged if I haven't found myself using it fairly constantly ever since. It is an extremely practical and pragmatic, "just move the dammed freight," language, of extraordinary strength and ability. Perhaps most importantly, its battle-tested CPAN library goes on forever... |
theNbomr, it turns out the only thing that was causing my basic problem was the print commands, when I took the object reference entries out of the quotes, then I started getting the results I wanted. Except, I wasn't getting the print command from sub_B. Here is what I am getting now:
Code:
#!/usr/bin/perl -w Code:
Here is the hash entry at HASH_1: THIS IS A TEST So why am I getting the "This is a test." back in line two, and why isn't $test->sub_B giving me the print command at the printed string instead of the simple "1"? (Indicating a successfully completed command or something, I don't know) sundialsvcs, yes I see now that the brackets around the package part is not necessary, I tried it without and it works fine. And yes, my project includes modules. |
On last line, print evaluates terms in list context
Better use: Code:
print "Here is the hash entry at HASH_1: ", $test->sub_A,"\n"; |
Problem solved
Yes, maybe explicitly returning from a method is best. And yes, I see how print changes the way a sub works inside the quotes.
Here is an improvement showing better understanding, Code:
#!/usr/bin/perl -w Code:
Useless use of private variable in void context at TESTOO.pl line ... |
Instead of
Code:
ref $self ? $self = "OBJECT METHOD" : $self ; Code:
$self = "OBJECT METHOD" if ref $self; |
Yes, that's better. Thanks.
|
All times are GMT -5. The time now is 02:29 PM. |