[21984] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4206 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Dec 2 18:10:44 2002

Date: Mon, 2 Dec 2002 15:10:15 -0800 (PST)
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, 2 Dec 2002     Volume: 10 Number: 4206

Today's topics:
    Re: Regular expressions <Gary@galnet.co.uk>
        search text file, find substring, beginner help pls. (Suzanne Barron)
    Re: search text file, find substring, beginner help pls <tassilo.parseval@post.rwth-aachen.de>
        Show pictures? <mail@eircom.net>
    Re: Show pictures? (Walter Roberson)
        sys/wait.ph problem (David Turley)
    Re: sys/wait.ph problem (Walter Roberson)
    Re: Timezone <ddunham@redwood.taos.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 2 Dec 2002 13:40:24 -0000
From: "Gary" <Gary@galnet.co.uk>
Subject: Re: Regular expressions
Message-Id: <ioKG9.620$%d1.120615@newsfep2-gui>

Thanks
    Previous posts appear to have kept me busy.  They seem to have done the
trick so far.

cheers though

Gary

"Dave Cross" <dave@dave.org.uk> wrote in message
news:pan.2002.12.02.14.01.00.351019@dave.org.uk...
> [please don't top-post in this newsgroup]
>
> On Mon, 02 Dec 2002 09:54:32 +0000, Gary wrote:
>
> > Thanks Tassilo,
> >    I knew of the reference, but not the tutorial.
>
> There's also perlrequick.
>
> Dave...
>
> --
>   Love is a fire of flaming brandy
>   Upon a crepe suzette
>




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

Date: 2 Dec 2002 14:07:01 -0800
From: sbarron01@hotmail.com (Suzanne Barron)
Subject: search text file, find substring, beginner help pls.
Message-Id: <a583e5a9.0212021407.3bca862f@posting.google.com>

I was asked to write a batch file which would get the UserTemplates
directory from the NT registry and, using this value, copy a file
there because some users have problems copying a file to their
templates directory. I tried to do it in an ms-dos batch file but ran
into probs. with a multi-line pathname (regedit saves the value as hex
and it took up 3 lines). I was asked to use Perl which I've never used
but had success today writing to a file and the pathname shows up in
ascii which is very good. Question: how do I search a file for an
entry but then get only part of the line? My batch file saved the
registry value (UserTemplates=C:\Documents and Settings) and I was
able to search for 'UserTemplates' and get everything after the '='
and save it to a variable. How can I do this with Perl?
Here's the beginning lines from the batch file:

:: Export settings from registry to a temporary file
START /W REGEDIT /E  %Temp%.\test_reg.sb
"HKEY_CURRENT_USER\Software\Microsoft\Office\9.0\Common\General"
:: Read from the temporary file and store in variable
FOR /F "tokens=1,* delims==" %%A IN ('TYPE %Temp%.\test_reg.sb ^| FIND
"UserTemplates"') DO SET iTemplate=%%B

In my Perl script, I can save the values to a file but I don't know
how to search the file to find the value after 'UserTemplates='. I
want to be able to copy a templates .dot file to this directory.

use Win32::Registry;
$p = "Software\\Microsoft\\Office\\9.0\\Common\\General";
$main::HKEY_CURRENT_USER->Open($p, $General) || 
        die "Open: $!";
open(fileOUT, ">sb.txt") || dienice("Can't open sb.txt: $!");
seek(fileOUT, 0, 0);

$General->GetValues(\%vals);
foreach $k (keys %vals) {
	$key = $vals{$k};
print fileOUT "$$key[0] = $$key[2]\n";

}
close(fileOUT);

I've been looking thru Learning Perl on Win32 Systems and Advanced
Perl Programming (books here at work) but I can't find documentation
on how to search a file.
Thanks,
Suzanne


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

Date: 2 Dec 2002 22:53:03 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: search text file, find substring, beginner help pls.
Message-Id: <asgo8f$9cp$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Suzanne Barron:

> I was asked to write a batch file which would get the UserTemplates
> directory from the NT registry and, using this value, copy a file
> there because some users have problems copying a file to their
> templates directory. I tried to do it in an ms-dos batch file but ran
> into probs. with a multi-line pathname (regedit saves the value as hex
> and it took up 3 lines). I was asked to use Perl which I've never used
> but had success today writing to a file and the pathname shows up in
> ascii which is very good. Question: how do I search a file for an
> entry but then get only part of the line? My batch file saved the
> registry value (UserTemplates=C:\Documents and Settings) and I was
> able to search for 'UserTemplates' and get everything after the '='
> and save it to a variable. How can I do this with Perl?
> Here's the beginning lines from the batch file:
> 
>:: Export settings from registry to a temporary file
> START /W REGEDIT /E  %Temp%.\test_reg.sb
> "HKEY_CURRENT_USER\Software\Microsoft\Office\9.0\Common\General"
>:: Read from the temporary file and store in variable
> FOR /F "tokens=1,* delims==" %%A IN ('TYPE %Temp%.\test_reg.sb ^| FIND
> "UserTemplates"') DO SET iTemplate=%%B

Uggh, I can't even decipher that.

> In my Perl script, I can save the values to a file but I don't know
> how to search the file to find the value after 'UserTemplates='. I
> want to be able to copy a templates .dot file to this directory.

That's a good job for a regular expression. You don't tell us how the
line exactly looks, so I suspect you want everything following
'UserTemplates='.

Assuming you have a variable with a line containing such an entry:

    my ($wanted) = $line =~ /UserTemplates=(.*)/;
          # ^--------<------<-------<-------|--|

$line is matched against the pattern 'UserTemplates=(.*)'. The parens ()
tell perl to capture everything that matches the pattern in between the
parens. This is '.*' here which means: Any character zero or more times,
but be greedy. That means, match as much as possible.

Since the pattern matching ( $var =~ /pattern/ ) happens on the right
hand side of a list expression (called list context), the match returns
everything that was returned by the capturing pair of parens in the
pattern. We had only one pair of such parens here and so all of that is
stored in $wanted. Similarily:

    my ($first, $second) = $line =~ /key1=(.*?)key2=(.*)/;

where the stuff matched by '.*?' (match anything, but as short as
possible) would go into $first while the matching of '.*' into $second.

> use Win32::Registry;
> $p = "Software\\Microsoft\\Office\\9.0\\Common\\General";
> $main::HKEY_CURRENT_USER->Open($p, $General) || 
>         die "Open: $!";
> open(fileOUT, ">sb.txt") || dienice("Can't open sb.txt: $!");
> seek(fileOUT, 0, 0);
> 
> $General->GetValues(\%vals);
> foreach $k (keys %vals) {
> 	$key = $vals{$k};
> print fileOUT "$$key[0] = $$key[2]\n";
> 
> }
> close(fileOUT);

Considering this was your first contact with Perl, this already looks
quite promising.

> I've been looking thru Learning Perl on Win32 Systems and Advanced
> Perl Programming (books here at work) but I can't find documentation
> on how to search a file.

Searching a file commonly happens by traversing the file linewise and do
some tests on each of these lines. In your above code you open the file
for writing, whereas you now want a read-access:

    open IN, "<sb.txt" or die $!;
    while (<IN>) { # as long as a readline on the file produces something
        my ($templates) = $_ =~ /UserTemplates=(.*)/;
        ...
    }

The 'while(<HANDLE>)' idiom stores the next chunk read from the file in
the special variable $_. $_ is also the variable that is the default
target of many Perl functions and operators, so the match can be written
more nicely:

    while (<IN>) {
        my ($templates) = /UserTemplaces=(.*)/;
        ...
    }
    close IN;

Which leaves one question: Do you really need this intermediate step of
writing to the file and afterwards retrieving its content? A pattern
match can be applied to any scalar-ish Perl value:

    ($res) = "string" =~ /PATTERN/;
    ($res) = $string  =~ /PATTERN/;
    ($res) = $array[0] =~ /PATTERN/;
    ($res) = $hash{key} =~ /PATTERN/;
    ($res) = $ref->{key} =~ /PATTERN/;
    # etc

Perhaps you can diddle the match somewhere into your foreach loop and
thus save the file altogether.

By the way, all of the above techniques can be found in the
documentation shipped with Perl (these docs are much better than any
book I know of, even suitable for those new to Perl):

    perldoc perlretut   (regular expression tutorial)
    perldoc perlre      (regular expression reference)
    perldoc perlopentut (tutorial for open())
    perldoc perlop      (Perl operators, m// is one of them)
    perldoc -f FUNCTION (documentation for particular function)
    perldoc perlfunc    (all functions, with categorization)
    
Since you are using Perl under Windows, you probably have ActiveState's
Perl in which case all of these manpages come as html-documents with
nice cross-linking.

Tassilo
-- 
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;


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

Date: Mon, 2 Dec 2002 21:06:27 -0800
From: "Ireland" <mail@eircom.net>
Subject: Show pictures?
Message-Id: <asge4l$e61$1@dorito.esatclear.ie>

Is it possible for perl to show a picture in a script?

Heres the script. When i run the script can a  picture  show up for 5
seconds, close and continue on??
How do I do this ?


script below-----------------
#usr/bin/perl

#would like the picture to show up here for 5 seconds

#open file
open (REPLY, ">reply.txt");
$reply = REPLY;

#clear out contents
print clear $reply;

#close file
close (REPLY);

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

Thanks




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

Date: 2 Dec 2002 21:48:11 GMT
From: roberson@ibd.nrc.ca (Walter Roberson)
Subject: Re: Show pictures?
Message-Id: <asgker$89l$1@canopus.cc.umanitoba.ca>

In article <asge4l$e61$1@dorito.esatclear.ie>, Ireland <mail@eircom.net> wrote:
:Is it possible for perl to show a picture in a script?

:Heres the script. When i run the script can a  picture  show up for 5
:seconds, close and continue on??
:How do I do this ?

You gave us absolutely no clue about platform or connection method
or as to what you mean by "picture".


   #usr/bin/perl

That's just a comment unless you use #! instead of #.
And you should be providing an absolute path instead of a relative one.
You should probably also be turning on warnings.

#!/usr/bin/perl -w

   #would like the picture to show up here for 5 seconds

   #open file
   open (REPLY, ">reply.txt");

And if the file cannot be opened, what should happen? It is likely
that you will not be able to open the file, as you have not taken
any care about which directory is current. If it's the same directory
for everyone, then how are you going to avoid the possibility that
two people are running at the same time? How are you going to avoid
the problem that someone running the program again is going to
overwrite the first reply?

   $reply = REPLY;

Unusual statement. If you are going to be copying filehandles, you
would normally open the file using IO::Handle or similar techniques.

   #clear out contents
   print clear $reply;

That definitely doesn't do what you want. That tries to print the value
of the scalar variable named "reply" to the filehandle named "clear".

   #close file
   close (REPLY);
--
   History is a pile of debris               -- Laurie Anderson


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

Date: 2 Dec 2002 08:00:24 -0800
From: dturley@pobox.com (David Turley)
Subject: sys/wait.ph problem
Message-Id: <66216bd2.0212020800.72467f68@posting.google.com>

I'm trying to run a script that requires sys/wait.ph:

I get this error:
david:~/sans/mailmerge_port$ ./mailmerge
syntax error at /usr/lib/perl5/5.6.1/i386-linux/features.ph line 149,
near ") ("
syntax error at /usr/lib/perl5/5.6.1/i386-linux/features.ph line 155,
near "}"
Compilation failed in require at
/usr/lib/perl5/5.6.1/i386-linux/sys/wait.ph line 5.
Compilation failed in require at ./mailmerge line 2.

mailmerge line 2 is:
require "sys/wait.ph";

syntax check is okay on sys/wait.ph
fails with same error on features.ph

david:~/sans/mailmerge_port$ perl -c
/usr/lib/perl5/5.6.1/i386-linux/features.ph
syntax error at /usr/lib/perl5/5.6.1/i386-linux/features.ph line 149,
near ") ("
syntax error at /usr/lib/perl5/5.6.1/i386-linux/features.ph line 155,
near "}"
/usr/lib/perl5/5.6.1/i386-linux/features.ph had compilation errors.
david:~/sans/mailmerge_port$ perl -c
/usr/lib/perl5/5.6.1/i386-linux/sys/wait.ph
/usr/lib/perl5/5.6.1/i386-linux/sys/wait.ph syntax Ok

to my untrained eyes is seems weird to get compilation errors on a
stock perl installation, perhaps the error mesages are misleading?

Ideas and suggestions welcome


This is perl, v5.6.1 built for i386-linux, on Mandrake 8.2


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

Date: 2 Dec 2002 16:58:15 GMT
From: roberson@ibd.nrc.ca (Walter Roberson)
Subject: Re: sys/wait.ph problem
Message-Id: <asg3f7$p4$1@canopus.cc.umanitoba.ca>

In article <66216bd2.0212020800.72467f68@posting.google.com>,
David Turley <dturley@pobox.com> wrote:
:I'm trying to run a script that requires sys/wait.ph:

:I get this error:
:david:~/sans/mailmerge_port$ ./mailmerge
:syntax error at /usr/lib/perl5/5.6.1/i386-linux/features.ph line 149,
:near ") ("

:to my untrained eyes is seems weird to get compilation errors on a
:stock perl installation, perhaps the error mesages are misleading?

I encountered a similar situation recently in sys/wait.ph
on SGI's IRIX. On IRIX, I traced it down to the fact that 
the standards dealing with macros such as WIFEXITED() 
{see the wait(2) man page} indicate that those should be passed
in a pointer to an integer, but the conditional compiles 
in <sys/wait.h> were such that under nearly all useful compilation
options, the macros would instead expect a pointer to a union
and would proceed to type-pun the pointer. The .h -> .ph conversion
utility, not expecting the type-pun, was leaving in the
de-referencing '*' that C uses, and that was leading to perl
errors as '*' is not a valid unary operator in perl.

I found this on one of SGI's wait(2) man pages:

  Historically, many BSD derived programs pass as the status
  pointer, a pointer to a union wait, rather than a pointer to int. 


I make no claim that the issue you face is exactly the same, but
it gives you something to look at.
--
Before responding, take into account the possibility that the Universe
was created just an instant ago, and that you have not actually read
anything, but were instead created intact with a memory of having read it.


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

Date: Mon, 02 Dec 2002 18:39:38 GMT
From: Darren Dunham <ddunham@redwood.taos.com>
Subject: Re: Timezone
Message-Id: <KNNG9.416$p71.5792078@newssvr14.news.prodigy.com>

BrianWhitehead <bgwhite@uofu.net> wrote:
> Howdy,

> I receive data from all different timezones.  I usually convert the
> data and store it in one common timezone.

> In the past I do this by minipulating the TZ variable.  For example:

> $ENV{TZ} = "GMT+8";
> print scalar localtime;
> print "\n";

> $ENV{TZ} = "GMT+9";
> print scalar localtime;
> print "\n";

> This is supposed to print out two different times... it does work on a
> Sun box, but when I move to a linux (Redhat 7.2, Redhat 7.3 and Redhat
> 8.0), it doesn't work

Works fine on a redhat box here..

# uname -a
Linux tester 2.4.18-5smp #1 SMP Mon Jun 10 15:19:40 EDT
2002 i686 unknown
# perl /tmp/perl
Mon Dec  2 10:36:16 2002
Mon Dec  2 09:36:16 2002
# perl -v

This is perl, v5.6.1 built for i386-linux
[...]

> From the command line it does work on linux, for example:
> perl -e '$ENV{TZ}="GMT+8"; print scalar localtime, "\n"'
> perl -e '$ENV{TZ}="GMT+9"; print scalar localtime, "\n"'

Verrry interesting.  That would seem to rule out problems with the
actual time libraries.  

> Does anyone have any help?

What version of perl is on the Linux box?

-- 
Darren Dunham                                           ddunham@taos.com
Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >


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

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.  

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 4206
***************************************


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