package VCP::Utils; =head1 NAME VCP::Utils - utilities used within VCP's modules. =head1 SYNOPSIS use VCP::Utils qw( shell_quote ); =head1 DESCRIPTION A mix-in class providing methods shared by VCP::Source::cvs and VCP::Dest::cvs, mostly wrappers for calling the cvs command. =cut @EXPORT = qw( shell_quote profile profiling ); @ISA = qw( Exporter ); use Exporter; use Carp; use strict ; # disallow defined but not true logfiles. use constant profiling => defined $ENV{VCPPROFILE} && length $ENV{VCPPROFILE} && $ENV{VCPPROFILE} ; BEGIN { if ( profiling ) { eval "use Time::HiRes qw(gettimeofday); 1" or die $@; my $logfile = $ENV{VCPPROFILE}; open PROFILE_LOG, ">>$logfile" or die "couldn't open log file '$logfile' for append\n"; } } =head1 FUNCTIONS =over =item shell_quote my $line = shell_quote \@command; my $line = shell_quote @command; print STDERR, $line, "\n"; Selectively quotes the command line to allow it to be printed in a non-vague fashion and to be pastable in the local shell (sh/bash on Unix, COMMAND.COM, etc. on Win32 and OS2). NOTE: May not be perfect; errs on the side of safety and doesn't try to escape things right on Win32 yet. Patches welcome. =cut { my $q = $^O =~ /Win32|OS2/ ? '"' : "'"; sub shell_quote { my @parms = ref $_[0] eq "ARRAY" ? @{$_[0]} : @_; return join " ", map { defined $_ ? m{[^\w:/\\.,=-]} ? do { ( my $s = $_ ) =~ s/[\\$q]/\\$1/; "$q$s$q"; } : $_ : "<>"; } @parms; } } =item profile log high resolution time info to the PROFILE_LOG file. =cut sub profile { unless ( profiling ) { return; } die "usage: log_time " unless @_ == 1; die "profile's log message must start with the string BEG or END" unless $_[0] =~ /^(BEG|END)/ ; my ($sec, $usec) = gettimeofday(); printf PROFILE_LOG "TIME: %10d.%06d -> $_[0] \n", $sec, $usec; } =back =head1 COPYRIGHT Copyright 2000, Perforce Software, Inc. All Rights Reserved. This module and the VCP package are licensed according to the terms given in the file LICENSE accompanying this distribution, a copy of which is included in L. =cut 1 ;