LoginSignup
5
6

More than 5 years have passed since last update.

Perlで参照渡し

Posted at

参照渡しの記事を見たので便乗。
Perlにおいて参照渡しは以下のように\を付けてリファレンスとして渡します。
そうするとサブルーチンを抜けた後は変数が書き換えられて出てきます。

subroutine.pl
#!/usr/bin/perl
use strict;
use warnings;

my $foo = 0;

unless (bar(\$foo)) {  # リファレンスで渡す
  die "bar failed";
}

print "$foo\n";

sub bar
{
  my ($baz) = @_;
  $$baz = "hogehoge";  # リファレンスを参照して代入
  return 1;
}
5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6