By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Perl Noob Question #2

P: 5
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. print "This program will convert Decimal to Binary \n";
  4. print "Please pick your Decimal Number \n";
  5. # Get the number from the command line or use default.
  6. my $number = shift || <STDIN>;
  7. printf "Binary Number ---- > %b\n", $number;
  8.  
With the code above. I know it works. I am wonder how everything works.

What is "shift ||"?
What is printf.

Let me throw out some guesses.

With "printf "Binary Number ---- > %b\n", $number;" that is converting the $number into Binary because of the %b. Is this technical an array? print f seems like print with formula?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. print "Do you want a hex or octal \n";
  4. my $hoo = <STDIN>;
  5. chomp($hoo);
  6. if ($hoo =~ /^hex$/) {
  7. print "Please Type a Hex Number \n";
  8. my $A = <STDIN>;
  9. chomp($A);
  10. print "\n";
  11. print hex("$A")," <--- That is your Hex Number \n";
  12. }
  13. else {
  14. print "Please Type a Octal Number\n";
  15. my $AA = <STDIN>;
  16. chomp($AA);
  17. print "\n";
  18. print oct("$AA")," <--- That is your Octal Number \n";
  19. }
  20.  
The only question I have about this code is "if ($hoo =~ /^hex$/)" What does the /^ $/ mean?

Thank you all that read this post.
Feb 4 '08 #1
Share this Question
Share on Google+
6 Replies


nithinpes
Expert 100+
P: 410
Hi,
printf is used for getting formatted output. In this case you are printing data in binary format. Similarly,
Expand|Select|Wrap|Line Numbers
  1. printf "%.2f", $value;
  2.  
will print the floating-point number truncated to two digits after decimal point. It is similar to printf in C.

In the regular expression,
^ means begining of line
$ means end of line
The pattern /^hex$/ matches true only if user enters hex. The word hex followed by a space will return false.
Feb 4 '08 #2

nithinpes
Expert 100+
P: 410
I missed out one question.
The line:
Expand|Select|Wrap|Line Numbers
  1. my $number = shift || <STDIN>;
  2.  
will shift the first element from the argument array, if you are passing the value as an argument while executing the script, else it will wait for user input through command-line/terminal.
'shift' function is used to remove & return first element of an array.
Feb 4 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. print "This program will convert Decimal to Binary \n";
  4. print "Please pick your Decimal Number \n";
  5. # Get the number from the command line or use default.
  6. my $number = shift || <STDIN>;
  7. printf "Binary Number ---- > %b\n", $number;
  8.  
With the code above. I know it works. I am wonder how everything works.

What is "shift ||"?
What is printf.

Let me throw out some guesses.

With "printf "Binary Number ---- > %b\n", $number;" that is converting the $number into Binary because of the %b. Is this technical an array? print f seems like print with formula?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. print "Do you want a hex or octal \n";
  4. my $hoo = <STDIN>;
  5. chomp($hoo);
  6. if ($hoo =~ /^hex$/) {
  7. print "Please Type a Hex Number \n";
  8. my $A = <STDIN>;
  9. chomp($A);
  10. print "\n";
  11. print hex("$A")," <--- That is your Hex Number \n";
  12. }
  13. else {
  14. print "Please Type a Octal Number\n";
  15. my $AA = <STDIN>;
  16. chomp($AA);
  17. print "\n";
  18. print oct("$AA")," <--- That is your Octal Number \n";
  19. }
  20.  
The only question I have about this code is "if ($hoo =~ /^hex$/)" What does the /^ $/ mean?

Thank you all that read this post.
To answer your first question, what the "||" does is specify an either or condition. In other words "shift off the next value from the @ARGV array (which holds the values fed as options to the script), OR, if it is empty, then prompt the user for input".

As for printf, it is basically "print formatted". To get a better idea of how it all works, you can read the printf page on perldoc.perl.org, or better yet, look at the sprintf page as it has a lot more detail.

As for what does "/^ $/" mean, you will have to delve into regular expressions for that. It is highly advised that you learn regular expressions as they are a big part of Perl and you will see them everywhere.

The / and / enclose the regular expression. The ^ specifies that the match will start at the beginning of the line and the $ specifies the end of the line. So, basically, the match in the code is looking for a line that ONLY contains the word "hex".

Regards,

Jeff
Feb 4 '08 #4

P: 5
Thank you all for the help.
Feb 4 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
Thank you all for the help.

Bookmark the perldoc website:

http://perldoc.perl.org/

You could have found the answer to all your questions there with a bit of searching.
Feb 4 '08 #6

P: 5
Bookmark the perldoc website:

http://perldoc.perl.org/

You could have found the answer to all your questions there with a bit of searching.
Will do. Thanks again.
Feb 5 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.