title: PHP stream_context_create() equivalent in perl
body: <p>I am following a tutorial <a href="https://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/" rel="nofollow">https://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/</a> to implement connectivity with apple APNS server. The tutorial provided a php code for this:</p>
  
  <pre><code>// Open the connection
  $apnsHost = 'gateway.sandbox.push.apple.com';
  $apnsPort = 2195;
  $apnsCert = 'apns-dev.pem';
  
  $streamContext = stream_context_create();
  stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
  
  $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
  
  // Send the payload
  $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
  fwrite($apns, $apnsMessage);
  
  // Close Resources
  socket_close($apns);
  fclose($apns);
  </code></pre>
  
  <p>I want to write this in Perl. What is the best equivalent of PHP's stream_context_create(). I tried something like the below, but didnt work.</p>
  
  <pre><code>use IO::Socket::SSL;
  
  my $client = IO::Socket::SSL-&gt;new(
          # where to connect
          PeerHost =&gt; "gateway.sandbox.push.apple.com",
          PeerPort =&gt; "2195",
  
          SSL_cert_file =&gt;'localcert.pem',
  
      ) or die "failed connect or ssl handshake: $!,$SSL_ERROR";
  </code></pre>