#!/usr/bin/perl # --------------------------------------------------------------------------- # Example function for reading the parameters to a CDS callout script from # STDIN, and parsing them into a Perl object's hashes. # # NOTE: this code taken from my own production script code ;) --TJ # --------------------------------------------------------------------------- use strict; # assuming this script _is_ the callout script, parse STDIN and write the # callout parameters to a file (printing to STDOUT doesn't work too well # as callouts don't really have an attached terminal) my %params = (); my $output_file = "/tmp/cds-callout-parameters-list.txt"; get_params(parameters => \%params); open(OUTPUT, "> $output_file") or die "unable to open $output_file: $!\n"; foreach my $key (keys %params) { print OUTPUT "Parameter: $key\tValue: $params{$key}\n"; } close(OUTPUT) or die "unable to close $output_file: $!\n"); # done! exit 0; # --------------------------------------------------------------------------- sub get_params { my %args = @_; my $param = $args{'parameters'}; my $line; my $element_index = 0; my $do_read_element = 0; # reading input from STDIN while ($line = <>) { # remove the newline characters... chomp $line; if ($line =~ /^Scalar/) { my ($datatype, $variable, @value) = split (/\s+/, $line); my $value = join(" ", @value); if ($do_read_element) { # read the parameter and value, a little deeper in the hash $param->{'parameters'}{'destinations'}[$element_index]{$variable} = $value; } else { # record the parameter and value in an internal hash $param->{'parameters'}{$variable} = $value; } next; } elsif ($line =~ /^BeginElement/) { # signal that we're reading element values... $do_read_element = 1; next; } elsif ($line =~ /^EndElement/) { # done reading that element... $do_read_element = 0; # don't forget to increment the element index $element_index++; next; } elsif ($line =~ /^EndParams/) { last; } else { # NOTE: what to do with unrecognized input?? } } return 1; } # ---------------------------------------------------------------------------