# this is bad...
#
sub do_something {
if ( $MyApp::VERSION < 1.0 ) { # a global
do_something_simple();
}
else {
do_something_complex();
}
}
do_something();
# this is good...
#
sub do_something {
my ($version) = @_;
if ( $version < 1.0 ) { # a local variable
do_something_simple();
}
else {
do_something_complex();
}
}
do_something(1.2);
####
use v5.14;
use warnings;
use DBI ();
package MyApp {
sub do_something {
my ($version, $dbh, $output_file) = @_;
if ( $version < 1.0 ) { # a local variable
do_something_simple($output_file);
}
else {
do_something_complex($output_file, $dbh);
}
}
sub do_something_simple {
my ($output_file) = @_;
open my $fh, '>', $output_file or die;
print $fh "Hello world\n";
}
sub do_something_complex {
my ($output_file, $dbh) = @_;
open my $fh, '>', $output_file or die;
print $fh "Hello world\n";
printf $fh "There are %d rows in the table.\n",
$dbh->do("SELECT 1 FROM my_table");
}
}
MyApp::do_something(
1.2,
DBI->connect("dbi:SQLite:dbname=myapp.sqlite"),
'/tmp/output.txt',
);
####
use v5.14;
use warnings;
use DBI ();
package MyApp {
use Class::Tiny qw( version dbh output_file );
sub do_something {
my $self = shift;
if ( $self->version < 1.0 ) { # an object attribute
$self->do_something_simple;
}
else {
$self->do_something_complex;
}
}
sub do_something_simple {
my $self = shift;
open my $fh, '>', $self->output_file or die;
print $fh "Hello world\n";
close $fh;
}
sub do_something_complex {
my $self = shift;
open my $fh, '>', $self->output_file or die;
print $fh "Hello world\n";
printf $fh "There are %d rows in the table.\n",
$self->dbh->do("SELECT 1 FROM my_table");
close $fh;
}
}
my $app = MyApp->new(
version => 1.2,
dbh => DBI->connect("dbi:SQLite:dbname=myapp.sqlite"),
output_file => '/tmp/output.txt',
);
$app->do_something;