I ended up adding a function to my lib/(project)/Mason/Compilation.pm essentially containing
sub compileStringCurrent { my ($m, $string) = @_; my $compile = __PACKAGE__->new( interp => $m->interp(), path => $m->req()->path(), source_file => '/dev/null', ); $compile->{source} = $string; $compile->parse(); my $source = $compile->{current_method}->{body}; return($source); }; which lets me % eval Project::Mason::Compilation::compileStringCurrent( $some_string ); in a component. Not ideal but good enough for me at the moment. Maybe we can change Mason::Compilation to 1. Understand souce_file=>undef in BUILD (and make it ¬required) 2. Expose $self->{source} 3. Expose $self->{methods} and $self->{current_method} 4. Change that method hash to an object and expose ->{body} on it. Let me know if this sounds reasonable and I could possibly work up a patch. Allan On 9 January 2013 07:56, Allan Engelhardt <all...@cybaea.com> wrote: > *Slightly* better to do > > $compile->{source} = '<p>Hello world <% 1+1 %>.</p><& /footer.mi &>'; > $compile->parse(); > my $source = $compile->_output_methods() . '; main();'; > > – at least it avoids the regex…. > > Allan > > On 8 January 2013 22:10, Allan Engelhardt <all...@cybaea.com> wrote: > >> It is a total hack, of course, but for the common case something like the >> following appears to do the job. Caching and error handling (and much more) >> is left for the reader, but it should be easy enough to implement the bare >> bones in the standard Mason distribution (hint, hint) >> >> <%init> >> my $compile = CYBAEA::Mason::Compilation->new( # substitute your project >> name >> interp => $m->interp(), >> path => $m->req()->path(), # probably not quite right…? >> source_file => '/dev/null', # Trick - will need adjustment on msdos >> and others >> ); >> ## Assuming you have a /footer.mi component, of course…: >> $compile->{source} = '<p>Hello world <% 1+1 %>.</p><& /footer.mi &>'; >> my $source = $compile->compile(); >> ## If you understand how Mason works, you can probably create the whole >> ## package, but I don’t and I coudn’t figure it out. Just grab the main >> method. >> $source =~ s[\A .* method \s+ main \s* { (.*?) return \s* \; \s* >> }][$1]xms; >> </%init> >> >> <h1>Hi</h1> >> % eval($source); # Nasty, nasty hack. >> >> >> >> Improvements would be most welcome! >> >> Allan >> >> On 8 January 2013 17:27, Jonathan Swartz <swa...@pobox.com> wrote: >> >>> I haven't thought about how to do on-the-fly component compilation in >>> Mason 2 yet. It made the code a lot cleaner not to have to plan for it, and >>> I wasn't sure if anyone would need it. :) >>> >>> It should be possible since Class::MOP and Moose have utilities for >>> creating classes on the fly. Let me know if you come up with a good >>> solution. >>> >>> Jon >>> >>> On Jan 7, 2013, at 10:47 PM, Allan Engelhardt <all...@cybaea.com> wrote: >>> >>> Thanks Rolf, but I am trying to use Mason2. Anyone know of a solution >>> there? >>> >>> Allan >>> >>> On 7 January 2013 23:23, Rolf Schaufelberger <r...@plusw.de> wrote: >>> >>>> Yes, >>>> >>>> Am 07.01.2013 um 22:55 schrieb Allan Engelhardt <all...@cybaea.com>: >>>> >>>> Suppose I have a string (from a database or whatever) that I want to >>>> compile, run, and insert into the current component - is that possible? >>>> Something like (not tested): >>>> >>>> % # test.mc >>>> <%init> >>>> my $s = q{<p>I know 1+1=<% 1+1 %>.</p>}; # From external source >>>> </%init> >>>> <p>I know many facts</p> >>>> <% $m->magic( $s ) %> >>>> >>>> for some function magic( ... ) which should probably be called >>>> scomp_from_string or somesuch.... >>>> >>>> >>>> for HTML::Mason >>>> see the docs for HTML::Mason::Interp::make_component >>>> >>>> # Make an anonymous component >>>> my $anon_comp = >>>> eval { $interp->make_component >>>> ( comp_source => '<%perl>my $name = "World";</%perl>Hello >>>> <% $name %>!' ) }; >>>> die $@ if $@; >>>> >>>> $m->comp($anon_comp); >>>> >>>> >>>> >>>> Allan >>>> >>>> >>>> Mit freundlichen Grüßen >>>> Rolf Schaufelberger >>>> >>> >>> >>> >> >
------------------------------------------------------------------------------ Master Java SE, Java EE, Eclipse, Spring, Hibernate, JavaScript, jQuery and much more. Keep your Java skills current with LearnJavaNow - 200+ hours of step-by-step video tutorials by Java experts. SALE $49.99 this month only -- learn more at: http://p.sf.net/sfu/learnmore_122612
_______________________________________________ Mason-users mailing list Mason-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mason-users