Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Re: a regex for removing a password in a source listing (http://www.velocityreviews.com/forums/t948911-re-a-regex-for-removing-a-password-in-a-source-listing.html)

Wolf Behrenhoff 08-01-2012 10:11 AM

Re: a regex for removing a password in a source listing
 
Am 01.08.2012 08:00, schrieb Cal Dershowitz:
> if (/\$domain\) {
> print "matched\n";
> $word =~ m/


This is the whole match! From the first / right after the if to the
second one which only appears 2 lines later.

You simply forgot to end the match with / and used the \ instead.



Jürgen Exner 08-02-2012 03:17 AM

Re: using a help file to populate ftp values
 
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?

jue

Rainer Weikusat 08-02-2012 10:47 AM

Re: using a help file to populate ftp values
 
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.



Uri Guttman 08-02-2012 07:35 PM

Re: using a help file to populate ftp values
 
>>>>> "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


Tim McDaniel 08-02-2012 08:08 PM

Re: using a help file to populate ftp values
 
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

Tim McDaniel 08-03-2012 02:20 AM

Re: using a help file to populate ftp values
 
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

Uri Guttman 08-03-2012 05:23 AM

Re: using a help file to populate ftp values
 
>>>>> "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

Uri Guttman 08-03-2012 05:23 AM

Re: using a help file to populate ftp values
 
>>>>> "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

Rainer Weikusat 08-03-2012 12:23 PM

Re: using a help file to populate ftp values
 
tmcd@panix.com (Tim McDaniel) writes:

[...]

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


That's a Germanism. My bad. The German equivalent of 'unless' is
'ausser wenn' and the/a literal translation of that is 'except if'
(Possibly, 'except when'. So far, I haven't managed to figure out the
precise differences of if, whether and when in conditional clause :-).

OTOH, this has offered the Wilbur(?) guy a nice opportunity for some
additional rants. After all, my English is decidedly homegrown and
this comes accross as 'more or less retarded' to a native speaker who
doesn't really consider the circumstances (can't even talk =>
imbecile).


Rainer Weikusat 08-04-2012 05:09 PM

Re: using a help file to populate ftp values
 
Cal Dershowitz <cal@example.invalid> writes:

[...]

> My question is how to start out with what I have and end up having this bit:
>
> 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";


Assuming there's a configuration file named /tmp/ftp-config with the
following content:

----------------
{
my_ftp => {
domain => 'ftp.foo.com',
username => 'cal',
password => 'rocks',
frobnitz => 17
},

lutherhaven => {
domain => 'www.lh.com',
username => 'fred',
password => 'boulders',
frobnitz => 42,
},
}
----------------

the sample code below can be used to parse this config file and print
the domain, username and password setting for a given 'ftp domain'
passed to the script as first command-line argument

----------------
my ($config, $domain);

$config = do('/tmp/ftp-config');
unless ($config) {
die("read error: $!") if $!;
die("parse error: $@") if $@;
}

$domain = $config->{$ARGV[0]};
die("unknown domain: $ARGV[0]") unless $domain;

printf("server\t%s\nuser\t%s\npass\t%s\n",
$domain->{domain},
$domain->{username},
$domain->{password});
-----------------

Example:

[rw@sapphire]/tmp $perl sample.pl lutherhaven
server www.lh.com
user fred
pass boulders



All times are GMT. The time now is 04:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.