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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question 2

Status
Not open for further replies.

hokky

Technical User
Nov 9, 2006
170
0
0
AU
Hi guys,

I got couple of questions about this code:
Code:
### Need timestamp to find the files
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() - 24*60*60);
[b]my $datestamp[/b] = sprintf("%04d%02d%02d",$year+1900,$mon+1,$mday);
$output_prefix = $datestamp.[b]$DATABASE[/b];


[b]#[/b]printf "testing %04d%02d%02d\n",$year,$mon,$mday
print "output timestamp $output_prefix"

questions :
1. what's different between "my $datestamp" and "$datestamp", I've tried both and it's working fine?
any explanation?
2. what's $DATABASE for? I've tried without it and still working? is there in library somewhere?
3. if I remove the # behind the printf, those code won't work, any idea why?

the error as below :
syntax error at ./test.pl line 10, near "$mday
print"
Execution of ./test.pl aborted due to compilation errors.

Thanks guys for your explanation,
Appreciate that!
 
1 = If your using strict it will require the my
2 = I have no clue.. it must be being pulled from somewhere else in the code
3 = you missing the semicolon at the end of the line

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks travs69,

few questions more..
what the strict for? when are we using it?

Cheers,
 
Hi hokky,
strict is a pragma provided by perl. It forces to declare a variable before it is used in a code, thus obtaining better programming practice and prevents errors/misbehaving of code due to typo
e.g.
Code:
use strict ;
my $str1 = "ABC" ;
.....
....
$STR1 .= "XYZ" ; // typo $STR1 instead of $str1

The above piece of code will throw undefined variable error when using strict, because $STR1 is not declared before it's used.
If strict is not used the code will get away with that assigning unwanted value to $str1 and $STR1.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
hokky said:
when are we using it?
Always. Perl is fairly lax about what you can get away with syntax-wise. Undefined variable? Obviously just an oversight on your part, perl creates it on the fly, sets it to undefined - that was what you wanted, right? Larry Wall's view is that isn't sloppiness, just an eloquent conservation of keystrokes. The programmer should know what he is doing, and is responsible for his own actions. But this isn't always the case. strict tightens up on what things you can get away with in your code. There are a few obscure situations where you can't use strict but they are few and far between, so you are best off starting all your scripts with
Code:
use strict;
use warnings;
and maybe even
Code:
use diagnostics;
for good measure...


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks guys,
have a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top