Loading ...
Sorry, an error occurred while loading the content.

Operator Overloading

Expand Messages
  • Jeff Eggen
    Hi all, I recently tried my hand at operator overloading for some classes I have written, so that I can do clever things like: if ( $object1 eq $object2 ) I
    Message 1 of 5 , Jun 1, 2004
    • 0 Attachment
      Hi all,

      I recently tried my hand at operator overloading for some classes I have written, so that I can do clever things like:

      if ( $object1 eq $object2 )

      I have only overloaded the 'eq' operator, with the following code:

      use overload
      'eq' => \&test_equals;

      sub test_equals
      {
      ...
      }

      The subroutine returns 0 if they aren't determined to be equal, and 1 otherwise. Using this setup, the if test works perfectly. However, now when I use lines like this:

      use Class;
      my $object1 = Class->constructor('parameter')
      or die "error creating object!\n";

      I get the following error:

      Operation `bool': no method found, argument in overloaded package Class at ...

      So, I didn't notice this in the Programming Perl chapter on overloading, but maybe I just missed it: do I always have to implement an overloaded 'bool' operator if I'm going to overload any other ones? Am I missing something simple in the documentation, or have I stumbled across a 'gotcha'?

      Thanks,

      Jeff Eggen
      IT Programmer Analyst
      Saskatchewan Government Insurance
      Ph (306) 751-1795
      email jeggen@...

      ************DISCLAIMER*************
      This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the named addressee, please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that using, disclosing, copying or distributing the contents of this information is strictly prohibited.
      ************DISCLAIMER*************
    • Jeff 'japhy' Pinyan
      ... You missed something. The overload docs mention a fallback method. use overload ( eq = &test_equals, fallback = 1, ); The explanation in
      Message 2 of 5 , Jun 1, 2004
      • 0 Attachment
        On Jun 1, Jeff Eggen said:

        >use overload
        > 'eq' => \&test_equals;

        >use Class;
        >my $object1 = Class->constructor('parameter')
        > or die "error creating object!\n";
        >
        >Operation `bool': no method found, argument in overloaded package Class
        >at ...
        >
        >So, I didn't notice this in the Programming Perl chapter on overloading,
        >but maybe I just missed it: do I always have to implement an overloaded
        >'bool' operator if I'm going to overload any other ones? Am I missing
        >something simple in the documentation, or have I stumbled across a
        >'gotcha'?

        You missed something. The 'overload' docs mention a 'fallback' method.

        use overload (
        'eq' => \&test_equals,
        fallback => 1,
        );

        The explanation in overload.pm:

        Fallback

        The key "fallback" governs what to do if a method for a particular
        operation is not found. Three different cases are possible depending
        on the value of "fallback":

        * "undef" Perl tries to use a substituted method (see "MAGIC
        AUTOGENERATION"). If this fails, it then tries to
        calls "nomethod" value; if missing, an exception will
        be raised.

        * TRUE The same as for the "undef" value, but no exception is
        raised. Instead, it silently reverts to what it would
        have done were there no "use overload" present.

        * defined, but FALSE
        No autogeneration is tried. Perl tries to call
        "nomethod" value, and if this is missing, raises an
        exception.

        --
        Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
        RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
        CPAN ID: PINYAN [Need a programmer? If you like my work, let me know.]
        <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
      • Jeff Eggen
        ... D oh! I must have read that page about 4 times and it never clicked in. I kept thinking it was only related to the whole deal where overloading and
        Message 3 of 5 , Jun 1, 2004
        • 0 Attachment
          >>> japhy@... 01/06/2004 4:32:44 pm >>>
          >You missed something. The 'overload' docs mention a 'fallback' method.

          D'oh! I must have read that page about 4 times and it never clicked in. I kept thinking it was only related to the whole deal where overloading <=> and cmp define the other comparison operators. Didn't think that the others would be affected too.

          I added the 'fallback => 1' to the use overload pragma, and that has worked perfectly. Thanks very much Japhy! Time to read that chapter again.

          Thanks again,

          Jeff Eggen
          IT Programmer Analyst
          Saskatchewan Government Insurance
          Ph (306) 751-1795
          email jeggen@...

          ************DISCLAIMER*************
          This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the named addressee, please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that using, disclosing, copying or distributing the contents of this information is strictly prohibited.
          ************DISCLAIMER*************
        • vinay mysore
          Hi, I am not able to run SSI on apache webserver, perl/ cgi scripts run perfectly but i am not able to run SSI. i am using windows 2000 and have all the
          Message 4 of 5 , Jun 2, 2004
          • 0 Attachment
            Hi,
            I am not able to run SSI on apache webserver, perl/
            cgi scripts run perfectly but i am not able to run
            SSI. i am using windows 2000 and have all the
            privilages tat r required to run the server. I have
            incorporated the following lines in the config file.
            it would be nice if anyone can help me out with this.
            Options +Includes
            AddType text/html .shtml
            AddHandler server-parsed .shtml






            __________________________________
            Do you Yahoo!?
            Friends. Fun. Try the all-new Yahoo! Messenger.
            http://messenger.yahoo.com/
          • daymobrew@yahoo.com
            ... Has the server been restarted to re-read the update httpd.conf? What errors are you getting? Are you seeing the SSI directives instead of them being
            Message 5 of 5 , Jun 3, 2004
            • 0 Attachment
              --- In perl-beginner@yahoogroups.com, vinay mysore <vinay_ma@y...> wrote:
              > Hi,
              > I am not able to run SSI on apache webserver, perl/
              > cgi scripts run perfectly but i am not able to run
              > SSI. i am using windows 2000 and have all the
              > privilages tat r required to run the server. I have
              > incorporated the following lines in the config file.
              > it would be nice if anyone can help me out with this.
              > Options +Includes
              > AddType text/html .shtml
              > AddHandler server-parsed .shtml
              >

              Has the server been restarted to re-read the update httpd.conf?
              What errors are you getting? Are you seeing the SSI directives instead
              of them being parsed?
              Any messages in the error_log?
              Have you read
              http://httpd.apache.org/docs/howto/ssi.html

              Bit of repeated info at:
              http://httpd.apache.org/docs/misc/FAQ.html#ssi-part-i

              If you have enbabled the server-info (mod_info) you can read about the
              server configuration. This is a good way to see whether the httpd.conf
              file has been read as expected:
              <Location /server-info>
              SetHandler server-info
              Order deny,allow
              Deny from all
              Allow from 119.
              </Location>
            Your message has been successfully submitted and would be delivered to recipients shortly.