OOP 펄에서의 멤버함수 참조 이용할려면

mr.lee의 이미지

제가 할려는것은 이렇습니다.

패키지(객체) 함수안에서 자기 패키지, 혹은 다른 패키지의 멤버함수의 참조를 로컬 변수에 대입하고, 그것을 콜하게 할려고 합니다.

단, 이때 콜된 함수에서는 각각 자기 객체의 bless된 self 해쉬멤버를 참조할 수 있어야 합니다.

이를테면 아래와 같은것을 할려고 합니다. 어떻게 하면 될련지요.
웬만한 방법은 다 써봐도 잘 안되는군요.

아래코드는 $self 객체를 받지 못하여 원하는 결과가 나오지 않습니다.

use strict;

{
 package Class1;

 sub new
 {
   my $class = shift;
   my $self = {
    a => "Class1",
   };
   bless($self, $class);
   return $self;
 }

 sub fun1
 {
  my $self = shift;
  my ($arg) = @_;
  my $a = $self->{a};
  print("$a: $arg\n");
 }

}

{
 package Class2;

 sub new
 {
   my $class = shift;
   my $self = {
    a => "Class2",
    c => Class1->new(),
   };
   bless($self, $class);
   return $self;
 }

 sub fun2
 {
  my $self = shift;
  my ($arg) = @_;
  my $a = $self->{a};
  print("$a: $arg\n");
 }

 sub main
 {
  my $self = shift;
  my $ref1 = \&Class1::fun1;
  my $ref2 = \&fun2;
  
  $ref1->("ref1");
  $ref2->("ref2");
 }

}

Class2->new()->main();
mr.lee의 이미지

자답입니다. 일반적으로 함수 참조를 콜할때는 $funcRef->(argument) 머 이런식이어야 하는데, 그런식으로는 에러나고..여튼 아래 2가지 방법으로 가능하네요. use strict 에서도 오류나지 않습니다.
위 코드에서 Class2::main 부분입니다.

sub main
{
  my $self = shift;
  my $ref1;
  my $ref2;

  $ref1 = \&Class1::fun1;
  $ref2 = \&fun2;
  $self->{c}->$ref1;
  $self->$ref2;

  $ref1 = \&{$self->{c}->fun1};
  $ref2 = \&{$self->fun2};
  $ref1;
  $ref2;
}
mr.lee의 이미지

인자를 넘겨야 하기 때문에... 여러 테스트를 해보았지만 $funRef->(arg) 나 $self->funRef->(arg) 머 이런식의 호출은 힘드네요. 위에서 두번째 방법으로는 아무 인자도 전달하기가 어렵고..첫번째 방법에서 객체를 인자로 넘길수 밖에 없군요.

sub main
 {
  my $self = shift;
  my $ref1;
  my $ref2;

  $ref1 = \&Class1::fun1;
  $ref2 = \&fun2;
  $ref1->($self->{c}, "ref1");
  $ref2->($self, "ref2");
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.