[32484] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3749 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 3 03:09:20 2012

Date: Fri, 3 Aug 2012 00:09:05 -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           Fri, 3 Aug 2012     Volume: 11 Number: 3749

Today's topics:
    Re: a regex for removing a password in a source listing (Tim McDaniel)
    Re: i18n for netnews (Seymour J.)
    Re: using a help file to populate ftp values <rweikusat@mssgmbh.com>
    Re: using a help file to populate ftp values <uri@stemsystems.com>
    Re: using a help file to populate ftp values (Tim McDaniel)
    Re: using a help file to populate ftp values <ben@morrow.me.uk>
    Re: using a help file to populate ftp values <cal@example.invalid>
    Re: using a help file to populate ftp values <cal@example.invalid>
    Re: using a help file to populate ftp values <cal@example.invalid>
    Re: using a help file to populate ftp values <ben@morrow.me.uk>
    Re: using a help file to populate ftp values (Tim McDaniel)
    Re: using a help file to populate ftp values <ben@morrow.me.uk>
    Re: using a help file to populate ftp values <uri@stemsystems.com>
    Re: using a help file to populate ftp values <uri@stemsystems.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 2 Aug 2012 16:03:46 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: a regex for removing a password in a source listing
Message-Id: <jve8d2$1pe$1@reader1.panix.com>

In article <87r4rponj9.fsf@stemsystems.com>,
Uri Guttman  <uri@stemsystems.com> wrote:
>>>>>> "CD" == Cal Dershowitz <cal@example.invalid> writes:
>
>  CD> On 07/31/2012 10:29 PM, Uri Guttman wrote:
>  >> since he is doing this in a script and not the command line, edit_file
>  >> or edit_file_lines from File::Slurp with do that easily
>  >> 
>  >> use File::Slurp qw( edit_file ) ;
>  >> 
>  >> edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;
>  >> 
>  >> done.
>
>  CD> ...in a way that unnecessarily ruins this line:
>
>  CD> $ftp->login($username, $password)       or die "Couldn't login\n";
>
>huh??

To expand on the "huh??" (which was my initial reaction too): in

>  >> edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;

(1) he didn't mean the literal word

        password

He meant whatever password or other text that you're trying to redact.
"whatevrUCh0se4APassw0rd", or whatever your password is, would not
match the text $password in your ftp->login line.

(2) Even if that line

>  >> edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;

were literally there, it can't modify

>  CD> $ftp->login($username, $password)       or die "Couldn't login\n";

Note that there are quotation marks in s/'password'/ -- those
quotation marks are part of the pattern being searched for and
substituted for, Since there are no quotation marks around $password
in

>  CD> $ftp->login($username, $password)       or die "Couldn't login\n";

the s/// wouldn't touch it.

(3) If you're going to do substitution to source code, there's a risk
of false positives no matter what you do -- the substitution code may
not bright enough to know the context; really, only a full Perl parser
could do a perfect job.  Metaphorically, it's inherently sinful, so
don't be surprised if you get hit by lightning.

If we were talking about multi-user systems, login information does
not belong in scripts, if for no other reason than that any user has
to be able to read the script (barring some work behind the scenes if
Linux) to be able to run it and can therefore find the login and
password.

When I needed it on a multi-user system, I had the public script read
it from a separate file.  The public script was in the source-code
repository, so anyone could see it.  The login info file was readable
and writable only by me and lived only on the system where I ran the
script.

Or at least you can assign it at the top

    my $username = 'Fred';
    my $password = 'Bloggs';

and use $username and $password throughout.  Then you don't have to
have the inherent riskiness of source substitution, and don't have to
bother with writing a program to do it -- you just delete them by
hand.

(Note to others: yeah, I consider

    use constant USERNAME => 'Fred';
    use constant PASSWORD => 'Bloggs';

to be better.  I just didn't want to get into "but here's how you'd
have to use it if you want its value in a quoted string, and there are
contexts where you need to write PASSWORD() instead of just PASSWORD",
and all that.)

>  >> to the OP: next time stop with the nonsense, the quine, the
>  >> logs, and just say you want to edit a file in place. pretty
>  >> simple concept but you said everything BUT that.
>
>  CD> Well, uri, I just came down from the mountains, and I've
>  CD> decided to turn over a few new leafs.  Maybe one of them could
>  CD> be with you.  Would you like me to try File::Slurp and see if
>  CD> it can work into my own meaningful projects?
>
>huh??

To expand on that "huh??": your reply there has nothing to do with his
comment, which echoed the comments others made.  (I would have
remarked the same if others hadn't done it first and better.)

To expand on his point there:

As I recall, you posted programs, possibly not runnable, and logs, and
other stuff.  Someone asked you to simply explain what the problem was
in plain English, and in reply (as I recall) you posted other code but
with none of the requested explanation.

I echo what other people have written.  When asking for help, please
start by just explaining what you want in plain English, along the
lines of

"I would like to take a script and remove a hard-coded password from
it so I can show it to other people."

"I am trying to run this code but it outputs

    Dingleberries detected, line 17 of frobotz.pl

and dies.  I am not using the dingleberry operator or Moose::Dingleberry."

"I am trying to connect to a Gopher server, but the connection doesn't
work.  I've connected to the Gopher server using the gopher command,
and the username and password work there."

Or whatever the problem is.

Below that, if you want to post code,
- Please give just enough code to exhibit the problem.
- Please provide runnable code.
- Please explain how to run the code -- what data file(s), what
  command-line arguments, whatever.

The idea is that someone can read the English and know whether they
can likely help, and at least what sorts of things to look for.  Then
they can read the small extract of code much easier that a large block
of code that has irrelevant details.  Then, if they like, they are
able to copy and paste the code onto their own machine to see if they
can reproduce your problem, and they can try editing the code to see
if they can fix it.

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Thu, 02 Aug 2012 11:55:07 -0400
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: i18n for netnews
Message-Id: <501aa2db$14$fuzhry+tra$mr2ice@news.patriot.net>

In <866292skzv.fsf_-_@gray.siamics.net>, on 08/01/2012
   at 10:13 PM, Ivan Shmakov <oneingray@gmail.com> said:

>	As of RFC 5536 (published November 2009), it's /allowed/ to use
>	national characters in Netnews article headers, /provided/ that
>	RFC 2047 is used to encode them in 7-bit ASCII.

Yes, but compare that to what is going on with e-mail; the
experimental RFC 5335, allowing raw UTF-8 in headers, has been
replaced by a standard track RFC, 6532. While RFC 3977 provides an
equivalent to 8BITMIME, there is no netnews equivalent to SMTPUTF8,
and I'm predicting that there will eventually be one.

BTW, I interpreted your "non-ASCII" as referring to octets beyond 127,
rather than to ASCII encoding of non-ASCII data. 

-- 
Shmuel (Seymour J.) Metz, SysProg and JOAT  <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action.  I reserve the
right to publicly post or ridicule any abusive E-mail.  Reply to
domain Patriot dot net user shmuel+news to contact me.  Do not
reply to spamtrap@library.lspace.org



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

Date: Thu, 02 Aug 2012 11:47:12 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: using a help file to populate ftp values
Message-Id: <877gth8tan.fsf@sapphire.mobileactivedefense.com>

Ben Morrow <ben@morrow.me.uk> writes:

[...]

>
> Why have you made your configuration file format look like Perl? Perl is
> not a particularly easy language to parse.

Except if the file also needs to be parsed by code not written in Perl,
the usual 'good reason' for that would be that perl can already parse
Perl: Configuration files using Perl syntax can be read with do FILE.




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

Date: Thu, 02 Aug 2012 15:35:53 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: using a help file to populate ftp values
Message-Id: <87lihxnl2e.fsf@stemsystems.com>

>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:

  BM> which you could then read into a hash something like this:

  BM>     my %info;
  BM>     {
  BM>         open my $INFO, "<", ...;
  BM>         while (<$INFO>) {
  BM>             chomp;
  BM>             my ($k, $v) = /^(\w+):\s+(.*)$/ 
  BM>                 or die "Incorrect format for password file: [$_]";
  BM>             $info{$k} = $v;
  BM>         }
  BM>         # no need to close, perl will do this for you when $INFO goes
  BM>         # out of scope
  BM>     }

  BM> (There are certainly more concise ways of doing this--I might use map
  BM> and File::Slurp--but this is simple and straightforward.)

my fave way to slurp and parse that style of config:

use File::Slurp ;
my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;

and that isn't any more complex than your longer loop and uses the same
regex (i changed \s+ to \s*). not even a need for map!

uri



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

Date: Thu, 2 Aug 2012 20:08:20 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: using a help file to populate ftp values
Message-Id: <jvemnk$gto$1@reader1.panix.com>

In article <87lihxnl2e.fsf@stemsystems.com>,
Uri Guttman  <uri@stemsystems.com> wrote:
>>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
>
>  BM> which you could then read into a hash something like this:
>
>  BM>     my %info;
>  BM>     {
>  BM>         open my $INFO, "<", ...;
>  BM>         while (<$INFO>) {
>  BM>             chomp;
>  BM>             my ($k, $v) = /^(\w+):\s+(.*)$/ 
>  BM>                 or die "Incorrect format for password file: [$_]";
>  BM>             $info{$k} = $v;
>  BM>         }
>  BM>         # no need to close, perl will do this for you when $INFO goes
>  BM>         # out of scope
>  BM>     }
>
>  BM> (There are certainly more concise ways of doing this--I might use map
>  BM> and File::Slurp--but this is simple and straightforward.)
>
>my fave way to slurp and parse that style of config:
>
>use File::Slurp ;
>my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;
>
>and that isn't any more complex than your longer loop and uses the
>same regex (i changed \s+ to \s*). not even a need for map!

I'm confused.  How does that work?  Isn't the right-hand side of =~
supposed to be a regex and the left-hand side a scalar?

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Thu, 2 Aug 2012 22:04:46 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: using a help file to populate ftp values
Message-Id: <ek6re9-doq1.ln1@anubis.morrow.me.uk>


Quoth Uri Guttman <uri@stemsystems.com>:
> >>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
> 
>   BM> which you could then read into a hash something like this:
> 
>   BM>     my %info;
>   BM>     {
>   BM>         open my $INFO, "<", ...;
>   BM>         while (<$INFO>) {
>   BM>             chomp;
>   BM>             my ($k, $v) = /^(\w+):\s+(.*)$/ 
>   BM>                 or die "Incorrect format for password file: [$_]";
>   BM>             $info{$k} = $v;
>   BM>         }
>   BM>         # no need to close, perl will do this for you when $INFO goes
>   BM>         # out of scope
>   BM>     }
> 
>   BM> (There are certainly more concise ways of doing this--I might use map
>   BM> and File::Slurp--but this is simple and straightforward.)
> 
> my fave way to slurp and parse that style of config:
> 
> use File::Slurp ;
> my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;

(Don't you mean read_file(...) =~ /.../g ?)

> and that isn't any more complex than your longer loop and uses the same
> regex (i changed \s+ to \s*). not even a need for map!

I agree. However, I have noticed here in the past that people who are
not familiar with Perl tend to find statements like that which do
several things at once confusing. Once you get used to the idea that, in
Perl, a statement has to be taken apart rather carefully to see what
each piece does, it is no more complex, but I suspect a beginner would
read that line as

    my %conf = OHMYGODWHATDOESTHATDO ($config_file);

:)

Ben



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

Date: Thu, 02 Aug 2012 18:51:17 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using a help file to populate ftp values
Message-Id: <MM6dnXdvy9YbvYbNnZ2dnUVZ_rqdnZ2d@supernews.com>

On 08/01/2012 09:17 PM, Jürgen Exner wrote:
> Cal Dershowitz<cal@example.invalid>  wrote:
>> Q1)  How could one read the word $domain or anything with a dollar sign
>> on it in the help file and then declare it at top scope in the script?
>
> Are you looking for a template system?

I tried to make something happen with Html::Template, but I'd say I came 
pretty close to a pratfall there.  Since you asked the question, I've 
googled and read up on it.

Are you /is anyone/ familiar with the badger book?  Now that I'm 
finished with _Learning Perl_ for the second time, I think I need to 
repopulate my reading.  Today, I customized ben's %info hash to be able 
to successfully open an ftp object, so I think I'm King Shit, while this 
might be a very modest pre-requisite for this material.

My basic question is whether you think it's too hard for me to use 
effectively.  It sounds wonderful:  I'd love to have the capability.
-- 
Cal



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

Date: Thu, 02 Aug 2012 18:56:15 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using a help file to populate ftp values
Message-Id: <MM6dnXZvy9YyvIbNnZ2dnUVZ_rqdnZ2d@supernews.com>

On 08/02/2012 04:47 AM, Rainer Weikusat wrote:
> Ben Morrow<ben@morrow.me.uk>  writes:
>
> [...]
>
>>
>> Why have you made your configuration file format look like Perl? Perl is
>> not a particularly easy language to parse.
>
> Except if the file also needs to be parsed by code not written in Perl,
> the usual 'good reason' for that would be that perl can already parse
> Perl: Configuration files using Perl syntax can be read with do FILE.
>
>

I don't understand.  Can you elaborate?
-- 
Cal


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

Date: Thu, 02 Aug 2012 19:16:15 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using a help file to populate ftp values
Message-Id: <Ib-dnYbKruz5u4bNnZ2dnUVZ_jWdnZ2d@supernews.com>

On 08/01/2012 11:53 PM, Ben Morrow wrote:
>
> Quoth Cal Dershowitz<cal@example.invalid>:
>>
>> $ perl safe_post4.pl
>> matched
>> my $domain = 'www.bedrock.com';
>> word is www.bedrock.com
>> matched
>> my $username = 'barney';
>> word is barney
>> matched
>> my $password = 'rocks';
>
> I hope this isn't your real password...

Yeah, I figure I have to keep a secret from fred, and since he and I 
work with 'boulders' all day, I decided to be less obvious.
>
>> word is rocks
>> $ cat safe_post4.pl
>> #!/usr/bin/perl -w
>> use strict;
>> use 5.010;
>> use Net::FTP;
>> my ( $domain, $username, $password );
>> my $identity_file = 'ftp_passwords_1.txt';
>> my ( @lines, $word );
>> open( my $gh, '<', $identity_file )
>>     or die("Can't open $identity_file  for writing: $!");
>
> *Reading*. If you use 'autodie' it will get this right for you.

autodie works on file open and closes but not others?
>
>> chomp( @lines =<$gh>  );
>> close $gh;
>>
>> foreach (@lines) {
>>       if (/\$domain/) {
>>           print "matched\n";
>>           print $_ . "\n";
>>           m/'(.*)'/;
>>           $domain = $1;
>>           print "word is $domain\n";
>>       }
>> }
>>
>> foreach (@lines) {
>
> Why are you iterating over the lines multiple times? You can do several
> matches in one loop.

I couldn't figure out how.
>
> Why have you made your configuration file format look like Perl? Perl is
> not a particularly easy language to parse. I would suggest something
> simpler; perhaps
>
>      domain: ftp.foo.com
>      username: cal
>      password: xxxx
>
> which you could then read into a hash something like this:
>
>      my %info;
>      {
>          open my $INFO, "<", ...;
>          while (<$INFO>) {
>              chomp;
>              my ($k, $v) = /^(\w+):\s+(.*)$/
>                  or die "Incorrect format for password file: [$_]";
>              $info{$k} = $v;
>          }
>          # no need to close, perl will do this for you when $INFO goes
>          # out of scope
>      }

This works great, ben, and it's definitely the way forward for my ftp 
projects:
$ perl safe_post5.pl
Net::FTP>>> Net::FTP(2.77)
 ...
Net::FTP=GLOB(0x970a83c)<<< 226 Transfer complete
ARRAY(0x974c398)$ cat safe_post5.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use autodie;
my %info;
my $identity_file = 'my_ftp.txt';

open( my $gh, '<', $identity_file );

while (<$gh>) {
     chomp;
     my ( $k, $v ) = /^\s*(\w+):\s+(.*)$/
       or die "Incorrect format for password file: [$_]";
     $info{$k} = $v;
}
# print "@{[ %info ]}\n";
#
# print "$info{domain}\n";

my $ftp      = Net::FTP->new( $info{domain}, Debug => 1, Passive => 1 )
   or die "Can't connect: $@\n";
$ftp->login( $info{username}, $info{password} ) or die "Couldn't login\n";

print scalar ( $ftp->ls());
$

I got the syntax for printing a hash from _Perl Cookbook_.  I've got my 
own format for my internet and other identities, but better to ask what 
others do, because I might benefit from their experience and techniques.

Q2)  Why didn't my little print statement at the end give me a scalar 
that I could associate with success?
>
> (There are certainly more concise ways of doing this--I might use map
> and File::Slurp--but this is simple and straightforward.)
>
> You could also use one of the existing config file parsers, perhaps
> YAML::XS or Config::Simple, though for something this trivial it
> probably isn't worth it.

I see that YAML is a section in _Intermediate Perl_.
>
>> Q1)  How could one read the word $domain or anything with a dollar sign
>> on it in the help file and then declare it at top scope in the script?
>
> What is 'the help file'? This file you've put your passwords in?
>
> You can't, at least not without trickery of some sort. The whole point
> of lexical ('my') variables is that they have to be declared where you
> can see them, in the file (and scope) you're looking at. You could
> require a file which set some globals, but that's not a good idea for
> all the usual reasons.

But I can declare them global, given that I have them in a hash already, 
without their dollar signs?
-- 
Cal



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

Date: Fri, 3 Aug 2012 03:10:12 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: using a help file to populate ftp values
Message-Id: <4hore9-jit1.ln1@anubis.morrow.me.uk>


Quoth Cal Dershowitz <cal@example.invalid>:
> On 08/01/2012 11:53 PM, Ben Morrow wrote:
> > Quoth Cal Dershowitz<cal@example.invalid>:
> >>
> >> open( my $gh, '<', $identity_file )
> >>     or die("Can't open $identity_file  for writing: $!");
> >
> > *Reading*. If you use 'autodie' it will get this right for you.
> 
> autodie works on file open and closes but not others?

Read the documentation. By default it affects all the builtin functions
which indicate failure through their return value (except system, for
technical reasons), but you can ask for something else.

> >> foreach (@lines) {
> >>       if (/\$domain/) {
> >>           print "matched\n";
> >>           print $_ . "\n";
> >>           m/'(.*)'/;
> >>           $domain = $1;
> >>           print "word is $domain\n";
> >>       }
> >> }
> >>
> >> foreach (@lines) {
> >
> > Why are you iterating over the lines multiple times? You can do several
> > matches in one loop.
> 
> I couldn't figure out how.

Take the 'if' statement out of each loop, and put them one after another
in one loop. Obviously if you alter $_ in one 'if' that will carry
through to the next...

What did you try?

> # print "@{[ %info ]}\n";

Data::Dumper is useful for this sort of thing. (Also Data::Dump, from
CPAN, which IMHO produces output which is easier to understand.)

> print scalar ( $ftp->ls());
> $
> 
> I got the syntax for printing a hash from _Perl Cookbook_.  I've got my 
> own format for my internet and other identities, but better to ask what 
> others do, because I might benefit from their experience and techniques.
> 
> Q2)  Why didn't my little print statement at the end give me a scalar 
> that I could associate with success?

What did it give you? Something like 'ARRAY(0xdeadbeef)'? That's what
you get if you stringify an array ref, which is what ->ls returns in
scalar context if it's successful. Again, Data::Dump(er) is useful for
printing out this sort of thing.

> > You can't, at least not without trickery of some sort. The whole point
> > of lexical ('my') variables is that they have to be declared where you
> > can see them, in the file (and scope) you're looking at. You could
> > require a file which set some globals, but that's not a good idea for
> > all the usual reasons.
> 
> But I can declare them global, given that I have them in a hash already, 
> without their dollar signs?

 ...No? I'm not sure what you mean; can you give an example?

Ben



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

Date: Fri, 3 Aug 2012 02:20:52 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: using a help file to populate ftp values
Message-Id: <jvfci4$kh9$1@reader1.panix.com>

In article <MM6dnXZvy9YyvIbNnZ2dnUVZ_rqdnZ2d@supernews.com>,
Cal Dershowitz  <cal@example.invalid> wrote:
>On 08/02/2012 04:47 AM, Rainer Weikusat wrote:
>> Ben Morrow<ben@morrow.me.uk>  writes:
>>
>> [...]
>>
>>>
>>> Why have you made your configuration file format look like Perl?
>>> Perl is not a particularly easy language to parse.
>>
>> Except if the file also needs to be parsed by code not written in
>> Perl, the usual 'good reason' for that would be that perl can
>> already parse Perl: Configuration files using Perl syntax can be
>> read with do FILE.
>
>I don't understand.  Can you elaborate?

English prepositions and conjunctions can be tricky.
s/Except if/Unless/.  That is,

Unless the file also needs to be parsed by code not written in Perl,
the usual "good reason" for making configuration files look like Perl
is that Perl can already parse Perl: Configuration files using Perl
syntax can be read with do FILE.  That is extremely easy to implement
and is very powerful.

For example, my $ORKPLACE has a config file that's Perl.  The last
value evaluated is the config info, in the form of an anonymous hash
along the lines of

    {
        servername1 => {
            port => 666,
            database => 'Fred',
            email => 'tmcd@panix.com',
            frobotz => 17,
            env => {
                home = '...',
                ...
            },
            ...
        },

        servername2 => {
            port => 777,
            database => 'Bloggs',
            email => 'tmcd@panix.com',
            frobotz => 17,
            ...
        },
    }

Since it's Perl, I can refer to $ENV{HOME} and such to get environment
variables; I do that a lot and it's very handy.  I could do `...` if I
needed to run a command to get a config parameter.  There are many
duplicated lines; some people start the file with something like, I
think (I only saw this once):

    my @common = (
        email => 'tmcd@panix.com',
        frobotz => 17,
        ...
    );

and then define a server like

        servername1 => {
            @common,
            port => 666, ...
        }

I could define subs to generate chunks if I wanted, and call subs and
use modules.

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Fri, 3 Aug 2012 06:13:11 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: using a help file to populate ftp values
Message-Id: <783se9-gd32.ln1@anubis.morrow.me.uk>


Quoth tmcd@panix.com:
> In article <MM6dnXZvy9YyvIbNnZ2dnUVZ_rqdnZ2d@supernews.com>,
> Cal Dershowitz  <cal@example.invalid> wrote:
> >On 08/02/2012 04:47 AM, Rainer Weikusat wrote:
> >>
> >> Except if the file also needs to be parsed by code not written in
> >> Perl, the usual 'good reason' for that would be that perl can
> >> already parse Perl: Configuration files using Perl syntax can be
> >> read with do FILE.
> >
> >I don't understand.  Can you elaborate?
> 
> English prepositions and conjunctions can be tricky.
> s/Except if/Unless/.  That is,
> 
> Unless the file also needs to be parsed by code not written in Perl,
> the usual "good reason" for making configuration files look like Perl
> is that Perl can already parse Perl: Configuration files using Perl
          ^^^^
          perl

Since we're being picky... :)

Ben



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

Date: Fri, 03 Aug 2012 01:23:00 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: using a help file to populate ftp values
Message-Id: <87hasko8gb.fsf@stemsystems.com>

>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:

  BM> Quoth Uri Guttman <uri@stemsystems.com>:
  >> >>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
  >> 
  BM> which you could then read into a hash something like this:
  >> 
  BM> my %info;
  BM> {
  BM> open my $INFO, "<", ...;
  BM> while (<$INFO>) {
  BM> chomp;
  BM> my ($k, $v) = /^(\w+):\s+(.*)$/ 
  BM> or die "Incorrect format for password file: [$_]";
  BM> $info{$k} = $v;
  BM> }
  BM> # no need to close, perl will do this for you when $INFO goes
  BM> # out of scope
  BM> }
  >> 
  BM> (There are certainly more concise ways of doing this--I might use map
  BM> and File::Slurp--but this is simple and straightforward.)
  >> 
  >> my fave way to slurp and parse that style of config:
  >> 
  >> use File::Slurp ;
  >> my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;

  BM> (Don't you mean read_file(...) =~ /.../g ?)

yes. brain fart. i have written that on the fly on blackboards
correctly. so i screwed up on usenet. :)

  >> and that isn't any more complex than your longer loop and uses the same
  >> regex (i changed \s+ to \s*). not even a need for map!

  BM> I agree. However, I have noticed here in the past that people who are
  BM> not familiar with Perl tend to find statements like that which do
  BM> several things at once confusing. Once you get used to the idea that, in
  BM> Perl, a statement has to be taken apart rather carefully to see what
  BM> each piece does, it is no more complex, but I suspect a beginner would
  BM> read that line as

  BM>     my %conf = OHMYGODWHATDOESTHATDO ($config_file);

yeah but i deal with all sorts of perl types. i support some who would
not get that line but like file::slurp in general. others will yell to
use a config file module (which one??). my point is to show how nicely
perl's parts work together. parsing keys/values and assigning directly
to a hash is just beautiful. i just made the string being parsed the
output of read_file().

uri


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

Date: Fri, 03 Aug 2012 01:23:44 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: using a help file to populate ftp values
Message-Id: <87d338o8f3.fsf@stemsystems.com>

>>>>> "TM" == Tim McDaniel <tmcd@panix.com> writes:

  TM> In article <87lihxnl2e.fsf@stemsystems.com>,
  TM> Uri Guttman  <uri@stemsystems.com> wrote:
  >>>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
  >> 
  BM> which you could then read into a hash something like this:
  >> 
  BM> my %info;
  BM> {
  BM> open my $INFO, "<", ...;
  BM> while (<$INFO>) {
  BM> chomp;
  BM> my ($k, $v) = /^(\w+):\s+(.*)$/ 
  BM> or die "Incorrect format for password file: [$_]";
  BM> $info{$k} = $v;
  BM> }
  BM> # no need to close, perl will do this for you when $INFO goes
  BM> # out of scope
  BM> }
  >> 
  BM> (There are certainly more concise ways of doing this--I might use map
  BM> and File::Slurp--but this is simple and straightforward.)
  >> 
  >> my fave way to slurp and parse that style of config:
  >> 
  >> use File::Slurp ;
  >> my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;
  >> 
  >> and that isn't any more complex than your longer loop and uses the
  >> same regex (i changed \s+ to \s*). not even a need for map!

  TM> I'm confused.  How does that work?  Isn't the right-hand side of =~
  TM> supposed to be a regex and the left-hand side a scalar?

yep. see my reply to ben. 

uri


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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 V11 Issue 3749
***************************************


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