Re: Using RETURN in a sub
by b10m (Vicar) on Feb 19, 2004 at 16:31 UTC
|
Please be a little more specific on the "but it doesnt return
anything" part, cause this code runs smoothly on my machine. Maybe a
stupid question, but do you actually do something with the returned
value? Like:
print makeDate();
sub makeDate{
@days = ("Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
@mons = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct",
+"Nov","Dec");
(undef,undef,undef,$mday,$mon,$year,$wday,undef,undef) = localtime(tim
+e);
$year = 2000 + ($year - 100);
$date = "$days[$wday] $mons[$mon] $mday $year";
return $date;
}
--
b10m
All code is usually tested, but rarely trusted.
| [reply] [d/l] |
|
|
ok, maybe i should try using print for it. i refer to it as
&makeDate;
im fairly new to sub's so, it is my error, i apologize. | [reply] [d/l] |
|
|
That will work too, but you have to understand the code in the sub. You call the subroutine (with &makeDate) and that routine does something (creating a date in this case). After the string has been created, you say "Alrighty! Let's have that result back!". And so the sub does. But ermm ... then nothing happens.
As demonstrated by me (and others), you can call the sub to directly print the result it's handing back, but of course, you can use that value later. For example:
sub makeDate{
@days = ("Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
@mons = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept",
"Oct","Nov","Dec");
(undef,undef,undef,$mday,$mon,$year,$wday,undef,undef) = localtime(
+time);
$year = 2000 + ($year - 100);
$date = "$days[$wday] $mons[$mon] $mday $year";
return $date;
}
$date = makeDate();
# ... do a lot of other things ...
print "<p>Hello, currently it's $date!</p>\n";
One more word of advice, please read perldoc strict, 'cause it will probably help you out big time later on. Might as well do it right from the beginning :)
im fairly new to sub's so, it is my error, i apologize.
We all started with Perl once and we all are here (except for maybe some monks ;) to learn something.
--
b10m
All code is usually tested, but rarely trusted.
| [reply] [d/l] |
|
|
I want to thank everyone who replied. It is very apreciated and I will look into all of these points that were made.
Be well and safe!!
| [reply] |
Re: Using RETURN in a sub
by Anonymous Monk on Feb 19, 2004 at 17:40 UTC
|
Just a few snooty comments on style:
- Indent code within a subroutine's blocks to make it easier to recognize which parts are actually within the subroutine block. This should also be done for conditionals and any other type of block (for instance, closures).
- You should really use lexical variables in a subroutine by using my $variable = .... You are actually using package globals. Having multiple subroutines working on globals can result in unexpected behavior if you aren't aware of the problems they can cause.
- There is a wonderful quoting mechanism in perl called qw. It returns an array of all items between the delimiters split on space. For instance, you would set @days and @mons with:
my @days = qw(Sun Mon Tues Wed Thurs Fri Sat);
my @mons = qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec);
- To quote from perldoc -f localtime:
If EXPR is omitted, localtime() uses the current time (localtime(time)).
- You can use a list slice to set your variables thusly:
my ($mday,$mon,$year,$wday) = (localtime)[3..6];
This tends to result in clearer code since you don't have to search in a sea of undefs to find the variables.
- The expression 2000 + ($year - 100) can be reduced to 1900 + $year.
- You can simplify the expression $year = 1900 + $year to $year += 1900.
- You don't need an intermediate variable to return the stringification of your variables. Instead of:
$date = "$days[$wday] $mons[$mon] $mday $year";
return $date;
You could quite simply do:
return "$days[$wday] $mons[$mon] $mday $year";
Put all of that together and you get:
sub makeDate {
my @days = qw(Sun Mon Tues Wed Thurs Fri Sat);
my @mons = qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec);
my ($mday,$mon,$year,$wday) = (localtime)[3..6];
$year += 1900;
return "$days[$wday] $mons[$mon] $mday $year";
}
You may wish to read perldoc perlsub for information concerning the different ways of calling subroutines, perldoc perlstyle for recommended code formatting (just for a guideline until you can develop your own style), and perldoc perldata for more information about perl data types and how they can be accessed. | [reply] [d/l] [select] |
Re: Using RETURN in a sub
by Abigail-II (Bishop) on Feb 19, 2004 at 16:35 UTC
|
but it doesnt return anything
Bull.
#!/usr/bin/perl
use warnings;
sub makeDate {
@days = ("Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
@mons = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","O
+ct","Nov",
"Dec");
(undef,undef,undef,$mday,$mon,$year,$wday,undef,undef) = localtime
+(time);
$year = 2000 + ($year - 100);
$date = "$days[$wday] $mons[$mon] $mday $year";
return $date;
}
print makeDate, "\n";
__END__
Thurs Feb 19 2004
Abigail | [reply] [d/l] |
Re: Using RETURN in a sub
by borisz (Canon) on Feb 19, 2004 at 16:38 UTC
|
ideas on what im doing wrong?
No, the code looks fine and works.
| [reply] |
Re: Using RETURN in a sub
by eoin (Monk) on Feb 19, 2004 at 16:47 UTC
|
Your problem isn't with the sub its most likely with the code that is refering to it. Will you post that code so we might be able to help you.
| [reply] |
Re: Using RETURN in a sub
by blue_cowdawg (Monsignor) on Feb 19, 2004 at 17:08 UTC
|
but it doesnt return anything. any ideas on what im doing wrong?
I just tried the code with some very minor changes and it
worked for me. I doubt that my changes "fixed"
anything, but here is my modified version in its
entirety:
use strict;
use warnings;
print makeDate(),"\n";
sub makeDate{
my @days = ("Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
my @mons = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","S
+ept","Oct","Nov","Dec");
my ($mday,$mon,$wday,$year);
(undef,undef,undef,$mday,$mon,$year,$wday,undef,undef) = local
+time(time);
$year = 1900 + $year;
my $date = "$days[$wday] $mons[$mon] $mday $year";
return $date;
}
When run it produced:
--$ perl /tmp/mystic.pl
Thurs Feb 19 2004
Since you did not provide the calling environment's code I
don't know what might be going wrong there, but that's
where I'd look...
Peter L. Berghold -- Unix Professional Peter at Berghold dot Net |
| |
Dog trainer, dog agility exhibitor, brewer of
fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and
a good Belgian ale in your chalice. |
| [reply] [d/l] [select] |