Click to See Complete Forum and Search --> : quick perl q's


Fandelem
02-22-2001, 05:28 PM
i have written a little iplogger cgi script, but instead of cut&pasting it into every other cgi script to log the ip address, how can i "call" it? ie. in another script, something like "call(~/cgi-bin/iplog.cgi)" or something like that.. i don't know ;o)

and i've seen &subroutine and subroutine() - can someone explain to me the difference between the two calls? (i assume they both call the function?)

and finally, what's the smallest way any of you have made $date show the correct date(and time)? here is mine (it is lengthy, that's the reason i ask)


# this will figure out the date

my (
$sec,
$min,
$hour,
$mday,
$mon,
$year,
$wday,
$date,
$time,
@days,
@months,
);

# Define arrays for the day of the week and month of the year. #
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June' ,'July',
'August','September','October','November','Decembe r');

# Get the current time and format the hour, minutes and seconds. Add #
# 1900 to the year to get the full 4 digit year. #
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf("%02d.%02d.%02d",$hour,$min,$sec);
$year += 1900;

# Format the date. #
$date = "$days[$wday], $months[$mon] $mday, $year at $time";


and then from anywhere i can use $date

(i kinda modified/borrowed this from matt's formmail.pl script ;o) hehe

thanks for your replies.. =)

~kyle

lazy_cod3R
02-22-2001, 06:08 PM
i think what you can do is put the peice of code u want to use frequantly into a file say call it foo.pm ..
then put your subroutine in the file.



sub bar {
##code
}

1;


and dont forget to put the one on the last line of the file foo.pm .

then from the scripts you want to call this sub routine from say baz.pl

just do


require "foo.pm";
&bar;
#code


thats it

as for the differece between
&routine and routine() i wouldn't have any idea. all the books have taught me to do it using the &routine method.


as for the date i usually call the system command date with backticks ($date=`date`).
it might not give you the format you want it in tho but you can give it a try, i dont know if its the best way to do it but it works for me most of the time :)

[ 22 February 2001: Message edited by: lazy_cod3R ]

TheLinuxDuck
02-22-2001, 06:42 PM
One way to get the date without having to jump through hoops is to use scalar localtime:


#!/usr/bin/perl -w
use strict;
use warnings;
#
print "Time/date is: ", scalar localtime,"\n";
exit;


scalar localtime is what builds the string of the numeric time returned by localtime. This outputs:

/home/root/perl/fandelem> ./date.pl
Time/date is: Thu Feb 22 16:25:17 2001


You can then split it into an array and use the pieces as you wish for a print (or whatever), as:


#!/usr/bin/perl -w
use strict;
use warnings;
#
my(@date)=split(/\s/, scalar localtime);
print "Time/date is: ", $date[0], ", ",
$date[1], " ",
$date[2], ", ",
$date[4], " at ",
$date[3], "\n";
exit;


This just splits the segments by a space into an array.

Ran, this produces:

/home/root/perl/fandelem> ./date2.pl
Time/date is: Thu, Feb 22, 2001 at 16:25:22


Certainly makes for cleaner code. :)

Also, the difference of calling a subroutine with and without & has to do with two main things, of which I am aware.

1. Any subroutine's strict prototype can be bypassed. If you define a subroutine as:

sub convertScalarToFoo($);

It will only accept a single scalar when called. But, if you use &, you won't get the compile time error of mismatched prototype.

2. If @_ is defined in the block of code before the &subroutine is called, it will keep the existing values of @_, instead of localizing @_ to the subroutine call.

I hope that this helps you out!! :)