[26359] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8532 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 17 14:05:37 2005

Date: Mon, 17 Oct 2005 11:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 17 Oct 2005     Volume: 10 Number: 8532

Today's topics:
    Re: finding if a hash entry exists <rvtol+news@isolution.nl>
    Re: making same change to *lots* of files, *without* da (David Combs)
    Re: making same change to *lots* of files, *without* da (David Combs)
    Re: making same change to *lots* of files, *without* da xhoster@gmail.com
    Re: making same change to *lots* of files, *without* da (David Combs)
        perldoc -q 'date|time' FAQ -- and -- Re: making same ch (David Combs)
        This worked... thanks :o) <info2005@remove-this-including-dot.bigbunker.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 17 Oct 2005 13:59:34 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: finding if a hash entry exists
Message-Id: <dj0ati.gg.1@news.isolution.nl>

Zebee Johnstone schreef:

> I have a hash, $hash.

Isn't that a hash reference?


> If may or may not have an entry
> $hash->{'fred"}

It can't have: the quotes don't match.

-- 
Affijn, Ruud

"Gewoon is een tijger."


------------------------------

Date: Mon, 17 Oct 2005 17:21:22 +0000 (UTC)
From: dkcombs@panix.com (David Combs)
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <dj0mii$kco$1@reader2.panix.com>

In article <XtH4f.8$Oy5.135@news.oracle.com>,
tester  <tester@uussssh.com> wrote:
>David Combs wrote:
>> Well, it's time to bite the bullet, and quit fighting
>> the idea (requirement?) that "/home" isn't for my own
 ...
>> 
>> 
>
>I think you can copy files from /home to /myhome with -p option! explore 
>man cp

Copy the files?  Really?

Can't I just rename (mv) the current (via hack to automoount) /home
to /myhome?

But that isn't the problem -- the problem is how to change all
the "/home[^a-zA-Z-]" to "/myhome" -- *within* those files.

david




------------------------------

Date: Mon, 17 Oct 2005 17:26:47 +0000 (UTC)
From: dkcombs@panix.com (David Combs)
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <dj0msn$ovm$1@reader2.panix.com>

In article <slrndl6ef6.9k4.1usa@asu1cornelledu.unur.com>,
A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
>
>Process files one by one. First stat the file, and save the result of
>the stat call, then touch the newly created file with the saved stat
>information.
>
>You can use either the touch command line utility or File::Touch
>
><URL: http://search.cpan.org/~nwetters/File-Touch-0.01/>
>
>Sinan

One by one, *by hand*?

Gee, I was hoping for a (if trustworthy!) single cmd based on something
like this:

  perl -i.bak -pe 's/(?:\015)?\012/\015/g' filenames  

, but with a "don't modify the date/times" (somehow) added in.


Thanks,

David


PS: FEATURE-SUGGESTION: if not already existing, then a *new*
option for just that purpose?




------------------------------

Date: 17 Oct 2005 17:45:56 GMT
From: xhoster@gmail.com
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <20051017134556.428$hg@newsreader.com>

dkcombs@panix.com (David Combs) wrote:
>
> Well, the time has come to modify several hundred files,
> changing every /home to /myhome.
>
> But doing that will screw up the creation dates of the
> files, on which I rely to remind me what I've been
> working on recently.
>
> Since I want to make this change to just about EVERYTHING,
> I think it best if the dates do NOT change.
>
> So, my question: how would *you* accomplish this task?

With utime.

>
> (maybe via a oneliner, the argument being a list of
> the files that need this minor change?)
>
> Yes, I've seen the faq, but before I try that,
> I'd ask here if someone had already doine something
> like this.

There are lots of faqs.  Which faq are you referring to?

>
> (Plus knew the dangers involved of dateless-hacking.)

Not all combinations of OS/FS/mount options/phase of moon allow you to
back-date files.  So test your program out on test files on the same
mount-point your real files, otherwise you may permently lose your date
information if something goes wrong.  (Or backup your files in
data-preserving way before doing anything)


Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: Mon, 17 Oct 2005 17:51:45 +0000 (UTC)
From: dkcombs@panix.com (David Combs)
Subject: Re: making same change to *lots* of files, *without* date changed
Message-Id: <dj0obh$971$1@reader2.panix.com>

In article <15K4f.974$S24.57600@news.xtra.co.nz>,
Tintin <tintin@invalid.invalid> wrote:
>
>"David Combs" <dkcombs@panix.com> wrote in message 
>news:divco0$t3q$1@panix1.panix.com...
>>
>> Well, it's time to bite the bullet, and quit fighting
>> the idea (requirement?) that "/home" isn't for my own
>> use, but for the automounter's.
>
>Only if you have a need for automounter.
>
>>
>> Way back with sunos (before solaris), use rooted a lot
>> of our stuff at /home, and refer to it throughout our
>> programs.
>>
>> But solaris says no can do, unless you hack the automounter
>> files.
>
>Or simply disable automounter.
>
>>
>> Well, the time has come to modify several hundred files,
>> changing every /home to /myhome.
>>
>> But doing that will screw up the creation dates of the
>> files, on which I rely to remind me what I've been
>> working on recently.
>
>No such thing as a creation date in Unix.  You probably meant last 
>modification date.
>
>>
>> Since I want to make this change to just about EVERYTHING,
>> I think it best if the dates do NOT change.
>>
>> So, my question: how would *you* accomplish this task?
>
>You certainly could do it in Perl, however it's just as easy with simple 
>Unix tools (my example references Perl to make it vaguely relevant to this 
>group), eg:
>
>#!/bin/sh
>for file in *.sh
>do
>   perl -pe 's:/home:/myhome:g' $file >$file.new &&  touch -r $file 
>$file.new && mv $file.new $file
>done 


AH HA!  COOL -- THANK YOU!

Please, (other) perl/unix gurus, visually/mentally verify the
above nifty-trick` (obvious only once you see it!) and 
RSVP here -- before I go try the thing!

(Of course I'll try it on some test files first --
but maybe some of you will see some inherent problems,
eg if a file has certain attributes ...?)


THANKS TO ONE AND ALL!

David

PS: again, please say something, pos or neg, about
the above super-concise scheme.

And thanks for *that*, too!

D.





------------------------------

Date: Mon, 17 Oct 2005 17:43:24 +0000 (UTC)
From: dkcombs@panix.com (David Combs)
Subject: perldoc -q 'date|time' FAQ -- and -- Re: making same change to *lots* of files, *without* date changed
Message-Id: <dj0nrs$2t4$1@reader2.panix.com>


In article <dwH4f.29049$Io.3499@clgrps13>,
John W. Krahn <krahnj@telus.net> wrote:
>David Combs wrote:
 ...
>> 
>> But doing that will screw up the creation dates of the
>> files, on which I rely to remind me what I've been
>> working on recently.
>
>AFAIK Unix doesn't record the creation dates of files anywhere.

Well, then whatever date it does change upon a "touch" of the file.

>
>
>> Since I want to make this change to just about EVERYTHING,
>> I think it best if the dates do NOT change.
>> 
>> So, my question: how would *you* accomplish this task?
>
>man tar
>man cpio

John, how does that help me *edit* the gillion files?,

, all the while, *not* changing any of the dates/times associated
with the files?

I figured that if anyone could come up with a one-liner,
it would be *you*!   :-)


>John


FYI: Here's what "perldoc -q 'date|time' >! foo.out" gets me:


Found in /me-FIRST-in-PATH-bin/perl-5.8.6/lib/5.8.6/pod/perlfaq4.pod
  How can I compare two dates and find the difference?
    If you're storing your dates as epoch seconds then simply subtract one
    from the other. If you've got a structured date (distinct year, day,
    month, hour, minute, seconds values), then for reasons of accessibility,
    simplicity, and efficiency, merely use either timelocal or timegm (from
    the Time::Local module in the standard distribution) to reduce
    structured dates to epoch seconds. However, if you don't know the
    precise format of your dates, then you should probably use either of the
    Date::Manip and Date::Calc modules from CPAN before you go hacking up
    your own parsing routine to handle arbitrary date formats.

  How do I find yesterday's date?
    If you only need to find the date (and not the same time), you can use
    the Date::Calc module.

            use Date::Calc qw(Today Add_Delta_Days);

            my @date = Add_Delta_Days( Today(), -1 );

            print "@date\n";

    Most people try to use the time rather than the calendar to figure out
    dates, but that assumes that your days are twenty-four hours each. For
    most people, there are two days a year when they aren't: the switch to
    and from summer time throws this off. Russ Allbery offers this solution.

        sub yesterday {
                    my $now  = defined $_[0] ? $_[0] : time;
                    my $then = $now - 60 * 60 * 24;
                    my $ndst = (localtime $now)[8] > 0;
                    my $tdst = (localtime $then)[8] > 0;
                    $then - ($tdst - $ndst) * 60 * 60;
                    }

    Should give you "this time yesterday" in seconds since epoch relative to
    the first argument or the current time if no argument is given and
    suitable for passing to localtime or whatever else you need to do with
    it. $ndst is whether we're currently in daylight savings time; $tdst is
    whether the point 24 hours ago was in daylight savings time. If $tdst
    and $ndst are the same, a boundary wasn't crossed, and the correction
    will subtract 0. If $tdst is 1 and $ndst is 0, subtract an hour more
    from yesterday's time since we gained an extra hour while going off
    daylight savings time. If $tdst is 0 and $ndst is 1, subtract a negative
    hour (add an hour) to yesterday's time since we lost an hour.

    All of this is because during those days when one switches off or onto
    DST, a "day" isn't 24 hours long; it's either 23 or 25.

    The explicit settings of $ndst and $tdst are necessary because localtime
    only says it returns the system tm struct, and the system tm struct at
    least on Solaris doesn't guarantee any particular positive value (like,
    say, 1) for isdst, just a positive value. And that value can potentially
    be negative, if DST information isn't available (this sub just treats
    those cases like no DST).

    Note that between 2am and 3am on the day after the time zone switches
    off daylight savings time, the exact hour of "yesterday" corresponding
    to the current hour is not clearly defined. Note also that if used
    between 2am and 3am the day after the change to daylight savings time,
    the result will be between 3am and 4am of the previous day; it's
    arguable whether this is correct.

    This sub does not attempt to deal with leap seconds (most things don't).

  How do I validate input?
    The answer to this question is usually a regular expression, perhaps
    with auxiliary logic. See the more specific questions (numbers, mail
    addresses, etc.) for details.

Found in /me-FIRST-in-PATH-bin/perl-5.8.6/lib/5.8.6/pod/perlfaq5.pod
  Why do I sometimes get an "Argument list too long" when I use <*>?
    The "<>" operator performs a globbing operation (see above). In Perl
    versions earlier than v5.6.0, the internal glob() operator forks csh(1)
    to do the actual glob expansion, but csh can't handle more than 127
    items and so gives the error message "Argument list too long". People
    who installed tcsh as csh won't have this problem, but their users may
    be surprised by it.

    To get around this, either upgrade to Perl v5.6.0 or later, do the glob
    yourself with readdir() and patterns, or use a module like File::KGlob,
    one that doesn't use the shell to do globbing.

  How do I randomly update a binary file?
    If you're just trying to patch a binary, in many cases something as
    simple as this works:

        perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs

    However, if you have fixed sized records, then you might do something
    more like this:

        $RECSIZE = 220; # size of record, in bytes
        $recno   = 37;  # which record to update
        open(FH, "+<somewhere") || die "can't update somewhere: $!";
        seek(FH, $recno * $RECSIZE, 0);
        read(FH, $record, $RECSIZE) == $RECSIZE || die "can't read record $recno: $!";
        # munge the record
        seek(FH, -$RECSIZE, 1);
        print FH $record;
        close FH;

    Locking and error checking are left as an exercise for the reader. Don't
    forget them or you'll be quite sorry.

  How do I get a file's timestamp in perl?
    If you want to retrieve the time at which the file was last read,
    written, or had its meta-data (owner, etc) changed, you use the -M, -A,
    or -C file test operations as documented in perlfunc. These retrieve the
    age of the file (measured against the start-time of your program) in
    days as a floating point number. Some platforms may not have all of
    these times. See perlport for details. To retrieve the "raw" time in
    seconds since the epoch, you would call the stat function, then use
    localtime(), gmtime(), or POSIX::strftime() to convert this into
    human-readable form.

    Here's an example:

        $write_secs = (stat($file))[9];
        printf "file %s updated at %s\n", $file,
            scalar localtime($write_secs);

    If you prefer something more legible, use the File::stat module (part of
    the standard distribution in version 5.004 and later):

        # error checking left as an exercise for reader.
        use File::stat;
        use Time::localtime;
        $date_string = ctime(stat($file)->mtime);
        print "file $file updated at $date_string\n";

    The POSIX::strftime() approach has the benefit of being, in theory,
    independent of the current locale. See perllocale for details.

  How do I set a file's timestamp in perl?
    You use the utime() function documented in "utime" in perlfunc. By way
    of example, here's a little program that copies the read and write times
    from its first argument to all the rest of them.

        if (@ARGV < 2) {
            die "usage: cptimes timestamp_file other_files ...\n";
        }
        $timestamp = shift;
        ($atime, $mtime) = (stat($timestamp))[8,9];
        utime $atime, $mtime, @ARGV;

    Error checking is, as usual, left as an exercise for the reader.

    Note that utime() currently doesn't work correctly with Win95/NT ports.
    A bug has been reported. Check it carefully before using utime() on
    those platforms.

Found in /me-FIRST-in-PATH-bin/perl-5.8.6/lib/5.8.6/pod/perlfaq8.pod
  How do I set the time and date?
    Assuming you're running under sufficient permissions, you should be able
    to set the system-wide date and time by running the date(1) program.
    (There is no way to set the time and date on a per-process basis.) This
    mechanism will work for Unix, MS-DOS, Windows, and NT; the VMS
    equivalent is "set time".

    However, if all you want to do is change your time zone, you can
    probably get away with setting an environment variable:

        $ENV{TZ} = "MST7MDT";                  # unixish
        $ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms
        system "trn comp.lang.perl.misc";

  How can I measure time under a second?
    In general, you may not be able to. The Time::HiRes module (available
    from CPAN, and starting from Perl 5.8 part of the standard distribution)
    provides this functionality for some systems.

    If your system supports both the syscall() function in Perl as well as a
    system call like gettimeofday(2), then you may be able to do something
    like this:

        require 'sys/syscall.ph';

        $TIMEVAL_T = "LL";

        $done = $start = pack($TIMEVAL_T, ());

        syscall(&SYS_gettimeofday, $start, 0) != -1
                   or die "gettimeofday: $!";

           ##########################
           # DO YOUR OPERATION HERE #
           ##########################

        syscall( &SYS_gettimeofday, $done, 0) != -1
               or die "gettimeofday: $!";

        @start = unpack($TIMEVAL_T, $start);
        @done  = unpack($TIMEVAL_T, $done);

        # fix microseconds
        for ($done[1], $start[1]) { $_ /= 1_000_000 }

        $delta_time = sprintf "%.4f", ($done[0]  + $done[1]  )
                                                -
                                     ($start[0] + $start[1] );

  How do I timeout a slow event?
    Use the alarm() function, probably in conjunction with a signal handler,
    as documented in "Signals" in perlipc and the section on ``Signals'' in
    the Camel. You may instead use the more flexible Sys::AlarmCall module
    available from CPAN.

    The alarm() function is not implemented on all versions of Windows.
    Check the documentation for your specific version of Perl.

  How do I add a directory to my include path (@INC) at runtime?
    Here are the suggested ways of modifying your include path:

        the PERLLIB environment variable
        the PERL5LIB environment variable
        the perl -Idir command line flag
        the use lib pragma, as in
            use lib "$ENV{HOME}/myown_perllib";

    The latter is particularly useful because it knows about machine
    dependent architectures. The lib.pm pragmatic module was first included
    with the 5.002 release of Perl.




------------------------------

Date: Mon, 17 Oct 2005 14:19:46 GMT
From: "Pritchie" <info2005@remove-this-including-dot.bigbunker.com>
Subject: This worked... thanks :o)
Message-Id: <6qO4f.3414$sm1.2621@newsfe5-win.ntli.net>

"Brian McCauley" <nobull@mail.com> wrote in message
news:dgrias$ahj$1@redhat2.bham.ac.uk...
> Pritchie wrote TOFU:
>
> [ Please don't do that, it is rude. TOFU corrected ]
>
> > "Josef Moellers" <josef.moellers@fujitsu-siemens.com> wrote
> > > Pritchie wrote:
> > > >What is the best way to include common settings/values into a perl
> > > >script?
>  > >
> > > Take a look at "use", "require" and friends.
> >
> > ok so I now have 2 files in the same folder
> >
> > ==> START myvar.pm<==
> > print "my var: $sVar\n";
> > $sVar="Hello World";
> > ==> END <==
> >
> > and
> > ==> START myperl.pl <==
> > #!/usr/bin/perl
> > my $sVar = "help";
> > require myvar;
> > print "sVar[".$sVar."]\n";
> > ==> END <==
> >
> >
> > When I run myperl.pl I get the following
> >
> > bash-3.00# perl ./myperl.pl
> > my var:
> > sVar[help]
> >
> > I was hoping to get
> >
> > my var:help
> > sVar[Hello World]
> >
> > Am I missing something or can you not pass variables values for some
> > reason using "use" or "require"?
>
> Separate source files do not share lexically scoped variables because
> each file is a separate lexical scope.
>
> You need to use package scoped variables. Or possibly a single packaged
> scoped hash.
>
> Consider also using a proper Exporter module[1] or use fully qualified
> names as the quick-and-dirty
> Perl4-library-masquerading-as-a-Perl5-module approach will break down
> quite quickly.
>
> [1] The variables are declared in the module's own namespace then
> aliases are exported into other namespaces when they use() the module.
>
> Example without Exporter - just using fully qualified names.
>
> ==> START myvar.pm<==
> package myvar;
> use strict;
> use warnings;
>
> our $sVar="Hello World";
>
> 1;
> ==> END <==
>
>
> ==> START myperl.pl <==
> #!/usr/bin/perl
> use strict;
> use warnings;
> require myvar;
> print "sVar[".$myvar::sVar."]\n";
> ==> END <==
>

This worked... thanks






------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8532
***************************************


home help back first fref pref prev next nref lref last post