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. Advertising

  2. fl

    fl Guest

    On 3 nov, 07:37, fl <> wrote:
    > 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;


    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. Advertising

  3. fl

    fl Guest

    On 3 nov, 07:37, fl <> wrote:
    > 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;


    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

    On 3 nov, 07:37, fl <> wrote:
    > 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;


    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:

    > 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;


    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
    --
    These are my personal views and not those of Fujitsu Technology Solutions!
    Josef Möllers (Pinguinpfleger bei FTS)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://de.ts.fujitsu.com/imprint.html
    Josef Moellers, Nov 3, 2010
    #5
  6. On 03.11.2010 12:37, fl wrote:
    > 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;


    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. On Wed, 03 Nov 2010 13:22:24 +0100, Wolf Behrenhoff wrote:

    > 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.


    $ 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

    On 3 nov, 16:15, Sherm Pendley <> wrote:
    > fl <> writes:
    > > 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;

    >
    > If you're just beginning, the major lesson to learn from the above is:
    >
    >     DO NOT write such a mess!
    >
    > Seriously! Deliberately terse one-liners have a place, in obfuscated
    > code contests, Perl Golf, and clever .sig files. But, if you intend to
    > write and maintain useful code that gets Real Work (tm) done, clarity
    > is far more important than brevity.
    >
    > What's more, the use of eval in the above to construct a variable name
    > from a value is pure evil to be avoided whenever possible - use a real
    > hash instead of abusing the symbol table hash.
    >
    > A simple, clear way to write the above would be:
    >
    >     #!/usr/bin/perl
    >
    >     use warnings;
    >     use strict;
    >
    >     my %args;
    >     foreach my $arg (@ARGV) {
    >         if ($arg =~ /^(\w+)=(.*)/) {
    >             $args{$1} = $2;
    >         }
    >     }
    >
    > For anything other than a learning exercise though, I wouldn't bother
    > reinventing that wheel - GetOpt::Std and GetOpt::Long are sufficiently
    > round, and learning to avoid unnecessary work by taking advantage of
    > existing modules is a *big* part of learning Perl.
    >
    > sherm--
    >
    > --
    > Sherm Pendley
    >                                    <http://camelbones.sourceforge.net>
    > Cocoa Developer


    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. 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. JTL.zheng
    Replies:
    0
    Views:
    330
    JTL.zheng
    Jan 24, 2007
  2. grocery_stocker
    Replies:
    10
    Views:
    597
    Keith Thompson
    May 25, 2005
  3. 322322
    Replies:
    3
    Views:
    290
    James Kanze
    Mar 30, 2008
  4. byang
    Replies:
    37
    Views:
    888
    Chris Dollin
    Apr 27, 2009
  5. G.
    Replies:
    1
    Views:
    347
    Ian Kelly
    Sep 12, 2011
Loading...

Share This Page