CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
subfunction return (true/false)

 



hoptar
Novice

Nov 5, 2007, 1:56 PM

Post #1 of 16 (1596 views)
subfunction return (true/false) Can't Post

Hi Guys, this is really basic, but after 3 hours or research, I still can't get it right..

I have simple sub called "checkdate" in file1. checkdate takes 2 arguments: filename1, filename2, and should return true/false depending on which file is older.

This subfunction, needs to be called from various other files (let's say file 2 for now.) Based on the return value, the script in file2 will either continue or die. But the "return" value always appears to be the "0" . I added it below the subfunction to make it "return a true value" How do I go about getting the "real" return value in file2 without getting the error? Thanks for the help.




File1:
sub check_date ## Begin Check_date.

{
chomp @ARGV;
my @FN0 = split('\.',$ARGV[0]);
my @FN1 = split('\.',$ARGV[1]);

...
...

$D0 = $FN0[1];
$D1 = $FN0[1];


# if ($D1 > $D0) {
# return 1;
# print "D1 greater than D0\n";
# }
# else {
# return 0;
# print "D0 greater than D1\n";
# }

return ($D1 > $D0);
} ## End Check_date
1


File2:
#! /usr/bin/perl
require 'checkdate.pl';

$A = &check_date ("abcd.041101.gz" , "wxyz.061001.gz");
if ($A) {
print "True\n";
}
else {
die ("Existing file is the newer version.");
}


(This post was edited by hoptar on Nov 6, 2007, 9:27 AM)


MorayJ
Novice

Nov 6, 2007, 6:26 AM

Post #2 of 16 (1585 views)
Re: [hoptar] subfunction return (true/false) [In reply to] Can't Post

Hi,

I'm by no means an expert, but I believe the problem is this.

Subroutines that are treated as libraries must return a true value. That is what the "1" at the very end of the file is for - to make the sub return a true value so that it will not fail at compile time.

I would suggest that you have a variable called $true, or something, and conditionally assign that with a 1 or a zero and return that for use in the programme that called the sub.

I *think* this should do the trick. Let me know.

Cheers

MorayJ


KevinR
Veteran


Nov 6, 2007, 8:00 AM

Post #3 of 16 (1583 views)
Re: [hoptar] subfunction return (true/false) [In reply to] Can't Post

your subroutine is returning a 0 or 1 depending on what the return function expression evaluates to:

return ($D1 > $D0);

if $D1 is greater than $D0 it will return a 1 (true), if not it will return zero (false) .
-------------------------------------------------


rgkrishnas
Novice

Nov 6, 2007, 8:42 AM

Post #4 of 16 (1581 views)
Re: [KevinR] subfunction return (true/false) [In reply to] Can't Post

Your return statement should be like this

return ($D1 > $D0)? 1 : 0;




KevinR
Veteran


Nov 6, 2007, 9:06 AM

Post #5 of 16 (1576 views)
Re: [rgkrishnas] subfunction return (true/false) [In reply to] Can't Post

Actually that is a good suggestion. My answers was wrong. return() returns undef in scalar context so:

return ($D1 > $D0);

will return 1 or undef (if scalar context), not 1 or 0.
-------------------------------------------------


hoptar
Novice

Nov 6, 2007, 9:25 AM

Post #6 of 16 (1572 views)
Re: [rgkrishnas] subfunction return (true/false) [In reply to] Can't Post

Thanks rgkrishnas, I tried return ($D1 > $D0)? 1 : 0; but the output is still always "false" regardless of the filenames.

KevinR, I understand your explanation-- that was exactly what I intended, but it actually isn't behaving that way..

Thanks MorayJ, I tried assigning a variable called $X the values "1" or "0" and returning $X, but it still appears to return "0" consistently.

any ideas? If you run the code, you will see what I mean. It's really frustrating.


hoptar
Novice

Nov 6, 2007, 9:33 AM

Post #7 of 16 (1570 views)
Re: [hoptar] subfunction return (true/false) [In reply to] Can't Post

This is all I am trying to work with now:




checkdate.pl:

#/usr/bin/perl
sub check_date ## Begin Check_date.
{
my @FN0 = split('\.',$ARGV[0]);
my @FN1 = split('\.',$ARGV[1]);

$D0 = $FN0[1];
$D1 = $FN1[1];

return ($D1 > $D0)? 1 : 0;

} ## End Check_date
1





test.pl:

#! /usr/bin/perl
require 'checkdate.pl';

$A = &check_date("abcd.041031.gz" , "abcd.071030.gz");
print "$A\n";

if ($A) {
print "This is True\n";
}
else {
die ("Old file is newer than the one on the server");
}


(This post was edited by hoptar on Nov 6, 2007, 9:55 AM)


KevinR
Veteran


Nov 6, 2007, 4:39 PM

Post #8 of 16 (1560 views)
Re: [hoptar] subfunction return (true/false) [In reply to] Can't Post

Thats not the correct way to check if one file is newer/older. You should use the stat() function or the -M file test operator.

The '>' (greater than) operator is a math operator but you are checking strings. Are the numbers in the file a date? If so what is the format?
-------------------------------------------------


hoptar
Novice

Nov 6, 2007, 6:03 PM

Post #9 of 16 (1557 views)
Re: [KevinR] subfunction return (true/false) [In reply to] Can't Post

Hi KevinR,
Yes, the filenames have the "date" stamped in them.
eg. update.071106.gz for today. i.e. update.yymmdd.gz

So comparing the yymmdd's seemed to be a good idea to me because the numbers will always be larger as the dates progress.

Comparing with stat wouldn't exactly work for me because the file timestamps would change when they're uploaded, moved between servers, etc. But the filename would always contain the correct patch date.

Do you think the comparison is failing because they're being interpreted as strings?


rgkrishnas
Novice

Nov 7, 2007, 1:17 AM

Post #10 of 16 (1551 views)
Re: [hoptar] subfunction return (true/false) [In reply to] Can't Post

Hi

Your code was wrong in the in the subroutine

It should be like this

my @FN0 = split('\.',shift);
my @FN1 = split('\.',shift);

and not like this

my @FN0 = split('\.',$ARGV[0]);
my @FN1 = split('\.',$ARGV[1]);

I hope your problem has solved




KevinR
Veteran


Nov 7, 2007, 9:16 AM

Post #11 of 16 (1544 views)
Re: [hoptar] subfunction return (true/false) [In reply to] Can't Post

it will work if you make the change rgkrishnas has noted.
-------------------------------------------------


hoptar
Novice

Nov 9, 2007, 2:13 PM

Post #12 of 16 (1535 views)
Re: [KevinR] subfunction return (true/false) [In reply to] Can't Post

Hmm.. This is still always returning a "false"
Either way, I don't understand why using $ARG[0] is wrong.

Thanks anyway.


hoptar
Novice

Nov 9, 2007, 2:41 PM

Post #13 of 16 (1533 views)
Re: [hoptar] subfunction return (true/false) [In reply to] Can't Post

Did you guys run that code and get correct results?
This is what I have.. still returning a "false" everytime, no matter what you make the dates:

checkdate.pl:
#! /usr/bin/perl
sub check_date {
@FN0 = split(".", shift);
@FN1 = split(".", shift);
$D0 = $FN0[1];
$D1 = $FN1[1];
return ($D1 > $D0)? 1:0;
}
1

test.pl:
#! /usr/bin/perl
require 'checkdate.pl';
$RETURN = &check_date("abcd.100000.gz", "abcd.200000.gz");
print "$RETURN\n";


KevinR
Veteran


Nov 9, 2007, 10:05 PM

Post #14 of 16 (1531 views)
Re: [hoptar] subfunction return (true/false) [In reply to] Can't Post

why did you change the regexp?

split(".", shift);

it should be:

split(/\./, shift);

or:

split('\.', shift);
-------------------------------------------------


hoptar
Novice

Nov 12, 2007, 8:41 AM

Post #15 of 16 (1521 views)
Re: [KevinR] subfunction return (true/false) [In reply to] Can't Post

The result is the same in all cases:

split(".", shift);
split(/\./, shift);
split('\.', shift);

It always returns a "0" for False.


hoptar
Novice

Nov 12, 2007, 1:36 PM

Post #16 of 16 (1516 views)
Re: [hoptar] subfunction return (true/false) [In reply to] Can't Post

Nevermind, I got it to work saving the file as a module, and using the export module, along with rgkrishna's correction.

Thank you all.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives