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:
could somebody explain why this dont work please

 



gerble1000
User

Jun 16, 2012, 3:28 AM

Post #1 of 4 (974 views)
could somebody explain why this dont work please Can't Post

i have this code that i want to save a string to a folder named archive and to a text file named by todays date
here is what i have



Code
my $date_string = localtime; 
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;

my $savedate = "$mday.$mon.$year";
my $location = "archive\$mday.$mon.$year";

open(F, '>>:utf8', $location);
print F "my string";
close F;
i have tried playing with this but no joy


gerble1000
User

Jun 16, 2012, 3:59 AM

Post #2 of 4 (970 views)
Re: [gerble1000] could somebody explain why this dont work please [In reply to] Can't Post

i forgot to mention that it does save the data but it saves it in the same directory call archive16.6.2012 or something like that


FishMonger
Veteran / Moderator

Jun 16, 2012, 6:50 AM

Post #3 of 4 (966 views)
Re: [gerble1000] could somebody explain why this dont work please [In reply to] Can't Post


Quote

Code
my $date_string = localtime;  
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;


Use the strftime function from the POSIX module to assign your date string. It is much easier and cleaner.

Code
my $date = strftime("%d.%m.%Y", localtime);



Quote

Code
my $location = "archive\$mday.$mon.$year";


Use a forward slash instead of the backslash and use the var that was already assigned the datestamp.

Code
my $location = "archive/$date"



Quote

Code
open(F, '>>:utf8', $location); 
print F "my string";


1) use a lexical var for the filehandle instead of the bareword
2) ALWAYS check the return code of an open call to verify it was successful and take proper action if it wasn't
3) Why are you using printf without specifying a format? It's better to use print if you don't need the printf formatting

Code
open my $fh, '>>:utf6', $location or die "failed to open '$location' in append mode: <$!>"; 
print $fh "my string";



gerble1000
User

Jun 16, 2012, 8:10 AM

Post #4 of 4 (961 views)
Re: [FishMonger] could somebody explain why this dont work please [In reply to] Can't Post

thankyou that helped on a few problems i was having :)

 
 


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

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