arvindmarlabs has asked for the wisdom of the Perl Monks concerning the following question:

Need a urgent help. I am new in perl. My sample script below.
my $date = `date +%Y%m%d`; my $tar = '.tar'; my $gz = '.gz'; my $dbfile = bugs_db_.$date.$gz."\n"; my $webfile = bugzilla_.$date.$tar.$gz."\n"; print $dbfile; print $webfile; exit;
I am execting this script but i am getting result like
bugs_db_20150702 .gz bugzilla_20150702 .tar.gz
I need a output like
bugs_db_20150702.gz bugzilla_20150702.tar.gz
in one line. Please help me guys as soon as possible.

Replies are listed 'Best First'.
Re: Perl Variable Help.
by choroba (Cardinal) on Jul 02, 2015 at 09:23 UTC
    See chomp:
    chomp $date;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Perl Variable Help.
by marto (Cardinal) on Jul 02, 2015 at 09:33 UTC

      I think the OP does want the filenames printing on two lines, he just doesn't want newlines inside the filenames (of course).

      arvindmarlabs, Perl has faster and similarly named equivalents for many of the Unix®-y commands. You don't need to use backticks for simple stuff like this. You can if you want, but it's better to leave them for interacting with other programs for which you don't have any other convenient interface.

      As marto says, shelling out isn't portable across OSes, and it will probably be slower than using Perl's own date function(s) e.g. localtime.

      localtime is fairly straightforward, but does require a couple of extra lines of code to get the date into the format you want. If you can install a module from CPAN, DateTime makes date and time handling a lot easier.

Re: Perl Variable Help.
by hippo (Archbishop) on Jul 02, 2015 at 09:23 UTC
    Need a urgent help. ... Please help me guys as soon as possible.

    If it is that urgent, perhaps formatting your post correctly will increase the chances of quick responses.

    Hint: use <code></code> tags to enclose the code.

    Update: The code has been formatted now, so we can read it clearly. This makes it obvious that you are not stripping the EOL characters from $date, so chomp() is the right approach as choroba has given below.

    That said, it would be highly advisable to avoid the barewords also as they will come back to bite you very swiftly.

Re: Perl Variable Help.
by 1nickt (Canon) on Jul 02, 2015 at 10:01 UTC

    Hi, arvindblabs, welcome to the monastery. It's not a soup kitchen.

    If you find yourself doing eight steps to generate a filename, you are probably not using Perl's built-in functions. Try to find them ... you can make a good guess that yes, Perl has a function for x and then search.
    If you do perldoc -f date on your terminal you will see what I mean.

    Also, don't use backticks to run system executables, you won't get any errors back.

    #!/usr/bin/env perl -w use 5.010; use strict; use POSIX qw/ strftime /; my $date = strftime "%Y%m%d", localtime; say join '.', (join '', $_, $date), 'tar.gz' for qw/ bugs_db_ bugzilla +_ /; __END__
    Remember: Ne dederis in spiritu molere illegitimi!
Re: Perl Variable Help.
by Monk::Thomas (Friar) on Jul 02, 2015 at 10:00 UTC
    This is how I would solve your problem in perl.
    #!/usr/bin/perl # don't leave home without these two use strict; use warnings; # get time the perl-way (use gmtime instead of localtime for UTC) # http://perldoc.perl.org/functions/localtime.html # (the brackets around time are optional) my @fields = localtime(time); # extract the relevant values # the 'dumb' way - single scalars #my $day = $fields[3]; #my $month = $fields[4]; #my $year = $fields[5]; # the smart way - array slice my ($day, $month, $year) = @fields[3,4,5]; # add required offsets $month++; $year += 1900; # use format codes to zero-pad the month and day values (and also the # year - just to be sure; replace 'tar' and 'gz' with '%s'-format code # and a variable if you need them to be dynamic) my $dbfile = sprintf 'bugs_db_%04i-%02i-%02i.gz', $year, $month, + $day; my $webfile = sprintf 'bugzilla_%04i-%02i-%02i.tar.gz', $year, $month, + $day; print "Database file: $dbfile\n"; print "Archive: $webfile\n"; exit;
    Output:
    Database file: bugs_db_2015-07-02.gz
    Archive:       bugzilla_2015-07-02.tar.gz
    

    Why not use `date`?

    There are 3 reasons - two have been mentioned already.

    a) date may not be available - this may or may not concern you at all

    b) it's really tricky to handle all side effects of calling external commands (e.g. properly catch the exit code, parse or hide StdErr, actually execute the correct command, ...)

    not mentioned yet

    c) The returned output of the external command may change unexpectedly. In your case it's not relevant, but you should keep that in mind when handling actual date values, parsing ifconfig output or any other command providing output that is influenced by the current language settings.

    In order to properly execute an external command, you should provide a sane environment, evaluate the return code, catch StdOut, catch StdErr and maybe do some other stuff I forgot ... usually I try to find a way to do this stuff in pure-perl because it's easier to get it right. ;)

Re: Perl Variable Help.
by Discipulus (Canon) on Jul 02, 2015 at 09:29 UTC
    put code tags please, and show some effort asking a question: 'urgent' and 'as soon as possible' are not the right words when asking for help. Anyway you are appending newlines to your variables and Perl print them. Always use stricts and warnings in your code, and you miss some quotes too.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      He wants newlines after them, just not inside them. So adding newlines is fine, but he needs to chomp the shell response (or better, get the date from Perl).
        Ah now i see, obviously he needs to chomp the date returned output, thanks

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.