in reply to Re: pass code block to function (PROTOTYPES)
in thread pass code block to function (PROTOTYPES)

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!

Replies are listed 'Best First'.
Re^3: pass code block to function (PROTOTYPES)
by JavaFan (Canon) on Jul 15, 2009 at 10:40 UTC
    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
        But why is catch executed before try ?
        Because perl5 doesn't have lazy evaluation. If you call a function, perl will evaluate all its arguments. And since catch {/phooey/ and print "unphooey\n"} is one of the arguments of try, catch is called first. Which is why it's important that catch just returns its first argument, not evaluates it. And that's why you're getting the warning.