Link to home
Start Free TrialLog in
Avatar of ewang1205
ewang1205

asked on

touch command in perl script

I want to run touch a file command like the following.  The file name is parameter passed into a perl script.  How to write the following?  Thanks.

touch $ARGV[0]  
Avatar of Michael Dyer
Michael Dyer
Flag of United States of America image

Try this
                 #!/usr/bin/perl
                 $now = time;
                 utime $now, $now, @ARGV;
Avatar of ewang1205
ewang1205

ASKER

What does your script do?  Thanks.
From the Perl doc:
 utime LIST
             Changes the access and modification times on each
             file of a list of files.  The first two elements of
             the list must be the NUMERICAL access and
             modification times, in that order.  Returns the
             number of files successfully changed.  The inode
             change time of each file is set to the current time.
             For example, this code has the same effect as the
             Unix touch(1) command when the files already exist
             and belong to the user running the program:

                 #!/usr/bin/perl
                 $atime = $mtime = time;
                 utime $atime, $mtime, @ARGV;

             Since perl 5.7.2, if the first two elements of the
             list are "undef", then the utime(2) function in the
             C library will be called with a null second
             argument. On most systems, this will set the file's
             access and modification times to the current time
             (i.e. equivalent to the example above) and will even
             work on other users' files where you have write
             permission:

                 utime undef, undef, @ARGV;

             Under NFS this will use the time of the NFS server,
             not the time of the local machine.  If there is a
             time synchronization problem, the NFS server and
             local machine will have different times.  The Unix
             touch(1) command will in fact normally use this form
             instead of the one shown in the first example.

             Note that only passing one of the first two elements
             as "undef" will be equivalent of passing it as 0 and
             will not have the same effect as described when they
             are both "undef".  This case will also trigger an
             uninitialized warning.
I don't see it can solve my original issue though.  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of gremwell
gremwell

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  Yes, I want to use touch to create a file.  I am testing gremwell's solution.  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you just want to use the touch command as a unix command then you can use system as well.

Try this:

system("touch ".$ARGV[0]);

Read more on this at:
http://www.perlhowto.com/executing_external_commands
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> system("touch ".$ARGV[0]);
> system "touch $ARGV[0]";

Just for the sake of correctness -- the code above may have difficulties handling some special cases, such as file names containing white spaces and file names starting with dash character. To cover for it, system() should be invoked as following: system("touch", "--", $ARGV[0]);
If the touch command is considered the standard of correctness, the code above handles those cases the same way that the touch command does.
It might be argued that
system "touch",@ARGV;
would be more correct, but the original specification did not state anything beyond $ARGV[0] should be interpreted
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ozo: You are right, ewang1205 has asked for equivalent of UNIX touch command, perhaps he wanted to simulate all its quirks as well.

@fredmc: As a matter of fact, the back ticks example will have problems with file names containing double quotes ;).
Suggest you add utime in addition to open/close, since if the file already exists you don't need to open and close it, you can simply call the utime built-in function to change its access and modification times.

Other way is to use  Shell::Command wrapper around ExtUtils::Command

use Shell::Command;
touch @files;
Thanks for all the help.  I have not tested each cases yet.  But, I think most of them will work fine.