SPUG: Pasing hash references?

Jay Scherrer jay at Scherrer.com
Mon Oct 28 03:26:36 CST 2002


Heres a short sample,
Is there a way of passing the establised hash content back to a subroutine? So 
far with the simplified setting below, I can pass values to a new class hash 
as in "get_Info()" and print them calling the keys of the new hash. but how 
do I get the whole anchelada without redefining the whole hash again?
Below is a very brief snapshot of what I'm working with. 
Thanks,
Jay

The script:
testForm.pl
#!/usr/local/bin/perl 
use strict;
use FORM;
my ($client, $clienId, $schedule, $firstname, $lastname);
## Create new class ##
$client = new FORM;
## Define new data ##
$clientId = (1234);
$schedule = ("2002");
$firstname = ("Jane");
$lastname = ("Doe");
## Input new data into $client hash ##
$client->FORM::get_Info($clientId, $schedule, $firstname, $lastname);
## print $client data to  xml file ##
$client->FORM::save2XMLfile;

The package:

package FORM;
## Some routines in package form ##

my %FORM = (
CLIENT_ID => 'undef',
SCHEDULE => 'undef',
FIRSTNAME => 'undef',
LASTNAME => 'undef'
);

new {
##Create new data file based on %client ##
my $that = shift;
my $class = ref($that) || $that;
my $self = {%FORM};
bless $self, $class;
return $self;
}

save2XMLfile {
my $form =@_;
my $outFile = "clientName.xml";  # prints new class to file#
my ($clientTag, $value);
open(OUTFILE, ">$outfile") or die "Can't $!\n";
print OUTFILE ("<?xml version=\"1.0\"?>\n");
print OUTFILE ("<FORM>\n"); 
foreach $clientTag (keys %form) {
$value = $form{$clientTag};
print OUTFILE ("<$clientTag>$value</$clientTag>\n");
}
print OUTFILE ("</FORM>\n");
close OUTFILE;
}

get_Info {
$form->{CLIENT_ID} = $clientId if defined $clientId;
$form->{SCHEDULE} = $schedule if defined $schedule;
$form->{FIRSTNAME} = $firstname if defined $firstname;
$form->{LASTNAME} = $lastname if defined $lastname;
return %form;
}


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list