Write filename w/ scalar and bareword

Discussion in 'Perl Misc' started by mt35, Sep 29, 2004.

  1. mt35

    mt35 Guest

    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.
    mt35, Sep 29, 2004
    #1
    1. Advertising

  2. (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.
    A. Sinan Unur, Sep 29, 2004
    #2
    1. Advertising

  3. mt35

    Tore Aursand Guest

    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)
    Tore Aursand, Sep 29, 2004
    #3
  4. 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
    Tad McClellan, Sep 29, 2004
    #4
  5. Tore Aursand <> wrote in
    news:p:

    > 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.
    A. Sinan Unur, Sep 29, 2004
    #5
  6. mt35

    187 Guest

    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.
    187, Sep 29, 2004
    #6
  7. mt35

    mt35 Guest

    "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!
    mt35, Sep 29, 2004
    #7
  8. 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
    Tad McClellan, Sep 29, 2004
    #8
  9. mt35

    mt35 Guest

    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.
    mt35, Sep 30, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clint Olsen
    Replies:
    6
    Views:
    355
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  2. Henry Law
    Replies:
    6
    Views:
    164
    Michel Rodriguez
    Apr 25, 2004
  3. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    163
    Arndt Jonasson
    Jan 27, 2005
  4. Replies:
    4
    Views:
    353
    Michele Dondi
    May 10, 2006
  5. Gunnar Hjalmarsson

    Bareword errors?

    Gunnar Hjalmarsson, Nov 28, 2006, in forum: Perl Misc
    Replies:
    31
    Views:
    350
    Martijn Lievaart
    Dec 9, 2006
Loading...

Share This Page