SPUG: Pasing hash references?

Adam Monsen adamm at wazamatta.com
Tue Oct 29 01:46:35 CST 2002


Jay,

The code you provided does not compile. Here are some free hints.

- subroutines start with the keyword 'sub' (see code I sent you earlier)
- please 'use strict' in the FORM package 
- indent your code (formatting may have been lost when the email was
  sent)
- pick a naming convention. 'get_info()' or 'getInfo()', but not both
- using the string 'undef' as a hash value might confuse other
  programmers. Don't do that.
- comments like 'Create new class' and 'Define new data' are frivolous
  and simply reduce readability.
- the comment 'Create new data file based on %client' is _wrong_. Remove
  it.
- it's a good idea to use a CPAN module to generate XML rather than
  write it by hand. Something like XML::Generator or XML::Simple would
  work.
- instead of '$client->FORM::get_Info' do '$client->get_Info'
- consider /not/ doing object orientation, it's only complicating the
  issue. However, if you really want to use object orientation, you do
  not need to pass the hash created in get_Info() to save2XMLfile()
  since the FORM object you create ($client) has this information. If
  this doesn't make sense, ditch object orientation.
- s/anchelada/enchilada/g
- see: perldocs 'perlboot' and 'perlstyle', for now.

I fixed your source code and cleaned it up quite a bit. The output looks
like this:

  <?xml version="1.0"?>
  <FORM>
    <FIRSTNAME>Jane</FIRSTNAME>
    <LASTNAME>Doe</LASTNAME>
    <SCHEDULE>2002</SCHEDULE>
    <CLIENT_ID>1234</CLIENT_ID>
  </FORM>

I would be happy to provide this source at the measly sum of US$6.95 for
services and time. The funds will be donated to The Perl Foundation to
further support of the Perl programming language.

-Adam

On 28-Oct-2002 01:26 -0800, Jay Scherrer wrote:
> 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;
> }

-- 
Adam Monsen

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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