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

Discussion in 'Perl Misc' started by DJ Stunks, Jan 20, 2009.

  1. DJ Stunks

    DJ Stunks Guest

    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__
    DJ Stunks, Jan 20, 2009
    #1
    1. Advertising

  2. DJ Stunks

    J. Gleixner Guest

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

    >
    > print "call it: $stringified_API_call\n";
    >
    > __END__
    J. Gleixner, Jan 20, 2009
    #2
    1. Advertising

  3. DJ Stunks

    Guest

    On Tue, 20 Jan 2009 10:53:57 -0800 (PST), DJ Stunks <> 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
    , Jan 20, 2009
    #3
  4. DJ Stunks

    DJ Stunks Guest

    On Jan 20, 1:53 pm, 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_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__
    DJ Stunks, Jan 21, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    276
  2. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  3. Barry
    Replies:
    9
    Views:
    444
    Ara.T.Howard
    Sep 15, 2005
  4. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    136
    George Ogata
    Apr 15, 2006
  5. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    154
    Ronald Fischer
    Aug 23, 2007
Loading...

Share This Page