Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Write filename w/ scalar and bareword

Reply
Thread Tools

Write filename w/ scalar and bareword

 
 
mt35
Guest
Posts: n/a
 
      09-29-2004
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.
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-29-2004
(mt35) wrote in news:2763816a.0409290416.1ddc5229
@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.
 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      09-29-2004
On Wed, 29 Sep 2004 12:46:43 +0000, A. Sinan Unur wrote:
> 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);
}


--
Tore Aursand <>
"First get your facts; then you can distort them at your leisure."
(Mark Twain)
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-29-2004
mt35 <> wrote:

> 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.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-29-2004
Tore Aursand <> wrote in
news:

> On Wed, 29 Sep 2004 12:46:43 +0000, A. Sinan Unur wrote:
>> 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.


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.
 
Reply With Quote
 
187
Guest
Posts: n/a
 
      09-29-2004
A. Sinan Unur wrote:
> (mt35) wrote in news:2763816a.0409290416.1ddc5229
> @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.
>


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.


 
Reply With Quote
 
mt35
Guest
Posts: n/a
 
      09-29-2004
"A. Sinan Unur" <> wrote in message news:<Xns9573594CF34A5asu1cornelledu@132.236.56.8> ...
> (mt35) wrote in news:2763816a.0409290416.1ddc5229
> @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;


This was the problem...Is my newness to perl painfully obvious yet?

> 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 mistake, I'll triple check next time.

> > 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?


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!
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-29-2004
mt35 <> wrote:
> "A. Sinan Unur" <> wrote in message news:<Xns9573594CF34A5asu1cornelledu@132.236.56.8> ...
>> (mt35) wrote in news:2763816a.0409290416.1ddc5229
>> @posting.google.com:



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


>> To solve your immediate problem, add a
>>
>> chomp $date;

>
> 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.)


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
mt35
Guest
Posts: n/a
 
      09-30-2004
Tad McClellan <> wrote in message news:<>.. .
> mt35 <> wrote:
> > "A. Sinan Unur" <> wrote in message news:<Xns9573594CF34A5asu1cornelledu@132.236.56.8> ...
> >> (mt35) wrote in news:2763816a.0409290416.1ddc5229
> >> @posting.google.com:

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

>
> >> To solve your immediate problem, add a
> >>
> >> chomp $date;

> >
> > 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.)


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bareword errors? gcr Perl Misc 31 12-09-2006 08:51 AM
"Bareword found where operator expected" in for loop. aaron.mosiah.curtis@gmail.com Perl Misc 4 05-10-2006 10:36 PM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Object method and file handle creates "bareword" error. How to code? Henry Law Perl Misc 6 04-25-2004 08:57 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57