Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help With My Sub

Status
Not open for further replies.

mtorbin

Technical User
Nov 5, 2002
369
US
Hey all,

Here is a sub I wrote:

sub createDateStamp {
($second,$minute,$hour,$day,$month,$year,$weekday,$dayOfYear,$isDST)=localtime(time);

my $realMonth = $month+1;
my $realYear;
my $localDate;
my $localTime;

if($realMonth < 10) {
$realMonth = "0" . $realMonth;
}

if($day < 10) {
$day = "0" . $day;
}

if($year <= 99) {
$realYear = "19" . $year;
}
else {
$realYear = "20" . ($year - 100);
}

$localDate = $realMonth . '/' . $day . '/' $realYear;

$localTime = $hour . ':' . $minute . ':' . $second;
}

Quick question. First in reference to "=localtime(time);", what is the "(time)" part? Is that a variable delcaration?

Second, how do I get it to return the values for $localDate and $localTime?

Thanks,

- MT
 
Hi

First, do not try to guess, read [tt]man perlfunc[/tt]. [tt]time[/tt] is a function : "Returns the number of non-leap seconds since what­ ever time the system considers to be the epoch".

Second, what do you want to return ? The sub can return only one thing and you have two values. Maybe you want to return them in an array. Put this as last line in the sub :
Code:
return ($localDate,$localTime);

Feherke.
 
So what would be the name of the array? How would I reference this?

- MT
 
Hi

There will be no name inside the sub. Outside, whatever you want :

Code:
@dt=&createDateStamp;
print $dt[0];          [gray]# print the date[/gray]
print $dt[1];          [gray]# print the time[/gray]
print join "--",@dt;   [gray]# print date and time separated by "--"[/gray]

Feherke.
 
mtorbin,

I'm assuming you are asking this in the context of your js post..

Here's an example of getting Perl to interact with the onclick event.

The .html page
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
<head>
<title>XML Request</title>
<script type="text/javascript">
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}
function getTime() {
var url = "cgi-bin/tektipsperl.pl";
xmlhttp.open("GET", url);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   alert(xmlhttp.responseText); }
   } 
xmlhttp.send(null);
}

</script>

</head>
<body>
<form name="f1">
<input type="button" value="Get Time" OnClick="getTime();">
</form>

</body></html>

the .pl
Code:
use strict;

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print header;


sub createDateStamp {
my($second,$minute,$hour,$day,$month,$year,$weekday,$dayOfYear,$isDST)=localtime(time);

my $realMonth = $month+1;
my $realYear;
my $localDate;
my $localTime;

if($realMonth < 10) {
$realMonth = "0" . $realMonth;
}

if($day < 10) {
$day = "0" . $day;
}

if($second < 10){
$second = "0" . $second;
}

if($year <= 99) {
$realYear = "19" . $year;
}
else {
$realYear = "200" . ($year - 100);
}

$localDate = $realMonth . '/' . $day . '/' . $realYear;

$localTime = $hour . ':' . $minute . ':' . $second;
return "The Star Date is $localDate , $localTime.";
}

print createDateStamp();

This example comes off my XP, you may need to make adjustments.

For anyone else that is interested:

Thanks,
 
Hi

Another sentence from the [tt]man[/tt] page : "$year is the number of years since 1900". And beside this, that [tt]if($day < 10)[/tt] looks strange. My approach for this :

Code:
sub shorterDateTime
{
  @d=localtime; $d[5]+=1900; $d[4]++;
  (sprintf("%02d/%02d/%04d",@d[4,3,5]),sprintf "%02d:%02d:%02d",@d[2,1,0]);
}

Feherke.
 
Thanks guys.... Ah, I love learning Perl... doing at work and reading at home... my head hurts every night but it's worth it.

- MT
 
Quick question. First in reference to "=localtime(time);", what is the "(time)" part? Is that a variable delcaration?

"time" is the argument you are passing to the localtime() function. The () after a function is the method perl (and many languages) use to pass arguments (or data) to functions and sub routines. Note that "time" is a function too, but it takes no arguments: it's sole purpose is to return the number of seconds since the epoch.
 
So when I need to use the sub in an external file, do I have to call it in any way such as you do in JS? How do I let the current script know where the sub is located if it's not in the same file?

- MT
 
Hi

Could be abit different depending on how you include the external file which contains the sub. For a short test :

--- main.pl : ---
Code:
#! /usr/bin/perl
sub mysub
{
  print "sub in main\n";
}
mysub;
use module;          [gray]# let only one of this three[/gray]
[gray]#require module;      # lines uncommented, then run[/gray]
[gray]#import module;       # the script and see the output[/gray]
mysub;

--- module.pm : ---
Code:
sub mysub
{
  print "sub in module\n";
}
1;

But generally the answer would be yes, it will know from where to take the sub. Eventually take a look at the [tt]package[/tt] statement.

Feherke.
 
I'm sorry, but I just don't follow. Where in main.pl did you point to the file?

- MT
 
Hi

The declaration of the sub [tt]mysub[/tt] is in the file module.pm, which is included in main.pl.

There are several ways to include it ( see the three lines with comment in the source ) :
[ul]
[li]use module;[/li]
[li]require module;[/li]
[li]import module;[/li]
[/ul]

I suggested to comment two of that an leave only one uncommented, then run the main.pl script. Repeat this three times, uncommenting one inclusion method in turn. Finally compare the three outputs. On my machine the outputs are :

When the uncommented line is [tt]use module.pl;
sub in module
sub in module
[/tt]
When the uncommented line is [tt]require module.pl;
sub in main
sub in module
[/tt]
When the uncommented line is [tt]import module.pl;
sub in main
sub in main
[/tt]

Feherke.
 
OK, here's the final code for the perLib.pl:

#!/usr/bin/perl
## Perl Library
## Copyright 2005 PEI-Genesis
## Written and developed by Matt Torbin
## Made Using The Awesome Power Of A Macintosh

use strict;

use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);

print header;

sub createDate {
($second,$minute,$hour,$day,$month,$year,$weekday,$dayOfYear,$isDST)=localtime(time);

my $realMonth = $month+1;
my $realYear;
my $localDate;
my $localTime;

if($realMonth < 10) {
$realMonth = "0" . $realMonth;
}

if($day < 10) {
$day = "0" . $day;
}

if($year <= 99) {
$realYear = "19" . $year;
}
else {
$realYear = "20" . ($year - 100);
}

$localDate = $realMonth . '/' . $day . '/' $realYear;

print $localDate;
}


sub createTime {
($second,$minute,$hour,$day,$month,$year,$weekday,$dayOfYear,$isDST)=localtime(time);
my $localTime;
$localTime = $hour . ':' . $minute . ':' . $second;

print $localTime;
}


And here's the errors:

Premature end of script headers: fileDownload.cgi, referer: fileDownload.cgi: Scalar found where operator expected at /var/ line 37, near "'/' $realYear", referer: Global symbol "$second" requires explicit package name at /var/ line 15., referer: Global symbol "$minute" requires explicit package name at /var/ line 15., referer: Global symbol "$hour" requires explicit package name at /var/ line 15., referer: Global symbol "$day" requires explicit package name at /var/ line 15., referer: Global symbol "$month" requires explicit package name at /var/ line 15., referer: Global symbol "$year" requires explicit package name at /var/ line 15., referer: Global symbol "$weekday" requires explicit package name at /var/ line 15., referer: Global symbol "$dayOfYear" requires explicit package name at /var/ line 15., referer: Global symbol "$isDST" requires explicit package name at /var/ line 15., referer: Global symbol "$month" requires explicit package name at /var/ line 17., referer: Global symbol "$day" requires explicit package name at /var/ line 26., referer: Global symbol "$day" requires explicit package name at /var/ line 27., referer: Global symbol "$day" requires explicit package name at /var/ line 27., referer: Global symbol "$year" requires explicit package name at /var/ line 30., referer: Global symbol "$year" requires explicit package name at /var/ line 31., referer: Global symbol "$year" requires explicit package name at /var/ line 34., referer: Global symbol "$day" requires explicit package name at /var/ line 37., referer: /var/ has too many errors., referer: Compilation failed in require at /var/ line 7., referer:


Woah! What the heck happened here??

- MT
 
you don't have

1;

as the last line of code and the module is not returning a true value otherwise. You are also using "strict", which you should, but not declaring all the variables with "my" as you should.

See post on other forum where you asked this question.
 
Hi

The string concatenation operator is missing here :
Code:
$localDate = $realMonth . '/' . $day . '/' [red][b].[/b][/red] $realYear;

By the way, I prefer it this way :
Code:
$localDate = "$realMonth/$day/$realYear";

And that [tt]$realYear[/tt] calculation is completely wrong. Please read man perlfunc :
man perlfunc said:
Note that the $year element is not simply the last
two digits of the year. If you assume it is, then
you create non-Y2K-compliant programs--and you
wouldn't want to do that, would you?

The proper way to get a complete 4-digit year is
simply:

$year += 1900;

And to get the last two digits of the year (e.g.,
'01' in 2001) do:

$year = sprintf("%02d", $year % 100);

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top