473,513 Members | 2,684 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl Noob Question #2

5 New Member
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
6 1582
nithinpes
410 Recognized Expert Contributor
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
410 Recognized Expert Contributor
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
3,509 Recognized Expert Moderator Specialist
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
Iron Blood
5 New Member
Thank you all for the help.
Feb 4 '08 #5
KevinADC
4,059 Recognized Expert Specialist
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
Iron Blood
5 New Member
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

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

Similar topics

23
1996
by: John M. Gabriele | last post by:
I recently posted this sort of question to the c.l.p.m but didn't get much of a response. I know a little Perl and a little Python, but master neither at the moment. I see that Python is a...
4
2516
by: Piotr Turkowski | last post by:
Hi! I've got some code in Perl and I have to have it in C, but my knowlege of Perl is < 0 :-(, so I need your help. here's the code. Thanks in advance. decrypt.pl #!/usr/local/bin/perl...
3
15270
by: FLOTServer | last post by:
Here's my problem: I run a gameserver that runs the game "Medal of Honor". On the game server is log file which contains all of the data from the players for that day (kills, deaths, etc...). I...
9
4503
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
0
5220
by: gedsta | last post by:
Hi all I am a total noob and its my 2nd post, first of all thank you for letting me use these forums as a source of information, and hopefully once i get my head round perl i maybe able to help...
1
2724
by: Ken Browning | last post by:
I have been working with Perl for a while now, but have not used it with XML - I am an XML noob. I want to be able to read an XML schema file and initialize an instance of the data item...
6
5736
by: psutoad | last post by:
I am somewhat a Perl Noob, but I've done some programming with my animal books at my side. This issue, however doesn't make sense to me. I am grabbing data via FTP corresponding to data I've...
6
1980
by: Paulchen | last post by:
Hello, I have found a perl script and need to "translate" this to PHP. I try to do it step by step and the first part of it is this function (the whole script is found at...
1
2352
by: JoeGilligan | last post by:
Hey Everyone, I am really stuck...possibly more stuck than almost anyone has ever been in his/her entire life. I am trying to write a Perl script that uses Visio to draw a 'connector' that is...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.