Hi Anant,

next time please reply to all recipients instead of only to me as you did just
now (as I suggest in my signature which you should have read.). I'll reply to
you while CCing the list.

On Fri, 19 Aug 2011 22:36:42 +0530
anant mittal <perl.an...@gmail.com> wrote:

> what does this eval do?

There's block eval (which can be used to trap exceptions) and there's
string-eval, which while trapping exceptions, executes the code in the string
as a small perl program. See:

http://en.wikipedia.org/wiki/Eval

It has a small number of legitimate uses, but otherwise can be easily abused.
If you do in a program

#!/usr/bin/perl

use strict;
use warnings;

my $expr = shift(@ARGV);
eval $expr;

Then if you allow the user of the program to input arbitrary Perl code inside
$ARGV[0], which will then be executed. So don't abuse string eval this  way.

> and also what this 'ge' is.'g' is for global substitution but 'ge' is for
> what.

Well, in Perl 5, each operation modifier is a single letter and can be studied
as is. So /ge is both "g" and "e". What /e (which stands for "expression" or
"evaluate") does is treat the right-hand-side of the substitution as a Perl
expression, instead of the string concatenation thing that is usually is. This
program:

[CODE]
#!/usr/bin/perl

use strict;
use warnings;

my $s = "The quick brown fox jumped over the lazy dog";

$s =~ s{\b(\w+)\b}{my $word = $1; print "Got word '$word'\n"; uc($word); }eg;

print "String is now '$s'.\n";
[/CODE]

Will print this:

[OUT]
Got word 'The'
Got word 'quick'
Got word 'brown'
Got word 'fox'
Got word 'jumped'
Got word 'over'
Got word 'the'
Got word 'lazy'
Got word 'dog'
String is now 'THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'.
[/OUT]

The /e flag of s/// is a very powerful tool - make sure you know it. One should
note that one can add another /e to the end of s/ to evaluate the expression
twice, but this is generally inadvised due from the same reasons that string
eval is dangerous.

Hope that helps.

Regards,

        Shlomi Fish

> plz let me known to it.
> 
> On Fri, Aug 19, 2011 at 1:29 PM, Shlomi Fish <shlo...@shlomifish.org> wrote:
> 
> > Hi,
> >
> > On Wed, 17 Aug 2011 16:15:18 -0400
> > "Uri Guttman" <u...@stemsystems.com> wrote:
> >
> > > >>>>> "RP" == Rajeev Prasad <rp.ne...@yahoo.com> writes:
> > >
> > >   RP> foreach $line (@arr1){
> > >   RP>  foreach (@arr2) {
> > >   RP>  chomp($_);
> > >   RP>  @arr3 = split(/ /,$_);
> > >   RP>  $mystringvar = eval "qq{$line}";             <--------------this
> > >   RP> suggestion came from web search.
> > >
> > > and it is a very bad idea. string eval is very dangerous and shouldn't
> > > be used for simple things like that. you can do the same thing with a
> > > hash and an s/// operation and be safe. string eval can execute any code
> > > that is in your data which could cause havoc in your program so don't do
> > > it unless you know exactly what is going on. you were given other
> > > working solutions so use those instead of string eval.
> > >
> >
> > I agree with Uri here. Please don't use string eval here. If someone puts
> > in
> > $line something like:
> >
> >        }.system("rm", "-fr", $ENV{HOME}).{
> >
> > Then you'll lose your home directory.
> >
> > Regards,
> >
> >        Shlomi Fish
> >
> > > uri
> > >
> >
> >
> >
> > --
> > -----------------------------------------------------------------
> > Shlomi Fish       http://www.shlomifish.org/
> > Best Introductory Programming Language - http://shlom.in/intro-lang
> >
> > We have nothing to fear but fear itself. Fear has nothing to fear but XSLT.
> >
> > Please reply to list if it's a mailing list post - http://shlom.in/reply .
> >
> > --
> >  To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> > For additional commands, e-mail: beginners-h...@perl.org
> > http://learn.perl.org/
> >
> >
> >



-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Rethinking CPAN - http://shlom.in/rethinking-cpan

Chuck Norris writes understandable Perl code.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to