Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Error Checking: Have you tried 'Try/Catch'?

by BrentDax (Hermit)
on Feb 20, 2001 at 14:31 UTC ( [id://59629]=note: print w/replies, xml ) Need Help??


in reply to Error Checking: Have you tried 'Try/Catch'?

2. For some reason, you can only have the function as the first operator in your prototype, which works fine for map, grep, and what have you, but is strangely rigid considering the broader scope of Perl.

I have a theory on this one. (Brace yourself...)

The only reason Perl can do that on the first argument is because it absolutely knows the first arg is a sub ref. If you had a prototype like foo($&), how is Perl to know what this means? (This is NOT a perfect example, but I think you can get the idea.)
foo $bar{$baz};
Is the {} a chunk of code, or someone trying to get a hash element and getting a scalar and a sub ref stored in that position? Obviously this depends on whether foo is a scalar or hash, but Larry and Friends decided not to write a complicated chunk of code for dealing with this, and I can't blame them. Just remember that the whole {}==sub{}, thing is a convenience only available when they can be absolutely sure of its meaning.

=cut
--Brent Dax

@HPAJ=split("", "rekcaH lreP rentonA tsuJ"); print reverse @HPAJ; #sucky but who cares?

Replies are listed 'Best First'.
Re^2: Error Checking: Have you tried 'Try/Catch'?
by tadman (Prior) on Feb 20, 2001 at 15:02 UTC
    You certainly have a good point from a parsing perspective, and this is likely where most of the implementation issues arise. Still, this limitation is really not all that different from the way Perl treats $AUTOLOAD type function calls. You have to be careful to adjoin the function name with brackets, because the compiler isn't astute enough to find the function reference, instead assuming there is a bare word in your code, and whining accordingly. Here's how the compiler appears to behave when finding functions to AUTOLOAD:
    Func($x) -> &Func($x) OK Func ($x) -> 'Func' ($x) !?!?
    So, presumably, the same thing would apply to the parsing of curly braces, in theory:
    foo $bar{$baz} -> &foo($bar{$baz}) foo $bar {$baz} -> &foo($bar,sub {$baz})
    I discovered this trying to eliminate the requirement for 'my ($self) = @_' having to be in every function for an OO program by writing a wrapper which did it for you. For whatever reason, you can't operate on a named sub. Oh well.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://59629]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-18 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found