Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > use hash to name params, then stringify values in specific order

Reply
Thread Tools

use hash to name params, then stringify values in specific order

 
 
DJ Stunks
Guest
Posts: n/a
 
      01-20-2009
Hey all,

I have an API I need to call as a string of comma separated values. I
want to name those values in a hash so that it's easy to see what I'm
setting each value to. However, the values have to go into the API
call in a certain order. The goal is to make the code as clean and as
self-documenting as possible (ideally both the API call syntax and the
call values should be clearly apparent in the Perl code)

I can do it using a hash slice as follows, but this seems like a
pretty bad approach so I thought I'd shop it around and see what you
all think would be a better approach. Tie::IxHash maybe? (I've never
used it)

TIA,
-jp

#/usr/bin/perl

use strict;
use warnings;

#API syntax: my_API_call
(<this_parameter>,<that_parameter>,<extra_paramete r>)

my %parms = (
this_parameter => 'some value',
that_parameter => 'another value',
extra_parameter => 'a third value',
);

my @order = qw{ this_parameter that_parameter extra_parameter };

my $stringified_API_call = 'my_API_call(' . join(',', @parms
{ @order }) . ')';

print "call it: $stringified_API_call\n";

__END__
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      01-20-2009
DJ Stunks wrote:
> Hey all,
>
> I have an API I need to call as a string of comma separated values. I
> want to name those values in a hash so that it's easy to see what I'm
> setting each value to. However, the values have to go into the API
> call in a certain order. The goal is to make the code as clean and as
> self-documenting as possible (ideally both the API call syntax and the
> call values should be clearly apparent in the Perl code)
>
> I can do it using a hash slice as follows, but this seems like a
> pretty bad approach so I thought I'd shop it around and see what you
> all think would be a better approach. Tie::IxHash maybe? (I've never
> used it)
>
> TIA,
> -jp
>
> #/usr/bin/perl
>
> use strict;
> use warnings;
>
> #API syntax: my_API_call
> (<this_parameter>,<that_parameter>,<extra_paramete r>)
>
> my %parms = (
> this_parameter => 'some value',
> that_parameter => 'another value',
> extra_parameter => 'a third value',
> );
>
> my @order = qw{ this_parameter that_parameter extra_parameter };
>
> my $stringified_API_call = 'my_API_call(' . join(',', @parms
> { @order }) . ')';


If you're actually using $strigified_API_call, if there's a ',' in any
of your values, it won't work very well. Printing it like that
is fine, to possibly see what you're sending, but to actually
call 'my_API_call', pass the slice.

print 'call it: my_API_call(',
join(',', @parms{ @order } ),
')';
my_API_call( @parms{ @order } );

>
> print "call it: $stringified_API_call\n";
>
> __END__

 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      01-20-2009
On Tue, 20 Jan 2009 10:53:57 -0800 (PST), DJ Stunks <(E-Mail Removed)> wrote:

>Hey all,
>

[snip]
>The goal is to make the code as clean and as
>self-documenting as possible (ideally both the API call syntax and the
>call values should be clearly apparent in the Perl code)

[snip]

Is that really a goal? Are you some kind of a manager with some CS god complex?
Should a manager ever say this to me I would have him/her fired in 5 minutes!

sln
 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      01-21-2009
On Jan 20, 1:53 pm, DJ Stunks <(E-Mail Removed)> wrote:
> Hey all,
>
> I have an API I need to call as a string of comma separated values. I
> want to name those values in a hash so that it's easy to see what I'm
> setting each value to. However, the values have to go into the API
> call in a certain order. The goal is to make the code as clean and as
> self-documenting as possible (ideally both the API call syntax and the
> call values should be clearly apparent in the Perl code)
>
> I can do it using a hash slice as follows, but this seems like a
> pretty bad approach so I thought I'd shop it around and see what you
> all think would be a better approach. Tie::IxHash maybe? (I've never
> used it)
>
> TIA,
> -jp
>
> #/usr/bin/perl
>
> use strict;
> use warnings;
>
> #API syntax: my_API_call
> (<this_parameter>,<that_parameter>,<extra_paramete r>)
>
> my %parms = (
> this_parameter => 'some value',
> that_parameter => 'another value',
> extra_parameter => 'a third value',
> );
>
> my @order = qw{ this_parameter that_parameter extra_parameter };
>
> my $stringified_API_call = 'my_API_call(' . join(',', @parms
> { @order }) . ')';
>
> print "call it: $stringified_API_call\n";
>
> __END__


Well, I think responder #1 missed what I was going for and responder
#2 appears to be off his meds.

However, I tried Tie::IxHash and it works quite well for me. It's
immediately clear what the values are for the various API parameters
and it skips the necessity of the @order array and the associated hash
slice. The only concern is that maintainers must realize the order in
the hash is critical.

Code below for the curious.

Thanks,
Jake

#/usr/bin/perl

use strict;
use warnings;

use Tie::IxHash;
tie (my %parms, 'Tie::IxHash');

#API syntax: my_API_call
(<this_parameter>,<that_parameter>,<extra_paramete r>)

%parms = (
this_parameter => 'some value',
that_parameter => 'another value',
extra_parameter => 'a third value',
);

my $stringified_API_call = 'my_API_call(' . join(',', values
%parms) . ')';

print "call it: $stringified_API_call\n";

__END__
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#keys, Hash#values order question Ronald Fischer Ruby 0 08-23-2007 09:34 AM
Hash#values and Hash#keys order Alex Fenton Ruby 1 04-15-2006 05:45 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
Macro to stringify an enum Chris C Programming 3 06-07-2004 06:40 PM



Advertisments