Hi-
Thanks for the quick reply. I didn't give you a full example because my test is contained in a couple of classes and it seemed to long to include, but here it is:
########
Foo/Y.pm
package Foo::Y;
require v5.8.0;
use strict;
use diagnostics;
use Data::Dumper;
use lib qw(/home/rledo/perllib);
require Exporter;
our $VERSION = 1.00;
our @ISA = qw(Exporter);
# symbols to export by request
our @EXPORT_OK = qw(pre);
# symbols to export by default
our @EXPORT = qw();
our %EXPORT_TAGS = ( );
sub pre {
print "******\n";
print "pre\n";
print Dumper(\@_);
# wrapped sub won't run if $_[-1] is assigned
#$_[-1] = 'assigned';
@_ = ($_[0], 'em', splice @_, 1);
}
1;
############
Foo/X.pm (this has the wrappers):
package Foo::X;
require v5.8.0;
use lib qw(/home/rledo/perllib);
#use parent qw(Foo::Y);
use Foo::Y qw(pre);
use strict;
use diagnostics;
use Data::Dumper;
use Hook::LexWrap;
our $pvar = 'hello world';
my $lvar = 'goodbye cruel world';
sub t();
wrap *t, pre => \⪯
sub new {
my ($this, $args) = @_;
my $class = ref($this) || $this;
my $self = {
dbh => 'handle'
};
bless $self, $class;
return $self;
}
sub t {
# my $self = shift;
print "t(): hello world\n";
print Dumper(\@_);
return;
}
1;
########
and the test script:
use strict;
use diagnostics;
use Data::Dumper;
use lib qw(/home/rledo/perllib);
use Foo::X;
my $x = Foo::X->new();
$x->t();
$x->t('arg1', 'arg2'); <-- I expect to see 'em' from the wrapper method prepended to these
Show quoted text________________________________
From: Alexandr Ciornii via RT <bug-Hook-LexWrap@rt.cpan.org>
To: rledousa@yahoo.com
Sent: Monday, May 16, 2011 4:02 PM
Subject: [rt.cpan.org #68251] can't modify @_ when using pre wrapper
<URL:
https://rt.cpan.org/Ticket/Display.html?id=68251 >
On Mon May 16 18:55:07 2011, rledousa wrote:
> In reading the docs on cpan, it appears that I can alter @_ of the
> wrapped sub in a pre wrapper.
>
> If I try to change an arg directly, I get a "can't modify constant item"
> error:
You didn't provide full example. For example, this code works correctly:
use Hook::LexWrap;
use Data::Dumper;
sub pre {
$_[1] = 'foo';
}
sub t {
print Dumper(\@_);
}
wrap *t, pre => \⪯
@args = (4,5,6);
t(@args);
If you call t with constant second argument, it means that second
argument is an alias to a constant, so you can't modify it.
--
Alexandr Ciornii,
http://chorny.net