Daniel
Daniel

Reputation: 12255

Is there a way to execute JavaScript in Perl?

I've worked some years now in Perl building Web scrapers, and given the problem that spam represents, and how scraping Web pages would turn out to be much more easier to those folks if, for example, Perl's LWP::UserAgent could handle its cup of JavaScript, I'm amazed no one has built a JS engine for it yet.

What am I missing here?

Thanks in advance. Regards.

PS: I'm not a spammer. Just curious.

Upvotes: 6

Views: 10855

Answers (5)

knb
knb

Reputation: 9383

You can try to install SpiderMonkey and -in your perl program- execute javascript in backticks, and capture the result, just like from any other unix command line tool. Spidermonkey has a command line option for that, similar to perl's -e command line option. The spidermonkey binary is called "js", thus:

/path/to/spidermonkey/bin/js -e "print(10);"

> 10

I think you can also install v8-shell as an alternative engine, but then you must install 'scons' first, which is available on unix-only.

./v8-shell -e 'print("10*10 = " + 10*10)'

Upvotes: 4

Ashley
Ashley

Reputation: 4335

There is also Win32::IE::Mechanize, Mozilla::Mechanize. But the previously mentioned WWW::Selenium is the most DWIW and well-supported if you have access to browsers and can run the Selenium server. Selenium is a Java critter that runs the browser interactions for you. It has IDEs for several browsers and can write code for you—by recording browser actions—in several languages including Perl or you can hand write it. It’s test-centric, where it excels, but there is no reason not to be use it for general automation/scraping.

Upvotes: 4

mscha
mscha

Reputation: 6840

TMTOWTDI. Another option is WWW::Scripter, with the Javascript or AJAX plugin.

Upvotes: 6

KooiInc
KooiInc

Reputation: 122986

Would you mean something like JavaScript::SpiderMonkey, a Perl interface to a JavaScript engine used by Mozilla?

Upvotes: 8

Zaid
Zaid

Reputation: 37156

WWW::Selenium?

Upvotes: 6

Related Questions