Help pls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hgdien
    New Member
    • Sep 2007
    • 3

    Help pls

    I am a complete newbie at Perl. I have just started getting from bash to Perl language. I am trying to do a simple perl scripting that takes three arguments: two integers and an operation (+, -, * or /) and outputs the computed value.
    Example:
    $ ./perl_calc.pl 12 -13 *
    -156
    $
    However, I don`t know how to turn that operation variable (+, -, * or /) back to an operation only, since when I put in $ARGV[0] $ARGV[1] $ARGV[1] they are all treated either as string or int.
    Pls help me.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Is this school work? Normally you would not eneter the math operators as arguments since they are treated as simple strings and have no functional purpose.

    Comment

    • hgdien
      New Member
      • Sep 2007
      • 3

      #3
      It belongs to the program of another course. I have just finish bash and wanted to learn some perl myself. However, looking for a simple solution through the webs is so difficult, since most of them only have difficult problems and tutorial texts, which couldn`t help me.
      Can you help me out, pls?

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        eval is the perl funtion that turns strings/text into code.

        perldoc: eval function

        But this is rarely something you would want to do in a real program. Allowing unvalidated user input to run as code is dangrous and extremely unwise.

        Comment

        • hgdien
          New Member
          • Sep 2007
          • 3

          #5
          Thank you very much for your help.
          I know these codes are not pratical use. I just try to do some small perl code first

          Comment

          Working...