Get the perl string calculation result

后端 未结 2 1642
终归单人心
终归单人心 2021-01-19 18:04

If one string is expressed like below $str = \"5+2-1\"; I\'d like to get the calculation result from that string. How do I convert to scalar to compute this? Thanks.

2条回答
  •  孤独总比滥情好
    2021-01-19 18:53

    If you are sure the string does not contain any malicious code you can use eval to treat the content of it as perl code.

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $string = "5+2-1";
    
    print eval($string);
    #print 6    
    

提交回复
热议问题