blog/content/post/2009/08/10/2009-08-10-00001214.md

65 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: ref関数 何のリファレンスなのかを判断する
author: kazu634
date: 2009-08-10
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4751;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
---
<div class="section">
<p>
リファレンスをでリファレンスしたものが、
</p>
<ul>
<li>
スカラー変数
</li>
<li>
配列
</li>
<li>
ハッシュ
</li>
</ul>
<p>
なのかを判断する関数がrefです。
</p>
<h4>
使い方
</h4>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">use </span>Perl6::Say;
<span class="synStatement">use utf8</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$foo</span> = <span class="synConstant">10</span>;
<span class="synStatement">my</span> <span class="synIdentifier">@array</span> = ( <span class="synConstant">1</span>, <span class="synConstant">2</span>, <span class="synConstant">3</span>, <span class="synConstant">4</span>, <span class="synConstant">5</span> );
<span class="synStatement">my</span> <span class="synIdentifier">$ref_foo</span> = <span class="synIdentifier">\$foo</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$ref_array</span> = <span class="synIdentifier">\@array</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$ref_hash</span> = {
<span class="synConstant">&#34;key&#34;</span> =&#62; <span class="synConstant">13</span>,
<span class="synConstant">&#34;foo&#34;</span> =&#62; <span class="synConstant">15</span>,
};
say <span class="synStatement">ref</span>(<span class="synIdentifier">$ref_foo</span>);
say <span class="synStatement">ref</span>(<span class="synIdentifier">$ref_array</span>);
say <span class="synStatement">ref</span>(<span class="synIdentifier">$ref_hash</span>);
</pre>
<h4>
実行例
</h4>
<pre class="syntax-highlight">
~/working/tmp_perl on simoom634 <span class="synStatement">[</span><span class="synConstant">506</span><span class="synStatement">]</span> $: perl <span class="synStatement">test</span>.pl
SCALAR
ARRAY
HASH
</pre>
</div>