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

D

DJ Stunks

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

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

J. Gleixner

DJ said:
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_parameter>)

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 } );
 
S

sln

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
 
D

DJ Stunks

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

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top