Perl - setting a variable ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - setting a variable ?
# 1  
Old 09-01-2009
Perl - setting a variable ?

hi there, I have a question about a snippet of code i have which runs localtime() to convert the current date/time into a mysql happy format



Code:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
printf "%4d-%02d-%02d  %02d:%02d:%02d\n",$year+1900,$mon+1,$mday,$hour,$min,$sec;

If i run the above as a standalone script, i get the desired result

Code:
 # ./time.pl 
2009-09-01 10:22:18

However, what i want to do is incorporate it as part of another script and populate an internal variable with the result rather than sending it to standard out ...so i tried this

Code:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my  $datetime = printf "%4d-%02d-%02d  %02d:%02d:%02d\n",$year+1900,$mon+1,$mday,$hour,$min,$sec;

print "\n datetime equals $datetime\n";

Code:
# ./time.pl 
2009-09-01 10:10:31

datetime equals 1
#

but as you can see, not only has it still output to result to standard out (not ideal), but it has populated the variable $datatime with the value "1"

is there something im doing drstically wrong here

any hlp would be great
# 2  
Old 09-01-2009
printf will print the string to a filehandle (STDOUT by default) and return a status code.
sprintf will print nothing, but return the interpolated string, which you then can assign to a variable.
See also: perldoc -f sprintf
# 3  
Old 09-01-2009
wow, thankyou that solves all my problems ....perl really does have an answer for everything doesnt itSmilie


script now returns
Code:
# ./time.pl 

datetime equals 2009-09-01 11:04:03



---------- Post updated at 07:38 AM ---------- Previous update was at 05:04 AM ----------

just out of interest and to save me repeating this code throughout my script when i need it, is there an easy way to functionise this ?

I tried this


Code:
#!/bin/perl -w

sub xxtime() {
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
        my $datetime = sprintf "%4d-%02d-%02d %02d:%02d:%02d\n",$year+1900,$mon+1,$mday,$hour,$min,$sec;
        return $datetime;
}

xxtime();

print "\n time date equals $datetime\n";

but it returns a null value for $datetime

presumably perl has the ability to create functions that

a) i dont pass arguments to
b) return an updated value for a variable that i can use elsewhere in the script
# 4  
Old 09-01-2009
Try it like this:
Code:
#!/bin/perl -w

sub xxtime() {
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
        my $datetime = sprintf "%4d-%02d-%02d %02d:%02d:%02d\n",$year+1900,$mon+1,$mday,$hour,$min,$sec;
        return $datetime;
}

my $datetime = xxtime();

print "\n time date equals $datetime\n";

With "my" you create a lexically scoped variable, meaning that variable is only valid within the innermost enclosing block. So the variable $datetime you declare in xxtime() is only valid within this function. To use it outside you have to assign the value return()ed to a new variable.
# 5  
Old 09-01-2009
Thank you very much pludi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Perl error : perl: warning: Setting locale failed.

This's my problem perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LC_ALL = "en_US.UTF-8", LC__FASTMSG = "true", LC_MESSAGES = "", LC_CTYPE = "en_US.UTF-8", LC_TYPE = "en_US.UTF-8", LANG = "EN_US"... (1 Reply)
Discussion started by: bobochacha29
1 Replies

2. Shell Programming and Scripting

Setting a variable within if block

Hi, i have a variable which i would like to set inside an if block for example IS_VAR=0 if then IS_VAR=1 fi echo IS_VAR the last echo statement gives 0.So setting variables in the if block doesnt have effect outside the block?Is there any workaround for this? Thanks , Padmini (11 Replies)
Discussion started by: padmisri
11 Replies

3. Programming

Setting Environment variable..!

Hi, I already have one CPP program which invokes the C program.And the C program contains whole function definitions..!This is a working program..I have to enable the logs in both CPP as well as in the C program ..!So I am reading the enviornmental variable log path from the CPP and doing the... (2 Replies)
Discussion started by: Kattoor
2 Replies

4. Shell Programming and Scripting

Help with setting a variable!

I am working within a while loop and i am trying to set a variable that will read out each count of the files. the problem is the count variable i have set up gives me a total and not the individual count of each file. in the data area there is 4 abc.dat and 1 def.dat. how can i do this??? ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

5. UNIX for Dummies Questions & Answers

setting a variable

In my script, I have the following command.... du -sk `ls -ltd sales12|awk '{print $11}'`|awk '{print $1}' it returns the value 383283 I want to modify my script to capture that value into a variable. So, I try doing the following... var1=`du -sk `ls -ltd sales12|awk '{print... (5 Replies)
Discussion started by: tumblez
5 Replies

6. Shell Programming and Scripting

Setting variable

How do you set a varible with information that contains a string and also another variable? For example: subject="Attention: $name / This $type needs your attention" The $xxxx are of course other variables that I instantiated earlier. Is it like Java where you have to use double quotes and... (1 Reply)
Discussion started by: briskbaby
1 Replies

7. Shell Programming and Scripting

Variable setting help please

L=0 cat test.sh | while read line do L='expr $1 + 1' echo $L done echo $l >>> the echo $L at the end produces 0 but i actually want it to produce the number of lines - any idea why this is happening? (16 Replies)
Discussion started by: penfold
16 Replies

8. UNIX for Dummies Questions & Answers

Setting a variable

I want to set a variable to be any number of dashes. Rather than doing the following: MYVAR="------------------" I'd like to be able to set the variable to, say, 80 dashes but don't want to have to count 80 dashes. Is there a way to do this? (2 Replies)
Discussion started by: photh
2 Replies

9. Programming

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies

10. UNIX for Dummies Questions & Answers

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies
Login or Register to Ask a Question