Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What are ` (back ticks) used for in Perl. 5

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
US
Occasionally I have seen these ` (back ticks) used. I can't seem to replace them with a regular single quote, etc. So, I was curious as to what they do, when they are used, etc.

Can anyone explain this simply?

Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
The stuff inside the backticks undergoes variable interpretation, then is sent to the shell. My favorite thing to do (because I'm a newbie and don't know if there's a "slicker way" to do stuff like this) is to use this to call other Perl scripts--at the end of one program, just write:

`myscriptname2.pl`;

if it's in the same directory, or a path if it's not.
I'm currently writing a custom syslog parser, and we have one main program that calls a second, "plug-in" type script by setting $sys_version, then

`sys$sys_version\.pl`;

which calls the correct plug-in; Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
Typically you use backticks to execute an external command or script, *AND* get back the STDOUT of that command or script, like

my @ls_lines_of_stdout = `ls -rlt /tmp`;

where each line of "ls -rlt" output will be an element in the @ls_lines_of_output array.

Another way to execute an external command or script is to use the "system" perl command. If you don't need to capture the output of the command or script, when done properly(there is more than one way to use this command) this is the safer alternative since this can be done *without* invoking the shell.

Read the perldocs on this on *nix by doing:

perldoc -q backtick
perldoc -f system

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
An example of what was used ... was:

$now= `date`; # See the back ticks around date ?

So, why .. in this case .. would the back ticks be used. And, what would have been an alternative method?? (Is there one?)

Thanks,
Gary

Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
If you enter the command "date" (unquoted) into a Unix shell, you get back the current date-time. So

$now = `date`;

invokes the shell to find the date, and stores the value (e.g., Tue May 22 11:13:25 CDT 2001) in the variable $now.

Another way to do this is through Perl's own "localtime" function (once again, w/o the quotes),

$now = localtime; Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
The writer of

$now = `date`;

wanted to retrieve the current date into the $now variable - he/she did this by executing the "date" command using backticks. On *nix, this is the same as you typing "date" at the command prompt - the date that comes back is the same date that will populate the $now variable above.

Perl also has a function called "localtime" that can be used for the same purpose - read about this on *nix by doing "perldoc -f localtime" at the command prompt. When used in scalar context:

$now_string = localtime; # e.g., "Thu Oct 13 04:54:34 1994"

it returns fully formated date value. Using the perl "localtime" function is safer since it is a perl function - it doesn't require the execution of an external command(which can be unsafe) like backticks does.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
So, ... the back ticks around the word date are acutally (from what I am understanding) ... extracting the value of the command date from the Unix shell. Then, assigning the value to the variable??

Is that correct?

PS: What is (a) the Unix shell? (Now don't go laughing at me.) hahaha

Sorry for my ignorance with this.

Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
So writing `date` from within a Perl script ... is the same as typeing "date" on the command line?? What I am gathering is ... in order to 'launch a program or unix command' ... you need to use the ` (back ticks)??

Is that correct?

Thanks,
Gary

PS: Is there any other way to launch a program or command .. other than with back ticks? Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
The unix "shell" is the command interpreter used to execute unix commands. This can be one of a variety of shells with varying syntax and capabilities including the standard shell (sh), the "C-shell" (csh), the "Korn Shell" (ksh), the "Bourne Shell" (bsh), and the "Bourne-Again Shell" (bash). It works something like DOS - interpreting the commands you type in and calling the appropriate system functions.

You use the backticks to do things that you can't do conveniently with a perl command, such as running a shell program, or running a system program that returns output which you need in your program (in which case you assign the value of the backticked expression to a scalar or list variable). Backticks can also be used on non-unix systems to run commands. You should be able to execute a DOS command on a DOS/Windows box using the backticks too.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Wow.

Thank you Tracy!

That helped a great deal.

Truly!!!

I have a question for you.

I am trying to learn Perl, and I am presumming that you can tell (from my questions, etc.) that I know very very little at this point. But, I know some stuff.

I am looking for some type of STEP BY STEP TUTORIAL that will explain a little, then ask me (or show me) an example of what was just taught, then continue on, etc., etc.

I would actually love to find a TUTORIAL for Perl ... maybe on CD where it was interactive or something. (I don't have the time to try and attend a formal class .. so I need to find something that will present the information I need in a slow, logical and detailed / structured format - that I can learn from.

I have puchased many books, including the camel series (on CD and in book form. I have also purchased the learn Perl in 21 days stuff, etc.

Nothing, that I have found so far is GREAT. They are all ok in their own way.

Do you have any suggestions as to what I should do, get, read, etc. - in order to learn Perl????

Thanks,
Gary




Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
I've never actually bought or read a book on perl. The way I learned perl was to read all of the perl docs in the recommended order and then started playing around with it. I don't recommend that method, but it worked for me. The perl docs DO have some good, simple examples, and I STILL use them for reference all the time (you don't think I had all that info above AND the perldoc reference memorized, do you?). If you've installed perl on your computer (I have ActiveState PERL) the docs are installed in HTML format. They main page should be in C:\Perl\html\lib\Pod\perl.html or something similar.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Are you using Perl on a Unix box, or ActiveState Perl on Windows, or other? Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
Brad,

I am learning it while I work on my PC (Windows 98), but I am using it on a Unix box - through my web hosting company. So, I learn it on my windows os, but use it (when I upload it, etc.) to the Unix box.

Does that answer your question?

Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
I started learning Perl a couple months ago and am using the ActiveState Perl for Windows port. I borrowed a copy of "Learning Perl" to get the basic syntax, then started playing around with the modules that come in the ActiveState documentation. This has worked wonders for me.

One caveat:
you can use backticks to execute commands in the DOS shell in Windows, as well as your shell in Unix. However, if you transfer scripts from Windows onto your Unix box, keep in mind the differences between DOS and Unix command lines. For instance, "erase" in DOS vs. "rm" in Unix.

Good Luck! Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
Brad and Tracy,

Being that I have never yet written a program, ... where would you recommend I start?

Any ... 'getting started' tips??

Gary


Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Start with something very simple and get it to work. Then add another "nice" feature and test that, repeat until you can't think of anything else to add. That's one way I learn a new language (and I've learned over a dozen in my career).

Also, looking at someone else's code is a good way to learn, but be aware that they may be doing some pretty sophisticated things that you may not be able to understand at first. It seems like that's what you've been doing. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Usually by the time I've finished the process of adding new features until I've got it doing all I want it to I've mananged to push my own limits as well as the limits of the language. EXCEPT: I don't think I've ever managed to push the limits of PERL - I'm not sure there ARE any limits to PERL.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Tracy,

:)

I think .. for me ... something that would be great (but I don't know where or how to find it) would be a real 'step by step' tutorial that would show me how to build a very simple script, then add more, then add more ... etc., just as you described, and as it would go along, ask me to think and try .. but then it would show me the answer, etc.

Anyhow ... do you have any ideas on anything like that being available. (Kind of like a Photoshop tutorial, etc.)

If not, don't even worry. I appreciated all your advice.

Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
I've never seen something like that - but I've never really looked either. Hopefully one of the other gurus (or even a beginnner) has found something like that they can refer you to. I'd love to be able to write one, but it would take a lot of time that I don't have.

Maybe my boss will let me set something like that up on our web site, and just add to it a little every day or so. I think it would be fun to do as you say, and add a brief tutorial on a new topic every day, with a little "homework" assignment, and a link to the answer. It could start out very simply and gradually work toward very complex topics. It might take months to get to the real good stuff, but it would be a good learning tool (and good advertising for my company too). I'll have to have a chat with him about it.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top