#! /usr/local/bin/ruby

## Perl module sample
## 1998 by yoshidam
##

require 'perl'

## Ruby handlers
def start(name, attr)
  print "(#{name}\n"
end

def end(name)
  print ")#{ name}\n"
end

def char(data)
  data.gsub!("\n", '\n')
  print "-#{data}\n"
end

## create Perl object
perl = Perl.new("-MRuby", "-MXML::Parser", "-e", "0")

## create Perl function objects to call Ruby handlers
start_cv = perl.eval(%q!
  sub { my ($self, $name, %attr) = @_; Ruby::call("start", $name, {%attr}); }
!)

end_cv = perl.eval(%q!
  sub { my ($self, $name) = @_; Ruby::call("end", $name); }
!)

char_cv = perl.eval(%q!
  sub { my ($self, $data) = @_;  Ruby::call("char", $data); }
!)

## create XML::Parser object
xml = perl.send('XML::Parser', 'new')
## set Perl function objects for event handlers
xml.setHandlers('Start', start_cv,
                'End', end_cv,
                'Char', char_cv)
## parse
if ARGV.length > 0
  xml.parsefile(ARGV[0])
else
  xml.parse($<.read)
end
