NAME

JavaScript::Code - A JavaScript Code Framework

SYNOPSIS

#!/usr/bin/perl

use strict;
use warnings;
use JavaScript::Code;
use JavaScript::Code::Variable;

# create the code object
my $code = JavaScript::Code->new();

# create a code block
my $block = JavaScript::Code::Block->new();

# create some variables
my $var1 = JavaScript::Code::Variable->new()->name('a');
my $var2 = JavaScript::Code::Variable->new()->name('b')->value(42);
my $var3 = JavaScript::Code::Variable->new()->name('c')->value(23);
my $var4 = JavaScript::Code::Variable->new()->name('x')->declared( 1 ); # could have been declared in a other script

# add some of them to the code block
$block->add( $var2 )->add( $var3 );

# create some numbers
my $x1 = JavaScript::Code::Number->new()->value( 10 );
my $x2 = JavaScript::Code::Number->new()->value( 20 );

# build expressions
my $c1 = $var2 - ($x1 + $x2 + $x1->value(30)) * $x2->value(40);
my $c2 = $c1 * $var3;

# assign a expression to a variable and add it to your code block
$block->add( JavaScript::Code::Variable->new()->name('d')->value( $c2 ) );

# add more stuff to your code block
$block->add( JavaScript::Code::Block->new()->add( $var1->value("Bar!") ) );
$block->add( $var1->clone->value("Foo!") ); # clone 'a' and give it a new value

# add your block and other stuff to the code object
$code->add( $var4->value( 4711 ) )->add( $var1->value("Perl!") );
$code->add( $block );
$code->add( $var2->value(21) ); # 'b' with value 21 (note the different scope)

# output to be embedded in your html code
print $code->output_for_html;

DESCRIPTION

Create javascript-code!

METHODS

$self->add( $element )

Adds a new element.

$self->elements( )

Returns a ref-array of all added elements.

$self->output( )

Returns the javascript-code.

$self->output_for_html( < \%args > )

Returns the javascript-code that can be directly embedded into html-code.

Optimal \%args: Key-value pairs that reprensent attributes that will be associated to the script tag.

print $code->output_for_html( { language => undef } );

SEE ALSO

JavaScript::MochiKit

http://www.perl-community.de

AUTHOR

Sascha Kiefer, esskar@cpan.org

LICENSE

This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.