Could you give me some explanation on this Perl line

Discussion in 'Perl Misc' started by fl, Nov 3, 2010.

  1. fl

    fl Guest

    Hi,
    I just begin Perl learning. A sample Perl program is relevant to my
    project. After reading some on Perl, I still do not understand the
    first line of this sample, see below. Could you help me on its
    meaning? Thanks in advance.


    eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift;
     
    fl, Nov 3, 2010
    #1
    1. Advertisements

  2. fl

    fl Guest

    More specific here. Why there is one more $ in the first argument? \$
    $1 means '$1'? I still do not understand the reason for this. Second,
    There is no definition on w, what is the \w+ for? Thanks
     
    fl, Nov 3, 2010
    #2
    1. Advertisements

  3. fl

    fl Guest

    More specific here. Why there is one more $ in the first argument? \$
    $1 means '$1'? I still do not understand the reason for this. Thanks
     
    fl, Nov 3, 2010
    #3
  4. fl

    fl Guest

    More specific here. Why there is one more $ in the first argument? \$
    $1 means '$1'? I still do not understand the reason for this.
    In order to understand it clearly, could you write the above line in
    several small lines?
    Thanks
     
    fl, Nov 3, 2010
    #4
  5. Am 3.11.2010 schrub fl:
    In essense, it takes each argument until it reaches one that doesn't
    look like <word>=<something> and assigns <something> to the variable $word.

    There is more than one $ in the first argument as the first $ is escaped
    and the "$1" is replaced by the first matching string, resulting in
    "$<word>" and the "$2" is replaced by whatever is after the equals sign.

    Note that fiddling around with variable names like this is deprecated.
    One should use a hash for this.

    HTH,

    Josef
     
    Josef Moellers, Nov 3, 2010
    #5
  6. This piece of code should NOT be relevant to your project as it is very
    bad code.

    eval
    "\$$1=\$2" <--- It creates a variable called $1 and assign the value
    $2 to it. You should NOT do this, use a hash instead. Read
    perldoc -q "variable name" for an explanation.
    while
    @ARGV Do this for all command line arguments
    && $ARGV[0]=~ /^(\w+)=(.*)/ as long as it looks like var=value
    && shift; and remove this command line arg

    Better use Getopt! See
    perldoc Getopt::Std
    perldoc Getopt::Long

    Also always "use strict; use warnings;" in all your programs. The above
    example will not even compile with "use strict". That is a clear sign
    that something should be changed in the code.

    Wolf
     
    Wolf Behrenhoff, Nov 3, 2010
    #6
  7. $ cat t.pl
    #!/usr/bin/perl
    use warnings;
    use strict;
    eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift;

    $ perl t.pl

    Although I agree, you should not set variables like this, the code does
    compile under strict.

    M4
     
    Martijn Lievaart, Nov 3, 2010
    #7
  8. fl

    fl Guest

    Thanks to all of you. From what you said, I know this line's function
    and where I can get the appropriate way (GetOpt) to do that. The
    feedbacks make me avoid some bad thing on this learning.
     
    fl, Nov 3, 2010
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.