Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get command-line Perl to accept shell variables?

Tags:

bash

perl

I can do math like

perl -e 'print 5253413/39151' -l

But I don't quite get how to take advantage of Perl's ability to do math with my own predefined bash variables. I've tried

var1=$(some wc command that yields a number); var1=$(some wc that yields another number)
perl -e 'print var1/var2' -l

But it doesn't work

like image 648
Tom Avatar asked Jan 19 '16 01:01

Tom


People also ask

How do I accept a command line argument in Perl?

To access your script's command-line arguments, you just need to read from @ARGV array. Perl allows using @ARGV array as filenames by using <>. The $ARGV contains the name of the current file when reading from <>.

How do you access shell environment variables?

Printing Shell and Environmental Variables. Each shell session keeps track of its own shell and environmental variables. We can access these in a few different ways. We can see a list of all of our environmental variables by using the env or printenv commands.

Can we call shell script from Perl?

Explanation. Line 2: We call a shell command from a Perl script, using backticks, and capture the output in a Perl variable output . Line 4: We display the output captured in line 2. Line 8: We call a shell command from the Perl script using the system function.


2 Answers

There are two main ways to do this.

  • Within the Perl code you can use the %ENV built-in hash to access environment variables that are exported from the shell

    $ export var1=5253413
    $ export var2=39151
    $ perl -E 'say $ENV{var1}/$ENV{var2}'
    134.183366963807
    
  • You can use the shell interpolation facility to insert the value of a shell variable into a command

    This is best done as parameters to the perl one-liner rather than introducing the values directly into the code

    $ var1=5253413
    $ var2=39151
    $ perl -E '($v1, $v2) = @ARGV; say $v1/$v2' $var1 $var2
    134.183366963807
    
like image 115
Borodin Avatar answered Sep 23 '22 16:09

Borodin


Two less common ways to do this make use of long-standing perl features.

The first is the core module Env, which ties process environment variables to perl variables:

sh$ export VAR1=1000
sh$ export VAR2=33
sh$ perl -MEnv -E 'say $VAR1/$VAR2'           # imports all environ vars
333.333333333333
sh$ perl -MEnv=VAR1,VAR2 -E 'say $VAR1/$VAR2' # imports only VAR1, VAR2
333.333333333333

Note that the variables need to be present in the environment inherited by the perl process, for example with export VAR as above, or explicitly for a single command (as by FOO=hello perl -MEnv -E 'say $FOO').

The second and rather more obscure way is to use use perl's -s switch to set arbitrary variables from the command line:

sh$ VAR1=1000
sh$ VAR2=33
sh$ perl -s -E 'say $dividend/$divisor' -- -dividend=$VAR1 -divisor=$VAR2
333.333333333333

awk does something similar with its -v switch.

like image 33
pilcrow Avatar answered Sep 23 '22 16:09

pilcrow