in reply to Moose: return null object

Are you saying you can't return undef from BUILDARGS? If so, then return undef from new.
sub new { ... return undef; ... }

Replies are listed 'Best First'.
Re^2: Moose: return null object
by halfcountplus (Hermit) on Apr 18, 2011 at 15:46 UTC

    Are you saying you can't return undef from BUILDARGS?

    That would be nice. Unfortunately, it's a fatal exception that requires catching:

    #!/usr/bin/perl -w use strict; package test; use Mouse; sub BUILDARGS { return undef } __PACKAGE__->meta->make_immutable(); my $eg = test->new(); print "All good!"; # nope...

    BUILDARGS did not return a HASH reference at ./test.pl line 11.

    ..then return undef from new.

    Yer not suppose to use your own "new", but I did try defining one and having it return undef. No errors, however, the object returned is still a hash ref, not undef:

    package test; use Mouse; sub new { return undef } __PACKAGE__->meta->make_immutable(); my $eg = test->new(); print $eg;

    test=HASH(0x2208f08)

      The title of this thread refers to Moose whereas your example uses Mouse. Moose behaves different in this example, so this might be considered to be a bug in Mouse?
      Not inlining a constructor for test since it defines its own construct +or. If you are certain you don't need to inline your constructor, specify +inline_constructor => 0 in your call to test->meta->make_immutable Use of uninitialized value $eg in print at test.pl line 14.

      Yer not suppose to use your own "new"

      That's an overstatement.

      No errors, however, the object returned is still a hash ref

      Sure, because you told Moose to use its own new instead of yours.

      package Module; use Mouse; sub new { return undef } __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); print __PACKAGE__->new();

      I think it used to warn when new existed and you inlined the constructor. It really should. (Upd: OH! Moose does warn, it's Mouse that doesn't. Mouse-- lamprecht++ )

        I think it used to warn when new existed and you inlined the constructor. It really should. (Upd: OH! Moose does warn, it's Mouse that doesn't. Mouse-- lamprecht++ )

        Yeah, that might have given me a clue, but I wouldn't count the lack of helping friendless as a genuine bug. Anyway, it works, so there was this something I missed, hooray. Thanks y'all.