Ask a Question related to Perl / CGI, Design and Development.

  1. #1

    Default What is eval?

    Can anybody explain the functionality of eval in brief?

    Thanks in advance,
    Mallik.
    Mallik Guest

  2. Similar Questions and Discussions

    1. Eval()
      I know i could use EVAL to evaluate VARs but what about if i want to evaluate a Var that have a funtion onit? like A="math.Cos(65)" B=Eval(A);...
    2. eval vs. set?
      I'm trying to dynamically create movieclip instances. They seem to be created fine, but when I go to move them it does not work. I've tried line 1...
    3. eval'
      I am retrieving the following string from the database. " and upper(gsa_template_data_header) >= '".$start_selection."' and...
    4. eval
      PHP-Code: eval('?>'.$string) Vars: eval('$string="'.str_replace('"','\\"',$string).'";'); ------------------------
    5. Help with eval
      I am trying to write a perl script that can parse a verilog file with syntax like: `if ABC == 1 abc is set something `else abc not set `endif...
  3. #2

    Default Re: What is eval?

    On Jan 29, 2004, at 6:26 AM, Mallik wrote:
    > Can anybody explain the functionality of eval in brief?
    perldoc -f eval

    James

    James Edward Gray II Guest

  4. #3

    Default RE: What is eval?

    [url]http://www.perldoc.com/perl5.8.0/pod/func/eval.html[/url]

    Bradley

    -----Original Message-----
    From: Mallik [mailto:mallikarjunk@softima.com]
    Sent: Thursday, January 29, 2004 7:26 AM
    To: Perl Beginners
    Subject: What is eval?


    Can anybody explain the functionality of eval in brief?

    Thanks in advance,
    Mallik.

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>


    Bradley A. Brown Guest

  5. #4

    Default Re: What is eval?

    On Jan 29, Mallik said:
    >Can anybody explain the functionality of eval in brief?
    For starters, please read 'perldoc -f eval'. All Perl functions are
    explained in detail in the standard documentation.

    eval() has two forms. The first takes a string, and treats it as Perl
    code. It compiles and executes it, returning whatever value is
    appropriate. If there is a fatal error, it sets the $@ variable.

    my $string = "make the vowels capitals";
    my $from = "aeiou";
    my $to = "AEIOU";

    eval "\$string =~ tr/$from/$to/";
    # now $string is mAkE thE vOwEls cApItAls

    We had to use eval() here, because tr/// doesn't interpolate variables.
    If we'd just done

    $string =~ tr/$from/$to/;

    we'd be changing $ to $, f to t, r to o, o to o, and m to o, and the
    string would be "oake the vowels capitals".

    The other form is eval { CODE }. This executes a block of code in a
    fail-safe environment. If there are any fatal errors, the $@ is set, and
    the block is exited. A common use for this is:

    print "Give me a number: ";
    chomp(my $n = <STDIN>);
    my $result = eval { 100 / $n };
    if ($@) { print "You tried dividing by zero.\n" }

    Basically, if we didn't use eval { } here, the program would die if the
    user entered the number 0.

    --
    Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  6. #5

    Default RE: What is eval?

    It evaluates the code that you give it. It can be used when you need to
    create code on the fly, like this...

    my $cmd = 'print';
    my $arg = 'Hello World';
    eval("$cmd '$arg'");

    This is useful for allowing a user to pass code to the program (for whatever
    reason).

    The other use it to trap errors.

    my $this = 1;
    my $that = 2;
    eval {
    if ($this != $that) {
    die "They don't match!";
    }
    };
    print $@;

    In this case the "die" doesn't terminate the program, it only terminates the
    eval. And $@ stores the error message that was "thrown" in the eval. So
    this prints the error message without abnormally terminating the program.
    This is akin to the try/catch blocks in other languages (the Errors gives
    you try/catch functionality by using eval).

    Hope that helps.

    Rob

    -----Original Message-----
    From: Mallik [mailto:mallikarjunk@softima.com]
    Sent: Thursday, January 29, 2004 7:26 AM
    To: Perl Beginners
    Subject: What is eval?


    Can anybody explain the functionality of eval in brief?

    Thanks in advance,
    Mallik.

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>

    Rob Hanson Guest

  7. #6

    Default Re: What is eval?

    Mallik wrote:
    > Can anybody explain the functionality of eval in brief?
    There are two different flavors of eval().

    The "string" version looks like this:

    eval $code;

    And it treats its argument as a string containing Perl source code,
    which it compiles and executes.

    The "block" version looks like this:

    eval { $obj->might_die() };

    And its argument is just an ordinary block (like the body of an
    "if" statement). Both forms will trap fatal errors and place the
    error in $@.

    There's a more complete explanation in "perlfunc"

    % perldoc -f eval

    --
    Steve
    Steve Grazzini Guest

  8. #7

    Default Re: What is eval?

    James Edward Gray II wrote:
    >
    > On Jan 29, 2004, at 6:26 AM, Mallik wrote:
    >
    > > Can anybody explain the functionality of eval in brief?
    >
    > perldoc -f eval
    How succinct!

    /R


    Rob Dixon Guest

  9. #8

    Default Re: What is eval?

    Hi,

    I am writing some script here i have noticed that first character of Japanese string (when used in eval function) is replaced with result1 or result2 variable.
    Here is the script that i am using

    #!/usr/bin/perl
    my $string1 = "<japanese string>";
    my $string2 = "Type";
    my $result1 = "S";
    my $result2 = "G";
    my $length = 8;

    print "EXPECTED\n";
    print "$string2: $result1\n";
    print "$string1: $result1\n";
    print "$string1: $result2\n";

    print "\n";

    print "ACTUAL\n";
    eval (
    "format TEST = \n"
    . $string2 . ':' . ' ' x ($length - length($string2)) .' @' . "\n"
    . '$result1'."\n"
    . $string1 . ':' . ' ' x ($length - length($string1)) .' @' . "\n"
    . '$result1'."\n"
    . $string1 . ':' . ' ' x ($length - length($string1)) .' @' . "\n"
    . '$result2'."\n"
    . ".\n");

    $�~ = 'TEST';
    write;


    Output of script:

    EXPECTED
    Type: S
    ^Cv: S
    ^Cv: G

    ACTUAL
    Type: S
    SCv:
    GCv:

    Can someone please explain me why eval function not abale to give result as expected

    Thanks in advance
    ashishgupta62@gmail.com is offline Junior Member
    Join Date
    Aug 2011
    Posts
    1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139