Chandra

 view release on metacpan or  search on metacpan

examples/bind_example.pl  view on Meta::CPAN

#!/usr/bin/env perl
#
# Example: Binding Perl functions to JavaScript
#
# This demonstrates how to make Perl functions callable from JavaScript
# using the bind() method and the window.chandra.invoke() API.
#

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../blib/lib", "$FindBin::Bin/../blib/arch";
use Chandra;

print "Starting Chandra bind example...\n";

# HTML content with JavaScript that calls our Perl functions
my $html = <<'HTML';
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, sans-serif;
            padding: 20px;
            background: #f5f5f5;
        }
        h1 { color: #333; }
        .card {
            background: white;
            border-radius: 8px;
            padding: 15px;
            margin: 10px 0;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        button {
            background: rgb(76, 175, 80);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover { background: rgb(69, 160, 73); }
        input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin: 5px;
        }
        #result {
            margin-top: 10px;
            padding: 10px;
            background: #e8f5e9;
            border-radius: 4px;
            min-height: 20px;
        }
    </style>
</head>
<body>
    <h1>Chandra Bind Example</h1>
    
    <div class="card">
        <h3>Greet</h3>
        <input type="text" id="name" placeholder="Enter your name" value="World">
        <button onclick="callGreet()">Call greet()</button>
    </div>
    
    <div class="card">
        <h3>Add Numbers</h3>
        <input type="number" id="num1" value="10" style="width:60px">
        +
        <input type="number" id="num2" value="20" style="width:60px">
        <button onclick="callAdd()">Call add()</button>
    </div>
    
    <div class="card">
        <h3>Get Time</h3>
        <button onclick="callGetTime()">Call get_time()</button>
    </div>
    
    <div class="card">
        <h3>Reverse String</h3>
        <input type="text" id="str" placeholder="Enter text" value="Hello Perl!">
        <button onclick="callReverse()">Call reverse()</button>
    </div>
    
    <div class="card">
        <h3>Result</h3>
        <div id="result">Click a button above to call a Perl function...</div>
    </div>

    <script>
    BRIDGE_JS
    </script>
    <script>
        function showResult(text) {
            document.getElementById('result').textContent = text;
        }
        
        async function callGreet() {



( run in 1.101 second using v1.01-cache-2.11-cpan-140bd7fdf52 )