Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'echo' do within a perl variable declaration?

Tags:

perl

I am working on transcribing an outdated file from perl to python and got caught up with some perl syntax.

my $jobID = `echo \$JOBID`;
chomp($jobID);
unless ($jobID) {
  print "Please specify a job\n";
  exit;
} 

Thus far, I have been able to transcribe all of the command-line parsing extremely easily but am very fairly stuck here with what exactly this code is doing (specifically the echo within the declaration on line 1).

Within the perl script cmd-line parsing options - that enables one to set the jobID - it states that "default = $JOBID". So my assumption is that the first line in this code simply sets this default jobID until one is specified.

If this is the case why would you need to use echo within the variable default declaration? Is this good practice in perl or am I missing a larger picture?

I have tried searching low and high for this but can't seem to google ninja my way to anything useful.

Any help on the 'echo' would be greatly appreciated (or any good reads on this as well)!

like image 963
Gerardo Hidalgo Avatar asked Nov 16 '25 05:11

Gerardo Hidalgo


2 Answers

This is one way to get a value from a shell variable. The backticks (`) run the shell command and give you the output. So the echo is running inside of a shell and in this case it just returns the one shell variable. A cleaner way to get this in Perl is to use %ENV like so:

 my $jobID = $ENV{'JOBID'};

This also removes the need for chomp, avoids creating an extra process, and is much more efficient.

like image 171
chicks Avatar answered Nov 18 '25 18:11

chicks


It is evaluating an environment variable named $JOBID and storing the result in $jobID, which (as duskwuff points out) is better accomplished using $ENV{JOBID}.

The backticks around the echo \$JOBID tell Perl to invoke the specified command in a subshell and return the output of the invoked command.