williey
Technical User
- Jan 21, 2004
- 242
How do you create generic subroutines that are reusable in different scripts? I tried using "require" but it does not work out.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
use strict;
use warnings;
use DBI;
# Subroutine : get_todays_date.pl
#
# Syntax : get_todays_date(format_type) where format_type correlate to the
# below type code.
#
# Description: return today's date in various formats.
# new format can be added when needed.
#
#
# type format
# ---- ------
# 1 YYMMDD
# 2 MONTH-DD-YYYY
#
#
#
#
require "test2.pl";
my $todays_date;
$todays_date = get_todays_date(1);
print($todays_date);
$todays_date = get_todays_date(1);
print($todays_date);
sub get_todays_date
{
my ($date_format_type) = @_;
my $return_date;
# Date and time definition #
my ($second, $minute, $hour, $day, $month, $year, $weekday, $dayofyear, $isdst)=localtime;
my $yyyy = $year + 1900;
my $yy = substr($yyyy, 2,2);
my $mm = $month;
my $dd = $day;
my @months = ("January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December");
if ($mm < 10)
{
$mm = "0" . $mm;
}
if ($day < 10)
{
$dd = "0" . $day;
}
if ($date_format_type == 1)
{
$return_date = $yy . $mm . $ dd;
return $return_date;
}
elsif ($date_format_type == 2)
{
$return_date = uc(substr($months[$mm - 1], 0, 3)) . "-" . $dd . "-" . $yyyy;
return $return_date;
}
}
package globalFunctions;
use Exporter;
@ISA = ('Exporter');
@EXPORT = qw($string @array %hash &function);
$string = "hello, global string.";
@array = (1,2,3,4);
%hash = (
"A" => "B",
);
sub function {
print "this is my function that can be used anywhere.";
}
1;
# Subroutine : get_todays_date.pl
#
# Syntax : get_todays_date(format_type) where format_type correlate to the
# below type code.
#
# Description: return today's date in various formats.
# new format can be added when needed.
#
#
# type format
# ---- ------
# 1 YYMMDD
# 2 MONTH-DD-YYYY
#
#
#
#
my $todays_date;
$todays_date = get_todays_date(1);
print($todays_date);
$todays_date = get_todays_date(1);
print($todays_date);
package dateFunctions;
use Exporter
@ISA = ('Exporter');
@EXPORT = qw(&get_todays_date);
sub get_todays_date
{
$date_format_type = @_;
$return_date;
# Date and time definition #
($second, $minute, $hour, $day, $month, $year, $weekday, $dayofyear, $isdst)=localtime;
$yyyy = $year + 1900;
$yy = substr($yyyy, 2,2);
$mm = $month;
$dd = $day;
@months = ("January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December");
if ($mm < 10)
{
$mm = "0" . $mm;
}
if ($day < 10)
{
$dd = "0" . $day;
}
if ($date_format_type == 1)
{
$return_date = $yy . $mm . $ dd;
return $return_date;
}
elsif ($date_format_type == 2)
{
$return_date = uc(substr($months[$mm - 1], 0, 3)) . "-" . $dd . "-" . $yyyy;
return $return_date;
}
}
1;
package dateFunctions;
use Exporter;
@ISA = ('Exporter');
@EXPORT = qw(&get_todays_date);
sub get_todays_date
{
$date_format_type = @_;
$return_date; <------- line 13
# Date and time definition #
($second, $minute, $hour, $day, $month, $year, $weekday, $dayofyear, $isdst)=localtime;
$yyyy = $year + 1900;
$yy = substr($yyyy, 2,2);
$mm = $month;
$dd = $day;
@months = ("January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December");
if ($mm < 10)
{
$mm = "0" . $mm;
}
if ($day < 10)
{
$dd = "0" . $day;
}
if ($date_format_type == 1)
{
$return_date = $yy . $mm . $ dd;
return $return_date;
}
elsif ($date_format_type == 2)
{
$return_date = uc(substr($months[$mm - 1] , 0, 3)) . "-" . $dd . "-" . $yyyy;
return $return_date;
}
}
1;
sub get_todays_date
{
my ($date_format_type) = @_;
my $return_date;
# Date and time definition #
my ($second, $minute, $hour, $day, $month, $year, $weekday, $dayofyear, $isdst)=localtime;
my $yyyy = $year + 1900;
my $yy = substr($yyyy, 2,2);
[b]my $mm = $month + 1;[/b]
my $dd = $day;
my @months = ([b]undef,[/b]
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December");
if ($mm < 10)
{
$mm = "0" . $mm;
}
if ($day < 10)
{
$dd = "0" . $day;
}
if ($date_format_type == 1)
{
$return_date = $yy . $mm . $ dd;
return $return_date;
}
elsif ($date_format_type == 2)
{
$return_date = uc(substr($months[[b]$mm[/b]], 0, 3)) . "-" . $dd . "-" . $yyyy;
return $return_date;
}
}
[b]1;[/b]
$todays_date = get_todays_date(1);
print($todays_date, [b]"\n"[/b]);
$todays_date = get_todays_date(2);
print($todays_date, [b]"\n"[/b]);