455,848 Members | 1,375 Online
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 #!/usr/bin/perl -w use strict; print "This program will convert Decimal to Binary \n"; print "Please pick your Decimal Number \n"; # Get the number from the command line or use default. my \$number = shift || ; printf "Binary Number ---- > %b\n", \$number;   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 #!/usr/bin/perl -w use strict; print "Do you want a hex or octal \n"; my \$hoo = ; chomp(\$hoo); if (\$hoo =~ /^hex\$/) { print "Please Type a Hex Number \n"; my \$A = ; chomp(\$A); print "\n"; print hex("\$A")," <--- That is your Hex Number \n"; } else { print "Please Type a Octal Number\n"; my \$AA = ; chomp(\$AA); print "\n"; print oct("\$AA")," <--- That is your Octal Number \n"; }   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
6 Replies

 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 printf "%.2f", \$value;   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

 Expert 100+ P: 410 I missed out one question. The line: Expand|Select|Wrap|Line Numbers my \$number = shift || ;   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