Using command line argument as variable name

Discussion in 'Perl Misc' started by Al, Jun 23, 2006.

  1. Al

    Al Guest

    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
     
    Al, Jun 23, 2006
    #1
    1. Advertising

  2. Al wrote:
    > 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
     
    Nick of course, Jun 23, 2006
    #2
    1. Advertising

  3. Al wrote:
    > 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
     
    Jürgen Exner, Jun 23, 2006
    #3
  4. Al wrote:
    > 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";
    }
     
    it_says_BALLS_on_your forehead, Jun 23, 2006
    #4
  5. Al

    darksaga Guest

    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
     
    darksaga, Jun 23, 2006
    #5
  6. Al

    Al Guest

    Nick of course wrote:
    > Al wrote:
    > > 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


    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?
     
    Al, Jun 23, 2006
    #6
  7. Al wrote:
    > 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
     
    Jürgen Exner, Jun 23, 2006
    #7
  8. Al

    Mirco Wahab Guest

    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
     
    Mirco Wahab, Jun 23, 2006
    #8
  9. Al

    Al Guest

    Thanks very much for the feedback everybody!
    - Al

    Jürgen Exner wrote:
    > Al wrote:
    > > 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
     
    Al, Jun 23, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fong
    Replies:
    3
    Views:
    1,230
  2. Ben Kial
    Replies:
    1
    Views:
    719
    Eric Enright
    Nov 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,984
    Eric Sosman
    Jan 7, 2005
  4. Bobby Chamness
    Replies:
    2
    Views:
    2,440
    Joe Smith
    Apr 22, 2007
  5. AikidoGuy
    Replies:
    11
    Views:
    594
    Seebs
    Nov 21, 2011
Loading...

Share This Page