Simple Output in Perl

Venny's Avatar, Join Date: Jan 2011
Go4Expert Member
Hi All,

I am a newbie to PERL, meas i have no knowledge of how the language works. I am trying to print the value of -3%2 . Could you tell me the syntax for the same and IDE and compiler that I should be using to run this program.


Thanks
0
Venny's Avatar, Join Date: Jan 2011
Go4Expert Member
40 views and no replies.. Wel I have a choice to do this program in Python too. If anyone PERL is not suitable for this.

Thanks
0
chorny's Avatar, Join Date: Jun 2010
Go4Expert Member
Code:
use 5.010;
say -3%2;
or just
Code:
print -3%2,"\n";

You didn't wrote form which OS you need IDE, and if you have other OS than Windows, you already have perl installed (unless you have some very exotic OS). Eclipse+EPIC IDE works on every major OS.
0
Venny's Avatar, Join Date: Jan 2011
Go4Expert Member
Hey thanks,

I have windows as my OS and have installed eclipse as well. So now tell me from here using the above syntax, I should be able to run the program right?

Here are my questions?

1. What are the header files used here and necessary libraries associated with that.
2. what will be the file type save as, like in c its .c or .cpp
3. how do I complile the code?

Its kind of learning from scratch. So if you can help further than its much appreciated.

Thanks
Venny
0
chorny's Avatar, Join Date: Jun 2010
Go4Expert Member
Save this as file with extension .pl, you don't need anything more. On Windows you need also install ActivePerl (or Strawberry, but it may be harder for beginner). To run program you just need to type "perl program_name.pl" on command prompt, it is compiled in memory.

If you want to use Eclipse, you need to install EPIC Eclipse extension.

P.S. Consider reading "Learning Perl" (no earlier than 4th edition) or perl.org/books/beginning-perl/ "Beginning Perl" - they both have good exercises.
0
Venny's Avatar, Join Date: Jan 2011
Go4Expert Member
well Thanks Corny..

I think its a really simple thing but gettin complicated out of no where. tell me if am correct in this program?

#!/usrl/bin/perl

print -3%2,"\n";



The above program simply prints the value of -3%2. isnt there any class or method that i need to defien here like main method in java which actually triggers evruthing..

The actuall output should be -1 but its showing 1 only. Can you analyse what went wrong here ?

Second ques? what is the abnormal behaviour we might see in printing the value of -3%2 in languages like Java, c/C++ or Perl ??

Thanks
0
chorny's Avatar, Join Date: Jun 2010
Go4Expert Member
There is no need for any additional class or method, with Perl (and most other dynamic languages) you don't need to write extra code for simple programs.

Read the definition here: perldoc.perl.org/perlop.html#Multiplicative-Operators
"Given integer operands $a and $b : If $b is positive, then $a % $b is $a minus the largest multiple of $b less than or equal to $a ." largest multiple of $b is -4, and (-3)-(-4) is 1. IMHO, 1 is more correct here than -1, but that depends on definition, Perl one is from math. AFAIK, in Python you would also get 1. In C you will get -1.
0
Venny's Avatar, Join Date: Jan 2011
Go4Expert Member
Ok i understand it more clearly now. in Perl and Python has maths libraries with correctly designed mod functions but languages like C/C++ and Java doesnot have. Can you tell me what strategy should I follow to correct my output in c/c++ and java ?

thanks
0
chorny's Avatar, Join Date: Jun 2010
Go4Expert Member
Implement it yourself .
Here is how I implemented it in Perl and tested it:

Code:
# perls '%' operator implemented manually and tested
# (c) Alexandr Ciornii
use POSIX(floor);
use Test::More qw/no_plan/;
my $list=[[-3,2],[8,3],[-8,3],[-8,-3],[-17,5]];
foreach my $pair (@$list) {
  my ($a,$b)=@$pair;
  is(my_rest($a,$b), $a % $b);
}

sub my_rest {
  my ($a,$b)=@_;
  my $c=floor($a/$b)*$b;
  print "$a,$b - $c\n";
  return $a-$c;
}