473,587 Members | 2,324 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 1585
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.or g, 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.or g/

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.or g/

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
2014
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 general purpose OO programming language that finds use among some system administrators, but my guess is that Perl is still more common in that area than...
4
2520
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 $keyword=$ARGV ; @key=split(//,$keyword) ; $period=length($keyword) ;
3
15275
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 have a perl script that runs on my webserver, which is supposed to login to the gameserver and download the log. The problem is that when it gets to...
9
4508
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 purposes. I have to admit that, although I'm very familiar with Python, I'm complete Perl noob (and I hope to stay one) which is reflected in my questions....
0
5232
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 other noobs as well. Ok, my problem is: Windows Server 2003 Plesk for Windows 7.5.6 X-Cart Software (ecommerce solution)
1
2730
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 specified in the XML schema file. I then would like to initialize the various "fields" that make up theoverall item. Once I have all my data in order I...
6
5745
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 already downloaded (this is a pre-processing front end to some other fortran code). However, in the part indicated below (the second FTP execution), I get...
6
1983
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 http://nonsense.sourceforge.net/): ### Load and parse a datafile, slurping the contents into the %pool hash sub LoadDataFile { my $file = shift; $file = SafeFile(...
1
2357
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 attached to two rectangles, however, I am having trouble getting the connector object to draw. I have: our $Visio =...
0
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.