By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,378 Members | 3,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,378 IT Pros & Developers. It's quick & easy.

Php/perl class/package field definition

P: n/a
My brain must be running slow because of New Year, but I'm missing the
trick to define fields for a package.

In php, I'd do:
----
<?php
class MyObj{
var $param1;
function MyObj(){$this->param1 = "Hello";}
function Hello(){echo $this->param1;}
}
$test = new MyObj;
$test->Hello();
?>
----
However this doesn't seem to be the correct perl translation:
----
package MyObj;
my $param1 = "";
sub new {
my $newobject = {};
bless $newobject;
my $param1 = "Hello";
return $newobject;
}
sub Hello {
print my $param1;
}
$test = new MyObj;
$test->Hello();
----
Ideas anyone ? (besides switching to php, which I unfortunatly can't).

PS: I tried posting on the perl newsgroup first, but Google Groups
didn't seem to be in the mood ("comp.lang.perl doesn't exist", rofl).

Jul 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Wildpeaks wrote:
Ideas anyone ? (besides switching to php, which I unfortunatly can't).

sub new {
my $newobject = shift;
bless {};
$param1 = "Hello";
return $newobject;
}
PS: I tried posting on the perl newsgroup first, but Google Groups
didn't seem to be in the mood ("comp.lang.perl doesn't exist", rofl).


http://groups.google.nl/groups?hl=nl...comp.lang.perl

But since this is very basic Perl, the only reply would have been RTFM
anyways ;-)
JW

Jul 17 '05 #2

P: n/a
Janwillem Borleffs wrote:
sub new {
my $newobject = shift;
bless {};
$param1 = "Hello";
return $newobject;
}


To complete my reply, the Hello sub should be as follows:

sub Hello {
print $param1;
}

Now, back to PHP!

JW

Jul 17 '05 #3

P: n/a
Janwillem Borleffs wrote:
Janwillem Borleffs wrote:
sub new {
my $newobject = shift;
bless {};
$param1 = "Hello";
return $newobject;
}


To complete my reply, the Hello sub should be as follows:

sub Hello {
print $param1;
}

Now, back to PHP!


Yup, since your Perl is bad :-(

package MyObj;

my $param1 = 'Hello';

sub new { return __PACKAGE__ }
sub Hello { print $param1 }

package main;

my $test = new MyObj;
$test->Hello();


--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #4

P: n/a
> > PS: I tried posting on the perl newsgroup first,
but Google Groups didn't seem to be in the mood
("comp.lang.perl doesn't exist", rofl). http://groups.google.nl/groups?hl=nl...comp.lang.perl


I *can* view the ng, but when posting, it was refusing
to post at the last step.

But since this is very basic Perl, the only reply
would have been RTFM anyways ;-)


I had RTFM but hadn't find the answer to that precise question,
therefore the post, heh. I post in ng only in last resort.
Ah, PHP is so much easier :-)

Jul 17 '05 #5

P: n/a
John Bokma wrote:
Yup, since your Perl is bad :-(


You never use strict, don't you?

And please enlighten me why your perl is better, because I don't see it.
JW

Jul 17 '05 #6

P: n/a
Janwillem Borleffs wrote:
John Bokma wrote:
Yup, since your Perl is bad :-(


BTW, it should be __PACKAGE__
JW

Jul 17 '05 #7

P: n/a
>Yup, since your Perl is bad :-(
[...]


Ah it was one damn word then, thanks :-)

Jul 17 '05 #8

P: n/a
Janwillem Borleffs wrote:
BTW, it should be __PACKAGE__


Nah, you had it right. But I still want to know from you why think that your
syntax is better.
JW

Jul 17 '05 #9

P: n/a
Janwillem Borleffs wrote:
Nah, you had it right. But I still want to know from you why think
that your syntax is better.


The difference appears to be that my constructor adds an anonymous hash to
the object, which isn't used anymore, while your constructor doesn't.

In that context I can see why you think my perl coding is bad...
JW

Jul 17 '05 #10

P: n/a
Janwillem Borleffs wrote:
Janwillem Borleffs wrote:
Nah, you had it right. But I still want to know from you why think
that your syntax is better.


The difference appears to be that my constructor adds an anonymous
hash to the object, which isn't used anymore, while your constructor
doesn't.

In that context I can see why you think my perl coding is bad...


:-). It's a nice way to create a singleton, just returning the __PACKAGE__
;-)

I didn't do the use strict; use warnings; since it's a code snippet. In
real world stuff I use it, as anyone else should (unless they really know
what they are doing).

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #11

P: n/a
Wildpeaks wrote:
> PS: I tried posting on the perl newsgroup first,
> but Google Groups didn't seem to be in the mood
>("comp.lang.perl doesn't exist", rofl).

http://groups.google.nl/groups?hl=nl...comp.lang.perl


I *can* view the ng, but when posting, it was refusing
to post at the last step.

But since this is very basic Perl, the only reply
would have been RTFM anyways ;-)


I had RTFM but hadn't find the answer to that precise question,
therefore the post, heh. I post in ng only in last resort.
Ah, PHP is so much easier :-)


It's not. But if you are fluent in language A, and learning language B,
language A is much easier ;-)

PHP is extremely badly designed.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #12

P: n/a
> It's not. But if you are fluent in language A, and learning
language B, language A is much easier ;-)
Often true :-) but not always: the thing is, I actually started by perl
some years ago (and like everyone who learns a new language, I wanted
to use perl for everything :-D
However, after that, I learnt php, which I now use for everything I was
using perl for. Even worse, I totally forgot how to write perl (which
explains that I sometimes ask stupid questions unfortunatly).

PHP is extremely badly designed.


I wouldn't be able to tell if it's bad designed or faster/slower, I can
only tell as developer that in the everyday programs, I find it more
intuitive, also because PHP.net's documentation is imho very good. But
that's just my opinion. Other people only like C or ASP instead
(however I do prefer C over perl too).

Jul 17 '05 #13

P: n/a
Wildpeaks wrote:
It's not. But if you are fluent in language A, and learning
language B, language A is much easier ;-)
Often true :-) but not always: the thing is, I actually started by
perl some years ago (and like everyone who learns a new language, I
wanted to use perl for everything :-D
However, after that, I learnt php, which I now use for everything I
was using perl for. Even worse, I totally forgot how to write perl
(which explains that I sometimes ask stupid questions unfortunatly).


If you forgot how to write Perl in a short time, you never really
learned the language.
PHP is extremely badly designed.


I wouldn't be able to tell if it's bad designed or faster/slower


extremely bad designed
quite likely slower
, I can
only tell as developer that in the everyday programs, I find it more
intuitive,
Probably because you never really learned Perl in the first place.
also because PHP.net's documentation is imho very good.
IMNSHO it's even worse than the language design. Especially the so
called user comments, half of it are garbage.
But
that's just my opinion. Other people only like C or ASP instead
(however I do prefer C over perl too).


You can not just prefer A over B. You pick a language based on how good
it helps you to get the job done. Hence, I sometimes have to use PHP.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.