#!/usr/bin/perl use strict; local $\ = '-' x 40 . "\n"; eval { my $test = CGI::thisFunctionDoesNotExist() }; print $@; eval { my $test = thisFunctionDoesNotExist CGI() }; print $@; eval { my $test3 = new Some::Package::That::Doesnt::Exist() }; print $@; # A function that does not exist in main:: functionThatDoesntExist('test'); exit; sub UNIVERSAL::AUTOLOAD { my $method = $UNIVERSAL::AUTOLOAD; # So that we have the Carp methods without # cluttering up our UNIVERSAL namespace use Carp (); # First, if they are in the main namespace, # Fail, since we don't have a package to load if ($method =~ /^main::/) { Carp::croak("AUTOLOAD failed: $method"); } # Else, split the name into package and method names my @methodParts = split('::', $method); my $methodName = pop(@methodParts); my $package = join('::', @methodParts); # Now, load the package. # Die if it fails eval "use $package"; Carp::confess($@) if ($@); # If you made it here, do standard autoload stuff # Check which form of method call this is # This will catch the packageName::methodName format # The methodName packageName format is already good if ($_[0] ne $package) { unshift @_, $package; }; no strict; # the goto call will pass along @_ transparently goto &$method; };