-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassessment.pl
More file actions
128 lines (108 loc) · 3.34 KB
/
Copy pathassessment.pl
File metadata and controls
128 lines (108 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
sub perl_is_awesome {
# Implicit variable scoping
$implicit_var = 10;
say "Implicit variable: $implicit_var";
{
$implicit_var = "Now I'm a string!";
say "Implicit variable changed: $implicit_var";
}
say "Implicit variable outside block: $implicit_var";
# Type coercion issues
my $num = 10;
my $str = "5";
my $coerced_result = $num + $str; # Numeric context
say "Coerced result (numeric context): $coerced_result";
$coerced_result = $num . $str; # String context
say "Coerced result (string context): $coerced_result";
# Context-sensitive behavior
my @array = (1, 2, 3);
my $scalar = @array; # Scalar context
say "Array in scalar context: $scalar";
my @new_array = @array; # List context
say "Array in list context: @new_array";
# Regex pitfalls
my $text = "Perl is awesome!";
if ($text =~ /awesome/) {
say "Regex matched!";
}
my $number = "1234";
if ($number =~ /(\d+)/) {
say "Captured number: $1";
}
# Signal handling issues
$SIG{INT} = sub {
say "Caught SIGINT signal";
exit 1;
};
# Use of eval
my $code = 'say "Eval code executed!"';
eval $code;
if ($@) {
say "Eval failed: $@";
}
# Bareword filehandles
open(FH, '>', 'output.txt') or die "Could not open file: $!";
print FH "Writing to a file using bareword filehandle\n";
close(FH);
# Implicit returns
say "Implicit return example: ", implicit_return();
# Variable shadowing
my $var = 42;
{
my $var = "Shadowed variable";
say "Inside block: $var";
}
say "Outside block: $var";
# Autovivification pitfalls
my %hash;
$hash{foo}{bar} = 1;
say "Autovivified hash: ", $hash{foo}{bar};
# Using the `goto` statement
my $count = 3;
say "Using goto statement:";
COUNTDOWN: while ($count) {
say $count--;
goto COUNTDOWN if $count > 0;
}
# JAPH example (Just Another Perl Hacker)
$_='987;s/^(\d{1,3})/chr $1+114/e;eval';eval;print
# Obfuscated code
my $obfuscated = q{
sub O{my $o=shift; $o=~s/(.)(.*)/$2$1/;return$o;} sub S{my $s=shift;
$s=~tr/a-zA-Z/n-za-mN-ZA-M/;return$s;} sub U{my $u=shift; $u=~s/^(.)/$1$1/;return$u;}
my $x="rtebl az gn!";
for my $fn (qw(O S U)) { $x = $fn->($x); }
say "Obfuscated code: $x";
};
eval $obfuscated;
# Misusing globals and dynamic variables
local $/; # Slurp mode
open my $fh, '<', $0 or die $!;
my $code = <$fh>;
close $fh;
say "Read entire script using global variable \$/: $code";
# Unintended list flattening
my @list1 = (1, 2, 3);
my @list2 = (4, 5, 6);
my @flattened = (@list1, @list2);
say "Flattened list: @flattened";
# Obscure regular expression
my $obscure_re = 's/([^\W_])([^\W_]*)/$2$1ay/g';
my $obscure_text = "Pig Latin is fun";
eval "\$obscure_text =~ $obscure_re";
say "Obscure regex: $obscure_text";
# Variable aliasing with typeglobs
*foo = \$var;
$foo = "Variable aliasing with typeglobs";
say "Variable aliasing: $var";
say "End of wildly awesome function.";
}
sub implicit_return {
my $value = "Implicit return value";
$value; # No explicit return statement
}
perl_is_awesome();