in reply to Re^4: Is it ok to mix functional and oo programming in one package?
in thread Is it ok to mix functional and oo programming in one package?

Is there some way to do this:

#!/usr/bin/perl use warnings; use strict; use Foo; # some "forward declaration" syntax instead of looking for Fo +o.pm? sub new { print "haha"; return; } my $a = new Foo; $a->hello; package Foo; sub new { return bless {},shift; } sub hello { print "hello"; }

I couldn't find it under use in perlfunc. I was also trying use main::Foo; and use ::Foo; based on some ideas that popped into my head while reading about Packages in perlmod.


I humbly seek wisdom.

Replies are listed 'Best First'.
Re^6: Is it ok to mix functional and oo programming in one package?
by Joost (Canon) on Oct 19, 2007 at 21:00 UTC
    Anything that unambiguously uses a package automatically declares a package, and so does a package Foo statement.

    So you could do

    package Foo; package main; # or whatever package is the 'current' one
    Which is probably the clearest way of doing it, or you can do something like:
    $Foo::a=$Foo::a; # using $Foo::a twice will silence the 'used only on +ce' warning
    Note that fully specified package variables are exempt from 'strict "vars"' rules. Or you can declare a subroutine:
    sub Foo::declare_package_Foo;
    The same technique would work for anything else that unambiguously uses the Foo package.