Mailing List Archive

Execute( sub =>...) and (object => ...)
I am attempting to #1 determine if a function is defined in a template
and if so #2 call it sending it parameters all from outside the
template.



I first thought I might just grep for [$ sub name $] and if found do:



my $html;

Embperl::Execute({inputfile => $template},

sub => 'name'

param =>
[$param1,$param2,$param3],

output => \$html});



But it doesn't seem that "param" feeds the subroutine (and it doesn't
feed the template either), so I can't figure out how to get parameters
to my function.



Then I thought I'd try to do:



my $object = Embperl::Execute({object => $template});

if($object->can('name')) {

$object->name($param1,$param2,$param3);

}



But for some reason, $object is always equal to zero. Does the template
need to have a package declaration? Not sure what I'm doing wrong.
Either of these ways would work, any help is appreciated.
RE: Execute( sub =>...) and (object => ...) [ In reply to ]
Ok so I figured out that the Execute( param => ... ) still goes into
@param not @_. Any idea why the ( object => $template ) isn't working?



From: Thompson, John [mailto:jthomp@midwestern.edu]
Sent: Tuesday, June 22, 2010 10:50 AM
To: embperl@perl.apache.org
Subject: Execute( sub =>...) and (object => ...)



I am attempting to #1 determine if a function is defined in a template
and if so #2 call it sending it parameters all from outside the
template.



I first thought I might just grep for [$ sub name $] and if found do:



my $html;

Embperl::Execute({inputfile => $template},

sub => 'name'

param =>
[$param1,$param2,$param3],

output => \$html});



But it doesn't seem that "param" feeds the subroutine (and it doesn't
feed the template either), so I can't figure out how to get parameters
to my function.



Then I thought I'd try to do:



my $object = Embperl::Execute({object => $template});

if($object->can('name')) {

$object->name($param1,$param2,$param3);

}



But for some reason, $object is always equal to zero. Does the template
need to have a package declaration? Not sure what I'm doing wrong.
Either of these ways would work, any help is appreciated.
RE: Execute( sub =>...) and (object => ...) [ In reply to ]
Hi John,

> -----Original Message-----
> From: Thompson, John [mailto:jthomp@midwestern.edu]
> Sent: Wednesday, 23 June 2010 5:31 AM

> Ok so I figured out that the Execute( param => ... ) still
> goes into @param not @_. Any idea why the ( object =>
> $template ) isn't working?

I use the following idiom quite successfully under Embperl::Object:

base.epl:

...
$lib = Execute({object => 'lib.epl'});
...

And then later in one of the main template files:

...
$lib->my_function( $arg1, $arg2 );
...

There is nothing special about lib.epl other than it only defines
subroutines. These are either perl subs in a [! !] block or [$ sub $]
constructs depending on whether they need to output HTML. There are
both, mixed interchangeably.

For example, lib.epl:

[.!
sub just_some_calculation {
my $self = shift;
my ($x, $y) = @_;
return $x * $y;
}
!]

[$ sub other_func $]
[-
my $self = shift;
my $arg1 = shift;
-]
[+ $arg1 +]
[$ endsub $]


There isn't much else to it. Hope that helped.

Cheers,

Andrew

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
RE: Execute( sub =>...) and (object => ...) [ In reply to ]
Hi,

 

as Andrew already wrote your object => code is quite ok, the only thing you have to take care is that sub’s should be defined inside [! !] blocks and not inside [- -] (because only the former get executed during the object => call)

 

Gerald

 

 

From: Thompson, John [mailto:jthomp@midwestern.edu]
Sent: Tuesday, June 22, 2010 9:31 PM
To: embperl@perl.apache.org
Subject: RE: Execute( sub =>...) and (object => ...)



 

Ok so I figured out that the Execute( param => ... ) still goes into @param not @_. Any idea why the ( object => $template ) isn’t working?

 

From: Thompson, John [mailto:jthomp@midwestern.edu]
Sent: Tuesday, June 22, 2010 10:50 AM
To: embperl@perl.apache.org
Subject: Execute( sub =>...) and (object => ...)



 

I am attempting to #1 determine if a function is defined in a template and if so #2 call it sending it parameters all from outside the template.

 

I first thought I might just grep for [$ sub name $] and if found do:

 

my $html;

Embperl::Execute({inputfile  => $template},

                                               sub    => ‘name’

                                           param  => [$param1,$param2,$param3],

                                           output => \$html});

 

But it doesn’t seem that “param” feeds the subroutine (and it doesn’t feed the template either), so I can’t figure out how to get parameters to my function.

 

Then I thought I’d try to do:

 

my $object = Embperl::Execute({object  => $template});

if($object->can(‘name’)) {

     $object->name($param1,$param2,$param3);

}

 

But for some reason, $object is always equal to zero. Does the template need to have a package declaration? Not sure what I’m doing wrong. Either of these ways would work, any help is appreciated.