0
$i = system(bc 110^151%14351);
print($i); 

Hey everyone, I am attempting to use a system call to use a calculator installed on my linux machine. The calculators name is GNU bc, for the basic calculator. Basically it is an easy way to calculate equations. I can easily run it from the terminal by just typing bc and then the function I want to equate, in this case 110 raised to 151 modded by 14351. The problem is, I don't know too much about perl and I keep getting this error Can't call method "bc" without a package or object reference when trying to write a perl script to automate it. How do I call a system call in Perl, or am I allowed to do it? Thanks

wheatfairies
  • 355
  • 2
  • 7
  • 19
  • The term "system call" usually refers to a call into the OS kernel, like `read`, `write`, or `ioctl`. A call to the `system` function, confusingly, is not a "system call". – Keith Thompson Feb 19 '14 at 00:07
  • 1
    Why use `system` and `bc` when you can just calculate that in Perl instead? You forgot to put quotes around your system call, and you should use backticks or `qx()` if you want to capture output. – TLP Feb 19 '14 at 00:45

1 Answers1

1

You're using both Perl's system() function and the bc command

You can get bc to do what you want from the command line like this:

$ echo '110^151%14351' | bc
7355

Perl's system function returns a number containing information about the termination status of the invoked command; you want the command's output instead.

To invoke this from Perl, this should work:

chomp($i = `echo '110^151%14351' | bc`)

The chomp is needed because the Perl backticks retain the trailing newline from the command's output.

But you can do this in Perl itself using the Math::BigInt package. Type

perldoc Math::BigInt

at your shell prompt for more information.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631