Skip to content

Instantly share code, notes, and snippets.

@Kuniwak
Created July 15, 2014 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kuniwak/fb6b2d353b2133216bcf to your computer and use it in GitHub Desktop.
Save Kuniwak/fb6b2d353b2133216bcf to your computer and use it in GitHub Desktop.
PerlQuizの答え

Quiz1

#!/usr/bin/env perl
use strict;
use warnings;

print 1;

BEGIN {
    print 2;
}

print 3;

答え

213

Quiz2

#!/usr/bin/env perl
use strict;
use warnings;

my $var = 'foo';

BEGIN {
    print $var; 
}

答え

Use of uninitialized value $var in print at /Users/yuki.kokubun/Desktop/perl-quiz/quiz1/quiz1.pl line 8.

Quiz3

#!/usr/bin/env perl
use strict;
use warnings;

BEGIN {
    print $var; 
}

my $var = 'foo';

答え

Global symbol "$var" requires explicit package name at /Users/yuki.kokubun/Desktop/perl-quiz/quiz1/quiz1.pl line 6.
BEGIN not safe after errors--compilation aborted at /Users/yuki.kokubun/Desktop/perl-quiz/quiz1/quiz1.pl line 7.

Quiz4

#!/usr/bin/env perl
use strict;
use warnings;

my $var;

BEGIN {
    $var = 'foo';
}

print $var; 

答え

foo

Quiz5

#!/usr/bin/env perl
use strict;
use warnings;

use Foo;

BEGIN {
    print "main BEGIN\n";
}

print "main\n";

use Bar;
package Foo;
use strict;
use warnings;

print "Foo\n";

BEGIN {
    print "Foo BEGIN\n";
}

1;
package Bar;
use strict;
use warnings;

print "Bar\n";

BEGIN {
    print "Bar BEGIN\n";
}

1;

答え

Foo BEGIN
Foo
main BEGIN
Bar BEGIN
Bar
main

Quiz6

package Foo;
use strict;
use warnings;

use Bar qw/BAR/;

sub say {
    print BAR;
    print Bar::BAR;
}

1;
package Bar;
use strict;
use warnings;
use Exporter qw/import/;

our @EXPORT_OK = qw/BAR/;

use constant BAR => 'hello!'; # use constant BAR パターン
sub BAR { 'hello!' }          # sub BAR パターン
sub BAR () { 'hello!' }       # sub BAR () パターン

1;
#! /usr/bin/env perl
use strict;
use warnings;

use Test::MockModule;

my $mocked_bar = Test::MockModule->new('Bar');
$mocked_bar->mock('BAR', sub () { 'bye!' }); # use constant BAR パターンまたは sub BAR () パターン
$mocked_bar->mock('BAR', sub { 'bye!' });    # sub BAR パターン

use Foo;     # use Foo パターン
require Foo; # require Foo パターン
Foo::say();

答え

use constant BAR パターン sub BAR パターン sub BAR () パターン
use Foo パターン hello!hello! hello!bye! hello!hello!
require Foo パターン bye!bye! bye!bye! bye!bye!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment