Professional Web Applications Themes

Regexp constructed from command line arguments. - PERL Miscellaneous

ddtl <this.is.invalid> wrote: > you > pass it a directory name to use, regexp to search for > 3) if ($entry[2] eq "") {next;} I like to say that like this: next unless length $entry[2]; 'cause excessive punctuation can get in the way of reading it. > 4) $name_before[$n] = "$entry[1]" . "$entry[0]" . "$entry[2]"; See the Perl FAQ: What's wrong with always quoting "$vars"? and join my anti-excessive-punctuation crusade :-) $name_before[$n] = "$entry[1]$entry[0]$entry[2]"; or, perhaps better: $name_before[$n] = join '', entry[1, 0, 2]; > 5) $entry[0] =~ s/\Q$opts{'s'}\E/\Q$opts{'r'}\E/g; ^^ ^^ Why did you put that there? What were you ...

  1. #1

    Default Re: Regexp constructed from command line arguments.

    ddtl <this.is.invalid> wrote:
    > you
    > pass it a directory name to use, regexp to search for
    > 3) if ($entry[2] eq "") {next;}

    I like to say that like this:

    next unless length $entry[2];

    'cause excessive punctuation can get in the way of reading it.

    > 4) $name_before[$n] = "$entry[1]" . "$entry[0]" . "$entry[2]";

    See the Perl FAQ:

    What's wrong with always quoting "$vars"?

    and join my anti-excessive-punctuation crusade :-)


    $name_before[$n] = "$entry[1]$entry[0]$entry[2]";

    or, perhaps better:

    $name_before[$n] = join '', entry[1, 0, 2];

    > 5) $entry[0] =~ s/\Q$opts{'s'}\E/\Q$opts{'r'}\E/g;
    ^^
    ^^

    Why did you put that there?

    What were you hoping that would do for you?

    Do you _want_ to insert extra backslash charcters into
    the replacement string?

    > 7) $n++;

    You should probably be letting Perl do the indexing for you by
    using push() instead of doing your own indexing into entry.

    push entry, "$entry[1]$entry[0]$entry[2]"; # Look Ma! No $n needed!


    --
    Tad McClellan SGML consulting
    [email]tadmcaugustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  2. #2

    Default Re: Regexp constructed from command line arguments.

    ddtl <this.is.invalid> wrote:

    > $a=~s/([\d]{2})/0\1/g;

    You should always enable warnings when developing Perl code!

    $a (and $b) are poor choices of variable names, they are used
    by sort() too, values could get mangled...

    The pattern would be easier to read and understand if written as:

    $x =~ s/(\d\d)/0$1/g;


    > print "$a";

    See the Perl FAQ:

    What's wrong with always quoting "$vars"?

    > What is another problem here?

    You only get one round of interpolation, and you are using it to
    fetch the value from the hash. You'd need an additional round
    to interpolate it yet again.

    > But why it is so -

    Because if you had:

    $_ = '$100.00';
    s/(\$1)/$1/g;

    and Perl kept doing rounds of interpolation until there was
    nothing left to interpolate, it would never finish. :-)

    So it only does one round.

    The solution to your problem can be found in the Perl FAQ:

    How can I expand variables in text strings?

    > containing regexp?

    The variable you are speaking of DOES NOT contain a regex.

    The replacement string is a (double quotish) _string_, not a regex.


    --
    Tad McClellan SGML consulting
    [email]tadmcaugustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  3. #3

    Default Re: Regexp constructed from command line arguments.

    [email]tadmcaugustmail.com[/email] (Tad McClellan) writes:
    > The solution to your problem can be found in the Perl FAQ:
    >
    > How can I expand variables in text strings?
    See also numerous previous threads discussing this FAQ (do a google
    groups search on the exact phrase) since the answer given in the FAQ
    is not really very good.

    A patch to replace it with, IMNSHO, a much better answer can be found:

    [url]http://www.wcl.bham.ac.uk/pub/bam/patches/perl/perlfaq4-scalar-interpolate-take-3.diff[/url]

    The question/answer reads:

    How can I expand/interpolate variables in text strings?

    You can process a string through Perl's interpolation
    engine like this:

    $text = 'this has a $foo in it...\n ...and a $bar';
    # Assume $text does not contain "\nEND\n"
    chop ( $text = eval "<<END\n$text\nEND\n" );
    die if $;

    This will not work, and for good reason, if $text is com-
    ing form an tainted source. For explanation of how $text
    could execute arbitrary Perl code see ``How do I expand
    function calls in a string?'' in this section of the FAQ.

    If $text is coming from a source external to the Perl
    script (typically a file) and you would be content to
    trust executable code from that source then you simply
    make data from that source untainted. This is no more or
    less dangerous than using "do()". For an explaination of
    tainting see the perlsec manpage.

    If you do not trust the source that much then you can
    limit and launder the parts of the string that are passed
    to eval() something like this:

    $text =~ s/(\$\w+)/$1/eeg; # needed /ee, not /e

    This still gives unrestricted access to your scalar vari-
    ables. It is often better to use a hash:

    %user_defs = (
    foo => 23,
    bar => 19,
    );
    $text =~ s/\$(\w+)/$user_defs{$1}/g;

    For other variations on the theme of text templates see
    the sprintf() function and numerous modules on CPAN.

    However the OP's question was actually about s///. Whilst the FAQ
    that Tad mentions is somewhat applicable I think the OP's exact
    question appears often enough that is should qualify as a FAQ in it's
    own right. Like most FAQs this one seems to come in bursts - it goes
    unasked for months then is asked be several people in the course of a
    couple of weeks (as it has been in the last couple of weeks).

    A good previous thread in this FAQ please see the thread following message
    <a3m4ff$5ra$1news.surfnet.nl>.

    [url]http://groups.google.com/groups?selm=a3m4ff%245ra%241%40news.surfnet.nl[/url]

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley Guest

Similar Threads

  1. Replies: 7
    Last Post: November 12th, 06:53 PM
  2. Replies: 0
    Last Post: October 27th, 11:14 AM
  3. command line arguments
    By pop83in in forum Dreamweaver AppDev
    Replies: 0
    Last Post: March 28th, 07:54 AM
  4. Any way to pass arguments to projectors from Windows command line?
    By matt_s webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 5
    Last Post: November 30th, 10:57 PM
  5. RUN/execute a Command-Line command from an ASP page.
    By Lucas Cowald in forum ASP Components
    Replies: 7
    Last Post: October 22nd, 01:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139