doulouli8686 2018-09-04 07:32
浏览 97
已采纳

perl中的“<<”,“ - ”和php中的实现是什么意思?

I am converting some Perl code to PHP.
However, I do not know much about Perl, so I have to code it with a rough meaning.

And, I do not understand what the below Perl code means...

What is the meaning of $req2->content(<<"POST_DATA") and --$boundary?
I've searched the Perl documentation, but it's too hard to find.

PHP code:

...
$boundary= 'Nobody-has-the-intention-to-erect-a-wall'; 

$req2 = curl_init($search_url); 
curl_setopt($req2, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($req2, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($req2, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($req2, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($req2, CURLOPT_COOKIE, $cookie); 
curl_setopt($req2, CURLOPT_HTTPHEADER, array( 
'Content-Type: multipart/form-data;boundary='.$boundary, 
'Content-Length: ' . strlen($data_string)) 
); 
$result= curl_exec($req2); 
...

Perl code:

...
my $boundary= 'Nobody-has-the-intention-to-erect-a-wall';
$req2->content_type('multipart/form-data;boundary='.$boundary);
$req2->header("Cookie"=>"access_token_cookie=$access_token_cookie; csrf_access_token=$csrf_access_token");

$req2->content(<<"POST_DATA"); #what means this?

--$boundary
Content-Disposition: form-data; name="num_result"
Content-Type: text/plain

$num_result
--$boundary
Content-Disposition: form-data; name="img"; filename="search.jpg"
Content-Type: image/jpeg

$image
--$boundary--
POST_DATA

my $res = $ua->request($req2);
...
  • 写回答

2条回答 默认 最新

  • dtlrp119999 2018-09-04 08:43
    关注

    Nothing very different from PHP actually.

    • << Heredoc, also present in PHP with slight differences:

      echo (<<<"POST_DATA"
      First line
      Second line
      POST_DATA
      );
      
    • -- Variable decrease, as in <?php $a=2; echo --$a;

    Note:

    Of course, inside a Heredoc -- is just text.


    Suggestion:

    If you don't fully understand the Perl, try to run it (it's not evil code).

    my $boundary = 'Nobody-has-the-intention-to-erect-a-wall';
    print(<<"POST_DATA");
    
    --$boundary
    Content-Disposition: form-data; name="num_result"
    Content-Type: text/plain
    
    $num_result
    --$boundary
    Content-Disposition: form-data; name="img"; filename="search.jpg"
    Content-Type: image/jpeg
    
    $image
    --$boundary--
    POST_DATA
    

    Will yield:

     
    --Nobody-has-the-intention-to-erect-a-wall
    Content-Disposition: form-data; name="num_result"
    Content-Type: text/plain
    
    
    --Nobody-has-the-intention-to-erect-a-wall
    Content-Disposition: form-data; name="img"; filename="search.jpg"
    Content-Type: image/jpeg
    
    
    --Nobody-has-the-intention-to-erect-a-wall--
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上