P.N.Jayasinghe
P.N.Jayasinghe

Reputation: 47

perl extract values from a http request

I have a perl rest API. When http request receives to my program I want to extract values in above mentioned request.

I used following code to take a dump

warn "\n\n  request : " . MyCT::Util::dumper($self->resp);

Following result could find in the log file. How can I extract "standalone" from this result?

Tue Feb 18 05:20:26 2020] [warn] [21783] [MyCT] 

request : $VAR1 = bless( {
  'outputSent' => 0,
  'headersSent' => 0,
  'autoFlush' => 0,
  'req' => bless( do{\(my $o = 196025568)}, 'MyCT::Base::Request' ),
  '_cookies' => {
    'sessionKey' => '1762839:c480474dd4f4623035e8f35b445e1aad:c9920499157cf9c2a7972f773d08b972:standalone'
  },
  'contents' => [],
  'active' => 1
}, 'MyCT::Base::Response' );

Upvotes: 0

Views: 183

Answers (1)

ikegami
ikegami

Reputation: 385847

One normally doesn't access an object variable directly, instead using the provided accessors provided by the object's class.

Seeing as you didn't provide information about the class, we are left with only the fragile, error-prone alternative.

my $session_key = $self->resp->{_cookies}{sessionKey};

One you have the session key, it's just a question of splitting on : and getting the fourth field.

( split(/:/, $sesion_key) )[3]

Upvotes: 3

Related Questions