Re: a regex for removing a password in a source listing

Discussion in 'Perl Misc' started by Wolf Behrenhoff, Aug 1, 2012.

  1. 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.
     
    Wolf Behrenhoff, Aug 1, 2012
    #1
    1. Advertising

  2. Re: using a help file to populate ftp values

    Cal Dershowitz <> 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
     
    Jürgen Exner, Aug 2, 2012
    #2
    1. Advertising

  3. Re: using a help file to populate ftp values

    Ben Morrow <> 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.
     
    Rainer Weikusat, Aug 2, 2012
    #3
  4. Wolf Behrenhoff

    Uri Guttman Guest

    Re: using a help file to populate ftp values

    >>>>> "BM" == Ben Morrow <> 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
     
    Uri Guttman, Aug 2, 2012
    #4
  5. Wolf Behrenhoff

    Tim McDaniel Guest

    Re: using a help file to populate ftp values

    In article <>,
    Uri Guttman <> wrote:
    >>>>>> "BM" == Ben Morrow <> 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,
     
    Tim McDaniel, Aug 2, 2012
    #5
  6. Wolf Behrenhoff

    Tim McDaniel Guest

    Re: using a help file to populate ftp values

    In article <>,
    Cal Dershowitz <> wrote:
    >On 08/02/2012 04:47 AM, Rainer Weikusat wrote:
    >> Ben Morrow<> 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 => '',
    frobotz => 17,
    env => {
    home = '...',
    ...
    },
    ...
    },

    servername2 => {
    port => 777,
    database => 'Bloggs',
    email => '',
    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 => '',
    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,
     
    Tim McDaniel, Aug 3, 2012
    #6
  7. Wolf Behrenhoff

    Uri Guttman Guest

    Re: using a help file to populate ftp values

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Quoth Uri Guttman <>:
    >> >>>>> "BM" == Ben Morrow <> 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, Aug 3, 2012
    #7
  8. Wolf Behrenhoff

    Uri Guttman Guest

    Re: using a help file to populate ftp values

    >>>>> "TM" == Tim McDaniel <> writes:

    TM> In article <>,
    TM> Uri Guttman <> wrote:
    >>>>>>> "BM" == Ben Morrow <> 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
     
    Uri Guttman, Aug 3, 2012
    #8
  9. Re: using a help file to populate ftp values

    (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, Aug 3, 2012
    #9
  10. Re: using a help file to populate ftp values

    Cal Dershowitz <> 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
     
    Rainer Weikusat, Aug 4, 2012
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    770
    Reedick, Andrew
    Jul 1, 2008
  2. AAaron123
    Replies:
    2
    Views:
    2,192
    AAaron123
    Jan 16, 2009
  3. AAaron123
    Replies:
    1
    Views:
    1,348
    Oriane
    Jan 16, 2009
  4. phaylon
    Replies:
    3
    Views:
    85
    Brian McCauley
    Jan 17, 2005
  5. Wolf Behrenhoff
    Replies:
    10
    Views:
    638
    Uri Guttman
    Aug 13, 2012
Loading...

Share This Page