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

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

Replies are listed 'Best First'.
Re^5: pass code block to function (PROTOTYPES)
by JavaFan (Canon) on Jul 15, 2009 at 13:04 UTC
    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.