Skip to content

Instantly share code, notes, and snippets.

@ynonp
Last active August 29, 2015 14:06
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 ynonp/81890baad8078e3b9208 to your computer and use it in GitHub Desktop.
Save ynonp/81890baad8078e3b9208 to your computer and use it in GitHub Desktop.
myapp/
├── lib
│   ├── File
│   │   └── Slurp.pm
│   ├── MyModule.pm
│   └── Try
│   └── Tiny.pm
└── run.pl
3 directories, 4 files
use strict;
use warnings;
use v5.18;
use File::Slurp;
use File::Find;
my $count = 0;
sub wanted {
return if ! -f;
# return if $_ eq $0;
my $text = read_file($_);
if ( $text =~ /holy grail/ ) {
print "Match: $File::Find::name\n";
$count++;
}
}
find(\&wanted, '.');
print "Total: $count matches\n";
use strict;
use warnings;
use v5.18;
use File::Find::Rule;
my @results =
File::Find::Rule->file()
->name("*.txt")
->grep(qr/holy grail/)
->in('.');
print "Found: @results\n";
print "Total: ", scalar(@results), "\n";
use strict;
use warnings;
use v5.18;
use File::Find;
sub handle_file {
my $filename = $_;
return if ! -f;
my $sz = -s;
say "Large file found: $File::Find::name [$sz]"
if $sz > 500;
}
find(\&handle_file, '.');
use strict;
use warnings;
use v5.18;
use Test::More tests => 4;
use Person;
my $p = Person->new(name => "bob");
my $q = Person->new(name => "jane");
my $r = Person->new(name => "george", age => 19);
eval {
Person->new;
};
ok($@, "Can't create anonymous people");
$p->grow_up;
$p->grow_up;
$r->grow_up;
is($p->age, 2, "bob grew up twice");
is($q->age, 0, "jane is a newborn");
is($r->age, 20, "george is 20");
package Person;
use Moose;
use strict;
use warnings;
our $VERSION = '1.00';
has 'name', is => 'ro', required => 1;
has 'age', is => 'rw', default => 0;
#################################
sub grow_up {
my ( $self ) = @_;
my $new_age = $self->age() + 1;
$self->age($new_age);
}
1;
# __END__
# # Below is stub documentation for your module. You'd better edit it!
package Person;
use strict;
use warnings;
our $VERSION = '1.00';
sub new {
my ( $cls, $name, $age ) = @_;
$age ||= 0;
die "missing name" if ! $name;
my $self = { name => $name, age => $age };
bless $self, $cls;
}
sub age { shift->{age} }
sub grow_up {
my ( $self ) = @_;
$self->{age}++;
}
1;
# __END__
# # Below is stub documentation for your module. You'd better edit it!
package ReverseNum;
use strict;
use warnings;
use Mouse;
our $VERSION = '1.00';
has 'value', is => 'ro', isa => 'Str';
sub orig { shift->value }
sub rev { scalar reverse shift->value }
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment