Write filename w/ scalar and bareword

M

mt35

Hi,

I'm trying to write a file name using a predefined scalar (my $date =
`date "+%m%d%Y"`) and a bareword (snort.tar). Here's the code:


#----------Start Code Block------------
#!/usr/bin/perl

use strict;
use Archive::Tar;

my $tar = Archive::Tar->new;

my $date = `date "+%m%d%Y"`;

chdir "/home/user/perl" or die "$!";

my @tarlst = ("test0", "test1");

$tar->add_files(@tarlst);
$tar->write ("$date test.tar");
#--------End Code Block--------------

However the file written is: 09292004?.snort.tar.gz

My question is why is the question mark being written?...If I add a
"print $date" it comes out fine.

Thanks for your time.
 
A

A. Sinan Unur

(e-mail address removed) (mt35) wrote in @posting.google.com:
I'm trying to write a file name using a predefined scalar (my $date =
`date "+%m%d%Y"`) and a bareword (snort.tar). Here's the code:


#----------Start Code Block------------
#!/usr/bin/perl

use strict;
use Archive::Tar;

my $tar = Archive::Tar->new;

my $date = `date "+%m%d%Y"`;

Arrrgh! Why???

To solve your immediate problem, add a

chomp $date;

after this.
chdir "/home/user/perl" or die "$!";

my @tarlst = ("test0", "test1");

$tar->add_files(@tarlst);
$tar->write ("$date test.tar");
#--------End Code Block--------------

However the file written is: 09292004?.snort.tar.gz

That is impossible. You have not run the code you posted. There is no
mention of 'snort' anywhere between the 'Start Code Block' and 'End Code
Block' markers. Make sure to post the actual code you run.
My question is why is the question mark being written?...If I add a
"print $date" it comes out fine.

Define fine. Is it 'fine' that a newline is written even though you have
not specified it?

There is no need to spawn an extra process to do something as simple as
this.

sub filename_prefix_generator {
my ($sec, $min, $hour, $mday, $mon, $year, undef, undef, undef) =
localtime time;
sprintf '%2.2d%2.2d%4.4d_%2.2d%2.2d%2.2d',
$mon+1, $mday, $year, $hour, $min, $sec;
}

or, look into POSIX::strftime.

Sinan.
 
T

Tore Aursand

sub filename_prefix_generator {
my ($sec, $min, $hour, $mday, $mon, $year, undef, undef, undef) = localtime time;
sprintf '%2.2d%2.2d%4.4d_%2.2d%2.2d%2.2d', $mon+1, $mday, $year, $hour, $min, $sec;
}

First of all, you should also add 1900 to $year. Secondly, TIMTOWTDI (untested);

sub filename_prefix_generator {
my ($sec, $min, $hour, $mday, $mon, $year) = (localtime)[0..5];
sprintf( '%02d%02d%4d%02d%02d%02d', $mon+1, $mday, $year+1900, $hour, $min, $sec);
}
 
T

Tad McClellan

mt35 said:
I'm trying to write a file name using a predefined scalar (my $date =
`date "+%m%d%Y"`) and a bareword (snort.tar). Here's the code:
^^^^^^^^

There IS NO bareword anywhere in your code...

A bareword is a word that is not quoted, that is what makes it "bare".

my $date = `date "+%m%d%Y"`;


Try adding a debugging print() statement here:

print "date is [$date]\n";

$tar->write ("$date test.tar");
^^^^^^^^
^^^^^^^^ it is in quotes, so it is not "bare"
However the file written is: 09292004?.snort.tar.gz


How did it become "snort.tar" instead of "test.tar"?

Where did the dot before the "snort" come from?

What happened to the space that you put into the filename?

Is this truly what your filename looks like?

If so, then you haven't shown us your real code.

If not, then how are we supposed to give an accurate answer
when given inaccurate symptoms?


Have you seen the Posting Guidelines that are posted here frequently?

My question is why is the question mark being written?


First, there is no question mark being written. I expect *your shell*
is using question mark to represent a newline character?

Second, the question mark is probably actually a newline character,
chomp() it off if you do not want it there.
 
A

A. Sinan Unur

First of all, you should also add 1900 to $year.

My bad ... Hey, aren't you impressed that I remembered to add 1 to the
month, though :)
Secondly, TIMTOWTDI (untested);

sub filename_prefix_generator {
my ($sec, $min, $hour, $mday, $mon, $year) = (localtime)[0..5];
sprintf( '%02d%02d%4d%02d%02d%02d', $mon+1, $mday, $year+1900,
$hour, $min, $sec);
}

Was being lazy ... I copied and pasted the my(...) from the perldoc.
Obviously, an array slice is nicer.

Thanks.

Sinan.
 
1

187

A. Sinan Unur said:
(e-mail address removed) (mt35) wrote in @posting.google.com:


Arrrgh! Why???

To solve your immediate problem, add a

chomp $date;

after this.

Of just:

chomp(my $date = `date "+%m%d%Y"`);

:)
That is, if you want ot use that approuch of using the "date" command,
which is rather limiting for portability. Your Perl-only method is much
more logical and portable. Even more the suggestion to use POSIX.
 
M

mt35

A. Sinan Unur said:
(e-mail address removed) (mt35) wrote in @posting.google.com:


Arrrgh! Why???

To solve your immediate problem, add a

chomp $date;

This was the problem...Is my newness to perl painfully obvious yet? ;)
after this.


That is impossible. You have not run the code you posted. There is no
mention of 'snort' anywhere between the 'Start Code Block' and 'End Code
Block' markers. Make sure to post the actual code you run.

My mistake, I'll triple check next time.
Define fine. Is it 'fine' that a newline is written even though you have
not specified it?

I see your point, I didn't even reconize that was occuring.
There is no need to spawn an extra process to do something as simple as
this.

sub filename_prefix_generator {
my ($sec, $min, $hour, $mday, $mon, $year, undef, undef, undef) =
localtime time;
sprintf '%2.2d%2.2d%4.4d_%2.2d%2.2d%2.2d',
$mon+1, $mday, $year, $hour, $min, $sec;
}

or, look into POSIX::strftime.

That *is* much better...I'll use this instead, along with Tore's suggestions.

Thanks for the help!
 
T

Tad McClellan

mt35 said:
This was the problem...Is my newness to perl painfully obvious yet? ;)


Not at all.

I've been using Perl for mumble,mumble years, and I still
forget to chomp() on occasion.

Your newness to programming itself seems obvious though, as you
did not try some debugging print()s before calling on hundreds
of other people to look at the problem.

(so try some debugging first the next time.)
 
M

mt35

Tad McClellan said:
Not at all.

I've been using Perl for mumble,mumble years, and I still
forget to chomp() on occasion.

Your newness to programming itself seems obvious though, as you
did not try some debugging print()s before calling on hundreds
of other people to look at the problem.

(so try some debugging first the next time.)

As was discussed in the first, second and sixth posts of this thread,
I had tried a print $date. However, I was unable to recognize what was
happening. So debugging is great, but you also need to know how to
interpret the results, which is where I was lacking. I also do see the
benifits of what you had written (print "date is [$date]\n";) as that
would of alerted me to the additional newline that was being injected
into the variable when the "]" printed on the newline. I will use that
method next time.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top