in reply to Re: P248 programming perl
in thread P248 programming perl

Number 1 is just weird and number 2 doesn't compile.

Huh? 2 compiles perfectly well for me!


# Doggie.pm package Doggie; 1; sub new { # ignore args print "hello from Doggie::new\n"; }

use strict; use warnings; use Doggie; sub Doggie { print "hello from main::Doggie\n"; } my $objref1 = Doggie::->new(Tail => 'short', Ears => 'long'); my $objref2 = new Doggie:: Tail => 'short', Ears => 'long'; # The following is parsed as Doggie()->new(...) # my $objref3 = Doggie->new(Tail => 'short', Ears => 'long'); __END__

hello from Doggie::new hello from Doggie::new

Also, in addition to the use that adrianh cited, 1 would be useful in the rare case in which one wants to use a class that has the same name as a subroutine defined in the client package. In such a case, as the OP has already noted, the parser gets confused with 3.

the lowliest monk

Replies are listed 'Best First'.
Re^3: P248 programming perl
by wackattack (Sexton) on Jul 05, 2005 at 06:06 UTC
    wow, 2 questions:

    1)Why does the Doggy.pm need the "#1;"?

    2)And how did you get your post to collapse like that?

    P.S. Perlmonks rule! thanks for the help guys.
      1)Why does the Doggy.pm need the "#1;"?
      A module needs to end with a true value, 1; is the standard convention for that. You could also use "this is false";. It just has to be true. :)
      2)And how did you get your post to collapse like that?
      Enclose the poste in <readmore title="foo">text</readmore> tags



      holli, /regexed monk/

      1)Why does the Doggy.pm need the "#1;"?

      Just to clarify holli's point, the value of the last statement in the module file to be evaluated must be true. That's why, in Doggie.pm the "1;" line is not the last line of the file; the subsequent lines consist entirely of subroutine definitions, which are not evaluated.

      the lowliest monk

Re^3: P248 programming perl
by wackattack (Sexton) on Jul 05, 2005 at 06:25 UTC
    Also in Doggy.pm I tried inserting the code:

    print "$objref1->{'tail'}\n";

    And I couldn't get it to print short. What am I doing wrong?


    Sorry for all of the noob questions.

      As I defined it, the Doggie class has practically no API, other than the rather useless new method, which ignores its arguments and, despite its name, doesn't even return an object, new or otherwise! So the main thing you did wrong was trying to use a useless class :-) . (I wrote it that way just to illustrate that the code as written compiles and runs without errors or warnings.)

      But even if new had returned an object, then most OO programmers will tell you that it is wrong software-engineering-wise to attempt to access $objref1->{'tail'}, because doing so "violates encapsulation", i.e. abuses a knowledge of how an object is implemented (in this case, presumably, as a hashref). In contrast to my Doggie, a well-designed class will provide accessors for those bits of instance data that are accessible to client code. I.e., you would invoke a method such as $objref1->tail.

      BTW, just for the record, the variable name $objref1 (which I realize follows the example of PP3, p. 248) is somewhat redundant, because all Perl objects are references.

      the lowliest monk