in reply to Can I do single-file, Object oriented perl?

package tom; sub new { my $self = "This is tom"; bless ($self, $class); }
That's very wrong. You should only bless references. This piece of code here is bound to fail.

I have tried creating a .pl file and the keyword "package" throws errors.
Are you sure you're not trying to run it with perl4?

Replies are listed 'Best First'.
Re^2: Can I do single-file, Object oriented perl?
by FatDog (Beadle) on Jul 05, 2005 at 19:27 UTC
    That's very wrong. You should only bless references. This piece of code here is bound to fail.

    Apologies. Of course my production code does something like this:

    my $self = { user_name => 'fubar', num_days => 0, }; bless ($self, $class);

    I threw the tom/dick/harry example together from memory. I wanted to emphasize that I was defining 3 classes in the same file - something that other OO languages do not like.

    If it works in the .pm, it'll work in the .pl. Another error must have been introduced.

    Ikegami - I think you got it - but with one exception:

    You have to include shebang (#!/usr/bin/perl) at the top of a .pl, but not at the top of a .pm file.

    I took my working .pm file and tried to get it to work as a .pl and it threw errors. But I did not notice the error messages were BASH error messages instead of Perl messages. (duh)

    I put shebang at the top of the script - and now it actually works. Thanks Monks!

      If it works in the .pm, it'll work in the .pl. Another error must have been introduced. Ikegami - I think you got it - but with one exception: You have to include shebang (#!/usr/bin/perl) at the top of a .pl, but not at the top of a .pm file.
      Just to nitpick a tad, the shebang has nothing to do with the difference between a ".pl" and a ".pm". Perl sees no difference between files, whatever their extension happens to be. The only practical difference is that 'use' and 'require' will append a .pm to the package name you pass them. In any case, anything involving a shebang has to do with your shell/kernel.