From: monkeys paw on 9 Aug 2010 18:50 I have a new and execute method in my module. I want to propogate the args from new to execute, but i get a null variable, plz see below: package Cmtname; use Stnet::DBH; sub new { my ($class, %opts) = @_; my $self = \%opts; bless $self, $class; return $self; } sub execute { my ($self, %opts); use Data::Dumper;die 'DDDEBUG' . Dumper($self); # This is null } If i dump $self in "new" i get the correct args, in "execute" it is null, what am i missing? here is test script: #!/bin/perl use strict; use lib '/web/docs/secure/dev/'; #Just location specific use Cmtname; my $c = new Cmtname(state => 'US', cmt_abbr => 'h'); $c->execute();
From: Don Piven on 9 Aug 2010 19:40 On 08/09/2010 05:50 PM, monkeys paw wrote: > I have a new and execute method in my module. I want > to propogate the args from new to execute, but i get > a null variable, plz see below: > > [...] > > sub execute { > my ($self, %opts); > use Data::Dumper;die 'DDDEBUG' . Dumper($self); # This is null > } > > If i dump $self in "new" i get the correct args, in "execute" it > is null, what am i missing? execute() gets passed a reference to the object, followed by any passed arguments. However, you have to retrieve the object reference yourself (just as you'd have to do with regular arguments) which you don't do in your example above. Try: sub execute { my $self = shift ; # $self now has reference to object ... # and the args you passed to new() print $self->{state} ; # can be accessed in the usual way } Don
From: Ben Morrow on 9 Aug 2010 19:56 Quoth monkeys paw <monkey(a)joemoney.net>: > > my $c = new Cmtname(state => 'US', cmt_abbr => 'h'); This is better written my $c = Cmtname->new(state => 'US', ...); The form you have used is called 'dative method syntax' or 'indirect object syntax' and can cause hard-to-debug problems. Ben
From: sln on 10 Aug 2010 05:41 On Mon, 09 Aug 2010 18:50:50 -0400, monkeys paw <monkey(a)joemoney.net> wrote: >I have a new and execute method in my module. I want >to propogate the args from new to execute, but i get >a null variable, plz see below: > >package Cmtname; >use Stnet::DBH; >sub new { > my ($class, %opts) = @_; > my $self = \%opts; > bless $self, $class; > return $self; >} > >sub execute { > my ($self, %opts); >use Data::Dumper;die 'DDDEBUG' . Dumper($self); # This is null >} > >If i dump $self in "new" i get the correct args, in "execute" it >is null, what am i missing? > >here is test script: > >#!/bin/perl > >use strict; >use lib '/web/docs/secure/dev/'; #Just location specific > >use Cmtname; > >my $c = new Cmtname(state => 'US', cmt_abbr => 'h'); >$c->execute(); > Try this. -sln ---------- use strict;use warnings; my $self={state=>'GB'}; execute($self);sub execute { my ($self, %opts); use Data::Dumper;die 'DDDEBUG' . Dumper($_[0]); }
From: sln on 10 Aug 2010 06:12 On Tue, 10 Aug 2010 02:41:55 -0700, sln(a)netherlands.com wrote: >On Mon, 09 Aug 2010 18:50:50 -0400, monkeys paw <monkey(a)joemoney.net> wrote: > >>I have a new and execute method in my module. I want >>to propogate the args from new to execute, but i get >>a null variable, plz see below: >> >>package Cmtname; >>use Stnet::DBH; >>sub new { >> my ($class, %opts) = @_; >> my $self = \%opts; >> bless $self, $class; >> return $self; >>} >> >>sub execute { >> my ($self, %opts); >>use Data::Dumper;die 'DDDEBUG' . Dumper($self); # This is null >>} >> >>If i dump $self in "new" i get the correct args, in "execute" it >>is null, what am i missing? >> >>here is test script: >> >>#!/bin/perl >> >>use strict; >>use lib '/web/docs/secure/dev/'; #Just location specific >> >>use Cmtname; >> >>my $c = new Cmtname(state => 'US', cmt_abbr => 'h'); >>$c->execute(); >> > >Try this. >-sln >---------- >use strict;use warnings; >my $self={state=>'GB'}; >execute($self);sub execute { > my ($self, %opts); >use Data::Dumper;die 'DDDEBUG' . Dumper($_[0]); >} > This also works: -sln ----------------- use strict; use warnings; my $obj = Cmtname->new(state=>'US',cmt_abbr=>'h'); $obj->execute(); package Cmtname; sub new { bless {@_[1..$#_]}} sub execute { use Data::Dumper; die Dumper($_[0]); }
|
Pages: 1 Prev: FAQ 3.10 Is there an IDE or Windows Perl Editor? Next: Matrix Ops with Rational Output |