Mojolicious - still blocking - how to?

103 views
Skip to first unread message

Statue

unread,
Aug 16, 2013, 1:34:50 PM8/16/13
to mojol...@googlegroups.com
Hello Ya'All:
 
I admit that I'm kind of a newbie to Mojolicious. I write code that is a hybrid of HTML4, CSS2, and PERL/CGI, but mainly focus on PERL. From what I can see on Mojolicious, you can't manipulate the html code with PERL in the middle of the code, because it consigns the web page to a value (which is blocking), and you can't alter items outside of the page. I can try to make it work this way, but it's not what I've done in the past. Consider this code fragment, and someone let me know how something like this would be done with Mojolicious:
 
my $webpage = 1;
print "content type=text/css
   <html>" ;
if ( $webpage == 1 ) {
   print "<head>
         <style type='css'>
             <p.jack width='100%'>
         </style>
      </head>" ;
}
else {
   print "<head></head>" ;
}
print "<body>" ;
my $value = 5 ;
print "   <img src='images/image" . $value . "'>
      <p style=jack>hello</p><br>
   </body>" ;
 
Not sure if there's any mistakes here, but you get the idea...How would I come close to doing this with Mojo? In my case, I use a single CGI script, and use it to pass parameters to itself to control what webpage it shows. I only want to do the processing which is applicable to that page. With Mojo, because it is blocking, it appears you have to calculate everything that is going to be on the page, and decide which page to display in advance, then display it all at once, rather than performing the calculations while the page is displaying. There is a difference, as I am using real real-time code, and the amount of calculations which is done directly affects how many things can be displayed - the fewer of each, the better.
 
LRG
 

Gabriel Vieira

unread,
Aug 16, 2013, 1:44:24 PM8/16/13
to mojol...@googlegroups.com
You can do this inside the tamplates.

-----------8<-----------
#!/usr/bin/perl

use Mojolicious::Lite;

get '/' => 'index';

app->start;

__DATA__
@@ index.html.ep
%
% my $webpage = 1;
%
<html>
%
% if ( $webpage == 1 ) {
<head>
<style type='css'>
<p.jack width='100%'>
</style>
</head>
%
% }
% else {
%
<head></head>
%
% }
%
<body>
%
% my $value = 5 ;
<img src='images/image<%= $value %>'>
<p style=jack>hello</p><br>
</body>
----------->8-----------

--
Gabriel Vieira

Lance Gropper

unread,
Aug 16, 2013, 1:47:24 PM8/16/13
to mojol...@googlegroups.com
Many thanks Gabriel - I will give it a try. It's not 100% what I was looking for, but is pretty close. I was hoping to do it above the __DATA__ section...



--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/1nEI90NiWj4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.



--
This e-mail and any attachments are intended only for use by the addressee(s) named herein and may contain confidential information. If you are not the intended recipient of this e-mail, you are hereby notified any dissemination, distribution or copying of this email and any attachments is strictly prohibited. If you receive this email in error, please immediately notify the sender by return email and permanently delete the original, any copy and any printout thereof. The integrity and security of e-mail cannot be guaranteed. If this email has been forwarded, it may have been altered, invalidating the content.

Gabriel Vieira

unread,
Aug 16, 2013, 1:56:01 PM8/16/13
to mojol...@googlegroups.com
You should always try to deal with HTML inside templates.

But you can do the logical part in the code itself, setting a stash
value and checking this value in the template.

I also suggest you to take a look at % include.
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mojolicious...@googlegroups.com.
> To post to this group, send email to mojol...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Gabriel Vieira

Lance Gropper

unread,
Aug 16, 2013, 1:57:43 PM8/16/13
to mojol...@googlegroups.com
Thanks again.

Lance Gropper

unread,
Aug 16, 2013, 2:59:12 PM8/16/13
to mojol...@googlegroups.com
Hello Gabriel:
 
Got another question - what about images? I tried <img src="photo.png">, and <img src="I/photo.png"> - it can't seem to find the file.
 
Lance

Gabriel Vieira

unread,
Aug 16, 2013, 3:00:52 PM8/16/13
to mojol...@googlegroups.com
You need to put them on public/ or pubic/I/ folder. Are they there?

On Fri, Aug 16, 2013 at 3:59 PM, Lance Gropper

Lance Gropper

unread,
Aug 16, 2013, 3:02:30 PM8/16/13
to mojol...@googlegroups.com
There in the same folder as the program that's displaying them, and I tried the I folder in that folder. I'll try public and public/I...

Lance Gropper

unread,
Aug 16, 2013, 3:04:30 PM8/16/13
to mojol...@googlegroups.com
Worked in the public folder within the program folder - I've got to style it, though. the image is something like 100x100 resolution, but in the browser, it's showing up more like 1000x1000...

Lance Gropper

unread,
Aug 19, 2013, 10:45:50 AM8/19/13
to mojol...@googlegroups.com
Hello Gabriel:
 
I have another question: With regards to HTML forms - how to I call a program running under Mojo, and how do I read the passed parameters?
 
Lance

Gabriel Vieira

unread,
Aug 19, 2013, 10:49:39 AM8/19/13
to mojol...@googlegroups.com
Hi,

what you mean by "call a program running under Mojo"?

About parameters, you should take a look at
http://mojolicio.us/perldoc/Mojolicious/Lite

The screencasts are really helpful too: http://mojocasts.com/

----
$self->param('input_name');
----

Regards,

On Mon, Aug 19, 2013 at 11:45 AM, Lance Gropper

Lance Gropper

unread,
Aug 19, 2013, 11:05:54 AM8/19/13
to mojol...@googlegroups.com
With Mojo, you are running a perl script, and in it, it has the html file. When you do an HTML form, it sends values to the program that it calls - i.e. Outside of Mojo, an HTML file, called index.html, could have a form on it which sends values to a program called "action.cgi." If I had the index.html in a perl script running under Mojo, how do I pass values from one "virtual" html file to another using forms?
 
For example, in HTML, I could have:
 
<form action="action.cgi">
   <input type="checkbox" name="test" value="0">
   <input type="submit" name="submit" value="send">
</form>
 
Then someone can click the checkbox, and hit submit, and the values of "test" and "submit" will be sent to action.cgi.
 
Then on the receiving end (in PERL):
 
use CGI qw(:standard);
my $test = param( 'test' ) ;
my $submit = param( 'submit ) ;
 
Lance

Lance Gropper

unread,
Aug 19, 2013, 11:07:48 AM8/19/13
to mojol...@googlegroups.com
Also, related to this: I notice that variables outside of Mojo aren't recognize inside. For example, if I did:
 
my $bob = 1;
 
then in the __DATA__section
 
% print $bob ;
 
It will say something like $bob is not defined.
 
Lance

Gabriel Vieira

unread,
Aug 19, 2013, 11:26:41 AM8/19/13
to mojol...@googlegroups.com
In __DATA__ section the variables are set from the values you set in stash.

$self->stash( bob => 1 );

and then:

%print $bob;

On Mon, Aug 19, 2013 at 12:07 PM, Lance Gropper

Gabriel Vieira

unread,
Aug 19, 2013, 11:27:27 AM8/19/13
to mojol...@googlegroups.com
I think you just answered your question, didn't you?

On Mon, Aug 19, 2013 at 12:05 PM, Lance Gropper

sri

unread,
Aug 19, 2013, 11:29:44 AM8/19/13
to mojol...@googlegroups.com
About parameters, you should take a look at
http://mojolicio.us/perldoc/Mojolicious/Lite

The screencasts are really helpful too: http://mojocasts.com/

This is excellent advice. Don't rush things, take some time to watch the screencasts and study the documentation.

--
sebastian 

Lance Gropper

unread,
Aug 19, 2013, 11:45:23 AM8/19/13
to mojol...@googlegroups.com
Awesome...

Lance Gropper

unread,
Aug 19, 2013, 11:46:48 AM8/19/13
to mojol...@googlegroups.com
Awesome on the other one as well...

Lance Gropper

unread,
Oct 16, 2013, 1:43:04 PM10/16/13
to mojol...@googlegroups.com
Hello Gabriel:
 
I have another question: I'm trying to alter CSS code in mojo with in-line substitution. This, for example works:
 
left: 80%
 
This doesn't for some reason (and $model is set to 80):
 
left: <%= $model %>%
 
What did I do wrong?
 
Lance

Stefan Adams

unread,
Oct 16, 2013, 1:45:50 PM10/16/13
to mojolicious
On Wed, Oct 16, 2013 at 12:43 PM, Lance Gropper <streamsc...@gmail.com> wrote:
Hello Gabriel:
 
I have another question: I'm trying to alter CSS code in mojo with in-line substitution. This, for example works:
 
left: 80%
 
This doesn't for some reason (and $model is set to 80):
 
left: <%= $model %>%
 
What did I do wrong?

Look at the source...  Is there a space between 80 and %   ?

Lance Gropper

unread,
Oct 16, 2013, 1:47:53 PM10/16/13
to mojol...@googlegroups.com
There's no space between the 80 and the %, and I tried it with <%= $model %> % (with space) - no difference.


--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/1nEI90NiWj4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.

Lance Gropper

unread,
Oct 16, 2013, 1:48:23 PM10/16/13
to mojol...@googlegroups.com
When I do it, I don't get any errors, it's just as if the value is evaluating to 0.

Stefan Adams

unread,
Oct 16, 2013, 1:50:11 PM10/16/13
to mojolicious
On Wed, Oct 16, 2013 at 12:47 PM, Lance Gropper <streamsc...@gmail.com> wrote:
There's no space between the 80 and the %, and I tried it with <%= $model %> % (with space) - no difference.

There should be no space, to my knowledge.  So that's bizarre that it isn't working.  I was thinking maybe there was a space and that was keeping it from working.

Lance Gropper

unread,
Oct 16, 2013, 1:53:24 PM10/16/13
to mojol...@googlegroups.com
Works fine in the HTML code, just doesn't seem to work in the CSS code. Also, I tried a different approach which didn't work for some reason. I tried putting all of the PERL code outside of the __DATA__, then using it to generate the HTML, putting it all into a single string and putting that into the data statement - that also didn't work, but it was too complicated to figure out why. The reason I was doing that is I wanted to make my code run under Mojo, as well as Webmin.


--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/1nEI90NiWj4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.

Stefan Adams

unread,
Oct 16, 2013, 1:54:01 PM10/16/13
to mojolicious
On Wed, Oct 16, 2013 at 12:48 PM, Lance Gropper <streamsc...@gmail.com> wrote:
When I do it, I don't get any errors, it's just as if the value is evaluating to 0.

But you verified that the Mojo template source of

left: <%= $model %>%

Expands to:

left: 80%

In your Browser's HTML View Source of the CSS?

I don't see how that's any different than having in your Mojo template source

left: 80%

Expanding to:

left: 80%

In your Browser's HTML View Source of the CSS.

But, that's the point of your question, I guess...  :)

 
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.

Lance Gropper

unread,
Oct 16, 2013, 1:56:25 PM10/16/13
to mojol...@googlegroups.com
I didn't verify it by viewing source - I will try that...

Lance Gropper

unread,
Oct 16, 2013, 1:57:34 PM10/16/13
to mojol...@googlegroups.com
When I view the source, nothing appears before the %.

Александр Грошев

unread,
Oct 16, 2013, 1:58:17 PM10/16/13
to mojol...@googlegroups.com

Try

left: <%= $model =%>%

16.10.2013 21:53 пользователь "Lance Gropper" <streamsc...@gmail.com> написал:
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.

Lance Gropper

unread,
Oct 16, 2013, 1:59:10 PM10/16/13
to mojol...@googlegroups.com
Hello Alexander:
 
No difference...
 
Lance
 

Lance Gropper

unread,
Oct 16, 2013, 2:01:19 PM10/16/13
to mojol...@googlegroups.com
I tried converting it to a string - also made no difference...

Lance Gropper

unread,
Oct 16, 2013, 2:02:03 PM10/16/13
to mojol...@googlegroups.com
Wait - I think I found the problem...Still checking...

Lance Gropper

unread,
Oct 16, 2013, 2:08:48 PM10/16/13
to mojol...@googlegroups.com
Problem was my bad - the original PERL variable assignment was in an if statement that wasn't evaluating correctly for some other reason (value that probably needed to be chopped). Thanks again guys.

Lance Gropper

unread,
Oct 22, 2013, 11:16:28 AM10/22/13
to mojol...@googlegroups.com
Hello Gabriel:

Got a different type of question: My app is running in real-time - this causes a lot of morbo info and debug messages - is there a way to disable the messages?

Lance

Allan Cochrane

unread,
Oct 22, 2013, 2:08:28 PM10/22/13
to mojol...@googlegroups.com
Hi,

the environment variable MOJO_LOG_LEVEL might help here, it can control the logger level. Alternatively make a directory called 'log' from where you start morbo and it *ought* to write to a development.log file in that directory.

allan

Allan Cochrane

unread,
Oct 22, 2013, 2:10:22 PM10/22/13
to mojol...@googlegroups.com

sri

unread,
Oct 22, 2013, 2:27:59 PM10/22/13
to mojol...@googlegroups.com

Got a different type of question: My app is running in real-time - this causes a lot of morbo info and debug messages - is there a way to disable the messages?

Please open new threads when changing the topic, then it will be easier for others that may have the same question to find the answer as well. If this happens to be a conversation between you two, please take it off list.

--
sebastian

Lance Gropper

unread,
Oct 23, 2013, 11:08:09 AM10/23/13
to mojol...@googlegroups.com
Hello Sri:
 
I did that and received no response.
 
Lance


--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/1nEI90NiWj4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.

Goke Aruna

unread,
Oct 23, 2013, 12:11:51 PM10/23/13
to mojol...@googlegroups.com
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages