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

I need to send a command to UNIX that copies a log file and renames it with a variable in my Perl program. I know how to send commands to UNIX:
print `cp x.txt y.txt';
which works fine...but if I have the variable a to rename the file with:
print `cp x.txt $a_log.txt`
the error message comes up when I run the Perl script dbi:
Name "main::a_log" used only once: possible typo at dbi line 146. Use of uninitialized value in concatenation (.) or string at dbi line +146.
Any suggestions for using perl variables to rename a file with UNIX commands? I've also looked into the system() and exec() commands but have not come up with anything that works.

Replies are listed 'Best First'.
Re: Using UNIX commands inside Perl scripts, with Perl variables
by arturo (Vicar) on Jun 08, 2001 at 01:07 UTC

    Your backticks are just fine. The problem is that you're running with warnings turned on and you haven't defined the variable $a_log anywhere.

    When you get error messages you don't understand, a good place to read up on them is perldoc perldiag, or better yet, run your script with use diagnostics; near the top.

    Oh yes, and read up on scoping (see Dominus' home node for a great link), and add use strict; and declare your variables using my, our (Perl 5.6.0+) or use vars qw($var $names). You'll thank yourself for it later.

    Update oh duh. The other replies figured out that you're trying to use $a as the variable and append "_log.txt" to the end of it. You could very well be running under use strict because $a is a special global variable. I'd suggest using something more descriptive anyway, but note that $a and $b have a special meaning in Perl. Ref: perlfunc:sort

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: Using UNIX commands inside Perl scripts, with Perl variables
by wog (Curate) on Jun 08, 2001 at 01:07 UTC
    First, see the File::Copy module.

    As for your actual problem, perl is reading $a_log, as a variable, instead of just using $a. You can fix the problem be explictly saying that "_log" isn't part of the variable by using ${a} instead of $a.

    You appear to not being using strict. It's a very good idea to do so.

Re: Using UNIX commands inside Perl scripts, with Perl variables
by runrig (Abbot) on Jun 08, 2001 at 01:06 UTC
    use "${a}" to keep longer names from being interpolated inside quotes and backticks.
A reply falls below the community's threshold of quality. You may see it by logging in.