return() inside eval

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default return() inside eval

    This works as needed:

    #!/usr/local/bin/perl
    use strict;
    use Exception::Class( 'Exception' );

    sub test() {
    eval {
    return( param1 => 'first',
    param2 => 'second' );
    };
    #print "here\n";
    }

    my %ret = &test();
    print "param1:".$ret{param1}."\n";
    print "param2:".$ret{param2}."\n";
    >./test.pl
    param1:first
    param2:second

    This doesn't. Why doesn't return work
    properly in this case. Why is the print
    clause executed? perl5.8.0
    #!/usr/local/bin/perl
    use strict;
    use Exception::Class( 'Exception' );

    sub test() {
    eval {
    return( param1 => 'first',
    param2 => 'second' );
    };
    print "here\n";
    }

    my %ret = &test();
    print "param1:".$ret{param1}."\n";
    print "param2:".$ret{param2}."\n";
    >./test.pl
    here
    param1:
    param2:



    A.J. Guest

  2. Similar Questions and Discussions

    1. #39982 [NEW]: when used inside a function, proc_open generated pipes are closed upon return
      From: federico at galassi dot net Operating system: gentoo linux PHP version: 5.2.0 PHP Bug Type: Program Execution Bug...
    2. Can't use return key to edit text inside a text box
      When I initially type a text in a text box, I can use the return key to create a space between paragraphs. Once I leave the text box I can't go back...
    3. Dynamic temp. datagrid col.gen. -Session access inside a class inside a UserCtrl
      Hello Dear Professionals: Based on this document:...
    4. 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);...
    5. eval'
      I am retrieving the following string from the database. " and upper(gsa_template_data_header) >= '".$start_selection."' and...
  3. #2

    Default Re: return() inside eval

    Using return within your eval returns from the eval block, not the
    surrounding subroutine. The reason the first version works is that the
    eval is the last statement, and therefore it's value is used as the
    return value. In the first version you are returning eval which in turn
    returns your hash. In the second version you are running eval, ignoring
    the result and returning print.

    Stefan


    A.J. wrote:
    > This works as needed:
    >
    > #!/usr/local/bin/perl
    > use strict;
    > use Exception::Class( 'Exception' );
    >
    > sub test() {
    > eval {
    > return( param1 => 'first',
    > param2 => 'second' );
    > };
    > #print "here\n";
    > }
    >
    > my %ret = &test();
    > print "param1:".$ret{param1}."\n";
    > print "param2:".$ret{param2}."\n";
    >
    >
    >>./test.pl
    >
    > param1:first
    > param2:second
    >
    > This doesn't. Why doesn't return work
    > properly in this case. Why is the print
    > clause executed? perl5.8.0
    > #!/usr/local/bin/perl
    > use strict;
    > use Exception::Class( 'Exception' );
    >
    > sub test() {
    > eval {
    > return( param1 => 'first',
    > param2 => 'second' );
    > };
    > print "here\n";
    > }
    >
    > my %ret = &test();
    > print "param1:".$ret{param1}."\n";
    > print "param2:".$ret{param2}."\n";
    >
    >
    >>./test.pl
    >
    > here
    > param1:
    > param2:
    >
    >
    >
    Stefan Guest

  4. #3

    Default Re: return() inside eval

    The reason I want to use eval is to catch
    possible exceptions inside various subroutines.
    I'm using Exception::Class. Is there a better
    way of doing this?

    sub test() {
    eval {
    do something..

    return( param1 => 'first',
    param2 => 'second' );
    };
    if( UNIVERSAL::isa( $@, 'Exception' ) ) {
    return( param1 => 'Catched exception:'.$@->message,
    param2 => '' );
    }
    }

    A.J. Guest

  5. #4

    Default Re: return() inside eval

    A.J. wrote:
    > The reason I want to use eval is to catch
    > possible exceptions inside various subroutines.
    > I'm using Exception::Class. Is there a better
    > way of doing this?
    Sure, just make sure you put your result in a variable before returning
    it. For example:

    sub test() {
    my %result;
    eval {
    do something...
    %result = (
    param1 => 'first',...
    );
    }
    if ($@) {
    ...
    }
    else {
    return %result;
    }
    }

    Stefan Guest

Posting Permissions

  • You may not post new threads
  • You may 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