fglock / Perlito

"Perlito" Perl programming language compiler
http://fglock.github.io/Perlito/
Other
414 stars 47 forks source link

Perlito5::Javascript2::IO #32

Closed kurpitsa closed 8 years ago

kurpitsa commented 8 years ago

My test program is:

print "hello";

I compile:

perl perlito5.pl -Cjs ./test.pl > ./test.js

When i run test.js in chrome, I get the message

"Uncaught ReferenceError: write is not defined"

It works fine in NodeJS though. I am inexperienced with the perlito compiler, but I found out that at line 489 on Perlito5::Javascript2::IO

       write(p5str(List__[i]));

looks doubtful. Everything works if I replace this with console.log or document.write. If nodejs is used
this line is never executed (nodejs ./test.js).

fglock commented 8 years ago

The nodejs backend does have a built-in "print", which uses the "process.stdout", "process.stderr", and "fs" javascript modules:

https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/Javascript2/IO.pm#L32

However, the browser variant does not come with a native "print" function, because the actual "printing" action depends on your web page layout. So the keywords "print", "say", and "printf" will call a javascript "write" function, which you have to provide.

This is implemented in line 489, as you said:

https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/Javascript2/IO.pm#L486

Here is an example for a browser implementation that supports STDOUT and STDERR:

https://github.com/fglock/Perlito/blob/master/html/perlito5.html#L119

Do you think calling "document.write()" would be a better default action?

Note: the switch between "nodejs" and "browser" modes is at: https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/Javascript2/IO.pm#L28

Another note: if you use html/perlito5.html as a starting point, you can compile Perl to Javascript in the browser (it uses "perlito5.js" instead of "perlito5.pl"). The js file is created by the "make" command.

2015-11-02 21:22 GMT+01:00 kurpitsa notifications@github.com:

My test program is:

print "hello";

I compile:

perl perlito5.pl -Cjs ./test.pl > ./test.js

When i run test.js in chrome, I get the message

"Uncaught ReferenceError: write is not defined"

It works fine in NodeJS though. I am inexperienced with the perlito compiler, but I found out that at line 489 on Perlito5::Javascript2::IO

   write(p5str(List__[i]));

looks doubtful. Everything works if I replace this with console.log or document.write. If nodejs is used

this line is never executed (nodejs ./test.js).

— Reply to this email directly or view it on GitHub https://github.com/fglock/Perlito/issues/32.

yati-sagade commented 8 years ago

I think console.log() is fairly common. Maybe something like this would help: http://stackoverflow.com/a/14469096/687467

kurpitsa commented 8 years ago

I think the solution above reflects what the user might expect. Perhaps it could go for console log as described in the link unless the write function is already defined.

fglock commented 8 years ago

print() will try harder now:

https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/Javascript2/IO.pm#L486

2015-11-03 17:55 GMT+01:00 kurpitsa notifications@github.com:

I think the solution above reflects what the user might expect. Perhaps it could go for console log as described in the link unless the write function is already defined.

— Reply to this email directly or view it on GitHub https://github.com/fglock/Perlito/issues/32#issuecomment-153415628.