Type detection: Difference between revisions

Content added Content deleted
m (→‎{{header|C}}: Remove vanity tags)
(Added Perl example)
Line 332: Line 332:
</lang>
</lang>


=={{header|Perl}}==
The function <code>ref</code> takes a reference to a variable, via '\', and returns the type. Some of the more common are shown here.
In the cases where the value in question is already a reference (<code>$regex</code> and <code>$subref</code>) the '\' is not used.
<lang perl>$scalar = 1;
@array = (1, 2);
%hash = ('a' => 1);
$regex = qr/foo.*bar/;
$reference = \%hash;
sub greet { print "Hello world!" };
$subref = \&greet;

$fmt = "%-11s is type: %s\n";
printf $fmt, '$scalar', detect(\$scalar);
printf $fmt, '@array', detect(\@array);
printf $fmt, '%hash', detect(\%hash);
printf $fmt, '$regex', detect( $regex);
printf $fmt, '$reference', detect(\$reference);
sub greet { print "Hello world!" };
printf $fmt, '$subref', detect( $subref);</lang>
{{out}}
<pre>$scalar is type: SCALAR
@array is type: ARRAY
%hash is type: HASH
$regex is type: Regexp
$reference is type: REF
$subref is type: CODE</pre>


=={{header|Perl 6}}==
=={{header|Perl 6}}==