You are missing something, here are two examples:
# prototypes only check arg format
# i.e. (scalar/array/hash/code/glob and number of args)
package Sample;
sub stringify () {
return ($_[0]->{data}) x $_[1]
}
package main;
sub print_string($) {
print $_[0]->stringify(1)
}
my $obj = bless ( {data=>"foo\n"}, 'Sample');
print_string($obj); # fine
print_string(5); # uh oh... basic numbers don't have a stringify me
+thod,
# yet the arg passed the prototype since 5 is a sc
+alar.
And also:
# methods don't check prototypes... at all.
sub print_string_bad($) {
print $_[0]->stringify() # Will pass the prototype, yet break since $_
+[1] will be
# undef in stringify
}
print_string_bad($obj);
Hence the reason for the general distaste for prototypes among the perl community. For most cases, they're pointless. They're only there so that you can call your own subs like perl-builtins.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|