Using command line argument as variable name

A

Al

How do I escape (or whatever) a command line argument so that I can use
it's content as a variable name in my program? E.g.:

myprogram myvariable

if ($myvariable =~ ....

Thanks much in advance,
Al
 
N

Nick of course

Al said:
How do I escape (or whatever) a command line argument so that I can use
it's content as a variable name in my program? E.g.:

myprogram myvariable

if ($myvariable =~ ....

Thanks much in advance,
Al
Sense not question make
 
J

Jürgen Exner

Al said:
How do I escape (or whatever) a command line argument so that I can
use it's content as a variable name in my program? E.g.:

myprogram myvariable

if ($myvariable =~ ....

The keyword you are looking for is symbolic references.
Please see the FAQ ("How can I use a variable as a variable name?") and
numerous earlier postings for details about why this is a Bad Idea (TM) and
what to do instead.

jue
 
I

it_says_BALLS_on_your forehead

Al said:
How do I escape (or whatever) a command line argument so that I can use
it's content as a variable name in my program? E.g.:

myprogram myvariable

if ($myvariable =~ ....

If I'm understanding your intent...

my_script.pl param1 param2

#!/usr/bin/perl

use strict; use warnings;

my ( $arg1, $arg2 ) = @ARGV;

if ( $arg1 =~ m/para/ ) {
print "yay!\n";
}
else {
print "boo!\n";
}
 
D

darksaga

use Getopt::Long;

my $msg;
my $msg2;
GetOptions(
'-FLAGNAME=s' => \$msg,
'-FLAGNAME2=s' => \$msg2
);
print "$msg\n$msg2\n"

call your script as follows:
perl myScript.pl -FLAGNAME hello -FLAGNAME2 world

greetz darksaga
 
A

Al

Nick said:
Sense not question make

Okay - let's say I have a program called: myprogram, and a command line
argument of: myvariable. I run the program like this:

myprogram myvariable

Within my program, there is an existing variable called: $myvariable

How can I address this variable in terms of the command line argument -
i.e., how do I find the value of $myvariable by referencing what was
passed on the command line? For example:

if ([command line argument substitution designating $myvariable] =~
'hello' )...

Sense make more?
 
J

Jürgen Exner

Al said:
Okay - let's say I have a program called: myprogram, and a command
line argument of: myvariable. I run the program like this:

myprogram myvariable

Within my program, there is an existing variable called: $myvariable

How can I address this variable in terms of the command line argument
- i.e., how do I find the value of $myvariable by referencing what was
passed on the command line? For example:

if ([command line argument substitution designating $myvariable] =~
'hello' )...

Sense make more?

Are you actually reading what people are writing? Again:

YOU ARE LOOKING FOR SYMBOLIC REFERENCES. SEE THE FAQ AND DEJANEWS FOR
DETAILS ABOUT WHY THEY ARE EVIL AND WHAT TO USE INSTEAD.

jue
 
M

Mirco Wahab

Thus spoke Al (on 2006-06-23 16:24):
Within my program, there is an existing variable called: $myvariable

How can I address this variable in terms of the command line argument -
i.e., how do I find the value of $myvariable by referencing what was
passed on the command line? For example:

if ([command line argument substitution designating $myvariable] =~
'hello' )...

Names are hash entries in the perl guts,
so use your own hash for your names, like:

my %NAMES;
my ($varname) = shift; # <== will be 'myvariable' etc.

$NAMES{ $varname } = 1e-4;

print "name of variable was: ",
grep { /$varname/ } keys %NAMES;

# above is basically the same as :
# print "name of variable was: ", $varname;

print "\nactual value of it is: ", $NAMES{ $varname }, "\n";


Don't use Perls Name-Hash directly, as most others
here have already said.

Regards

Mirco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top