Skip to content

Commit

Permalink
Item13897: Fixed a problem with @argv being emptied.
Browse files Browse the repository at this point in the history
- Merge from Item14152: Foswiki::Class imports 5.14 feature set into a
module. No need to always `use v5.14;' or a kind of.

- Removed @_newParameters from Foswiki::Exception.

- Merge from Item14152 of getNS() and fetchGlobal() function of Foswiki.pm.
  • Loading branch information
vrurg committed Sep 1, 2016
1 parent 85621e2 commit e9fecba
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 30 deletions.
79 changes: 77 additions & 2 deletions core/lib/Foswiki.pm
@@ -1,3 +1,4 @@
# See bottom of file for license and copyright information

package Foswiki;
use v5.14; # First version to accept v-numbers.
Expand Down Expand Up @@ -34,8 +35,6 @@ our $UNICODE = 1; # flag that extensions can use to test if the core is unicode
our $TRUE = 1;
our $FALSE = 0;
our $TranslationToken = "\0"; # Do not deprecate - used in many plugins
our $system_message; # Important broadcast message from the system
my $bootstrap_message = ''; # Bootstrap message.

# Note: the following marker is used in text to mark RENDERZONE
# macros that have been hoisted from the source text of a page. It is
Expand Down Expand Up @@ -904,6 +903,82 @@ sub saveFile {
close($FILE);
}

=begin TML
---++ StaticMethod getNS( $module ) => $globRef
Returns GLOB pointing to namespace of a module. Returns undef if module isn't
loaded.
=cut

sub getNS {
my ($module) = @_;

my @keys = split /::/, $module;

my $ref = \%::;
while (@keys) {
my $key = shift @keys;
my $sym = "$key\:\:";
return undef unless defined $ref->{$sym};
$ref = $ref->{$sym};
}
return $ref;
}

=begin TML
---++ StaticMethod fetchGlobal($fullName) => $value
Fetches a variable value by it's full name. 'Full' means it includes type of
variable data ($, %, @, or &) and package name. For & a coderef will be
returned.
The purpose of this function is to avoid use of =no strict 'refs'= in the code.
*Example:* fetchGlobal('$Foswiki::Extension::Sample::API_VERSION');
=cut

sub fetchGlobal {
my ($fullName) = @_;

$fullName =~ s/^([\$%@&])//
or Foswiki::Exception::Fatal->throw(
text => "Foswiki::fetchGlobal(): Invalid sigil in `$fullName'" );
my $sigil = $1;

my @keys = split /::/, $fullName;
my $symbol = pop @keys;
my $module = join( '::', @keys );

my $ns = getNS($module);

Foswiki::Exception::Fatal->throw( text => "Module $module not found" )
unless defined $ns;

state $sigilSub = {
'$' => sub { return ${ $_[0] } },
'%' => sub { return %{ $_[0] } },
'@' => sub { return @{ $_[0] } },
'&' => sub { return *{ $_[0] }{CODE} },
};
state $sigilKey = {
'$' => 'SCALAR',
'%' => 'HASH',
'@' => 'ARRAY',
'&' => 'CODE',
};

Foswiki::Exception::Fatal->throw(
text => "$sigil$symbol not declared in " . $ns )
unless defined $ns->{$symbol}
&& *{ $ns->{$symbol} }{ $sigilKey->{$sigil} };

return $sigilSub->{$sigil}->( $ns->{$symbol} );
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Expand Down
1 change: 0 additions & 1 deletion core/lib/Foswiki/AppObject.pm
Expand Up @@ -17,7 +17,6 @@ Method create() is imported from Foswiki::App class.
=cut

use Assert;
use Foswiki::Exception;

require Foswiki::Object;

Expand Down
4 changes: 1 addition & 3 deletions core/lib/Foswiki/Attach.pm
Expand Up @@ -15,10 +15,8 @@ use Assert;
use Unicode::Normalize;
use Foswiki qw(expandStandardEscapes);

use Moo;
use namespace::clean;
use Foswiki::Class qw(app);
extends qw(Foswiki::Object);
with qw(Foswiki::AppObject);

BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
Expand Down
24 changes: 17 additions & 7 deletions core/lib/Foswiki/Class.pm
Expand Up @@ -78,11 +78,14 @@ manually by the class using =with=.

use Carp;

require Foswiki;
require Moo::Role;
require Moo;
require namespace::clean;
use B::Hooks::EndOfScope 'on_scope_end';

use constant DEFAULT_FEATURESET => ':5.14';

our @ISA = qw(Moo);

my %_assignedRoles;
Expand All @@ -103,9 +106,14 @@ sub import {
);

my @p;
my @noNsClean = qw(meta);
my @noNsClean = qw(meta);
my $featureSet = DEFAULT_FEATURESET;
while (@_) {
my $param = shift;
if ( $param =~ /^:/ ) {
$featureSet = $param;
next;
}
if ( exists $options{$param} ) {
my $opt = $options{$param};
$opt->{use} = 1;
Expand All @@ -127,6 +135,8 @@ sub import {
$class->_apply_roles;
};

feature->import($featureSet);

namespace::clean->import(
-cleanee => $target,
-except => \@noNsClean,
Expand All @@ -139,11 +149,9 @@ sub import {
# Actually we're duplicating Moo::_install_coderef here in a way. But we better
# avoid using a module's internalls.
sub _inject_code {
my ( $name, $code ) = @_;
my ( $target, $name, $code ) = @_;

no strict "refs";
*{$name} = $code;
use strict "refs";
Foswiki::getNS($target)->{$name} = $code;
}

sub _apply_roles {
Expand All @@ -164,17 +172,19 @@ sub _assign_role {
sub _install_callbacks {
my ( $class, $target ) = @_;

Foswiki::load_package('Foswiki::Aux::Callbacks');
_assign_role( $target, 'Foswiki::Aux::Callbacks' );
_inject_code( "${target}::callback_names", \&_handler_callbacks );
_inject_code( $target, "callback_names", \&_handler_callbacks );
}

sub _handler_callbacks {
sub _handler_callbacks (@) {
my $target = caller;
Foswiki::Aux::Callbacks::registerCallbackNames( $target, @_ );
}

sub _install_app {
my ( $class, $target ) = @_;
Foswiki::load_package('Foswiki::AppObject');
_assign_role( $target, 'Foswiki::AppObject' );
}

Expand Down
4 changes: 1 addition & 3 deletions core/lib/Foswiki/Engine.pm
Expand Up @@ -20,10 +20,8 @@ use Assert;
use Scalar::Util ();
use Unicode::Normalize;

use Moo;
use namespace::clean;
use Foswiki::Class qw(app);
extends qw(Foswiki::Object);
with qw(Foswiki::AppObject);

use constant HTTP_COMPLIANT => undef; # This is a generic class.

Expand Down
12 changes: 0 additions & 12 deletions core/lib/Foswiki/Exception.pm
Expand Up @@ -54,19 +54,11 @@ use Carp ();
use Scalar::Util ();

use Foswiki::Class;
use namespace::clean;
extends qw(Foswiki::Object);
with 'Throwable';

our $EXCEPTION_TRACE = 0;

BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
require locale;
import locale();
}
}

=begin TML
---++ ObjectAttribute file
Expand Down Expand Up @@ -509,11 +501,8 @@ Attributes:

package Foswiki::Exception::HTTPResponse;
use Foswiki::Class;
use namespace::clean;
extends qw(Foswiki::Exception);

our @_newParameters = qw(status reason response);

has status =>
( is => 'ro', lazy => 1, default => sub { $_[0]->response->status, }, );
has response => (
Expand Down Expand Up @@ -562,7 +551,6 @@ use CGI ();
use Assert;

use Foswiki::Class;
use namespace::clean;
extends qw(Foswiki::Exception::HTTPResponse);

has header => ( is => 'rw', default => '' );
Expand Down
3 changes: 2 additions & 1 deletion core/lib/Foswiki/Object.pm
Expand Up @@ -87,7 +87,7 @@ has __id => (
has __clone_heap =>
( is => 'rw', clearer => 1, lazy => 1, default => sub { {} }, );

around BUILDARGS {
around BUILDARGS => sub {
my $orig = shift;
my ( $class, @params ) = @_;

Expand All @@ -101,6 +101,7 @@ around BUILDARGS {

my $paramHash;

Carp::confess("Undefined \$class") unless defined $class;
no strict 'refs';
if ( defined *{ $class . '::_newParameters' }{ARRAY} ) {
my @newParameters = @{ $class . '::_newParameters' };
Expand Down
2 changes: 1 addition & 1 deletion core/tools/configure
Expand Up @@ -45,8 +45,8 @@ binmode( STDIN, ':crlf' );
use Assert;

use Foswiki;
use Foswiki::App;
use Foswiki::Configure::Query;
use Foswiki::App;

{

Expand Down

0 comments on commit e9fecba

Please sign in to comment.