in reply to pass code block to function (PROTOTYPES)

Because all catch does is return the block. It doesn't call the code. The code is called from try, and then only when the first block fails.
  • Comment on Re: pass code block to function (PROTOTYPES)

Replies are listed 'Best First'.
Re^2: pass code block to function (PROTOTYPES)
by jeanluca (Deacon) on Jul 15, 2009 at 10:34 UTC
    that makes a lot of sense!
    I have now fix it as follows:
    #! /usr/bin/perl use strict ; use warnings ; sub try (&@) { my($try,$catch) = @_; eval { &$try }; if ($@) { $catch; } } sub catch (&) { &{$_[0]} } try { die "phooey"; } catch { print "unphooey\n"; }; catch { print "test\n"; } ;
    thnx!
      That doesn't make any sense. Now the argument of catch will be executed even before try is called. You've broken the entire reason of having a try/catch system.

      Why do you want to call catch directly anyway? It's like calling else without an if, or a continue without a while.

        think of catch as some function. I don't really care about the try/catch, I've used the code as a starting point for my experiments!

        UPDATE: But why is catch executed before try ?
        #! /usr/bin/perl -l use strict ; use warnings ; sub try (&@) { my($try,$catch) = @_; eval { &$try }; if ($@) { $catch; } } sub catch (&) { &{$_[0]} } try { die "phooey"; } catch { /phooey/ and print "unphooey\n"; }; __END__ output: Use of uninitialized value in pattern match (m//) at ./tc.pl line 18

        UPDATE: of course, its an argument which is evaluated first!! thnx JavaFan