singam has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: searching the strings
by Zaxo (Archbishop) on May 10, 2006 at 13:04 UTC

    If you plan to plug these filenames into two-arg open, you should redesign and start working with three+ arguments. That seperates actual filenames from what you plan to do with them. You won't need to parse the mode directives out of the filenames then. Consider that a file's actual name may start with those symbols, even though it really confuses the shell if unquoted and unescaped.

    That said, the kind of parsing you want can be done like this:

    sub name_parse { local $_ = shift; m/^>>/ and warn 'Error message' and return(); m/^([><])/ and return ($1, substr($_, 1)); m/^\|/ and print 'pipefront'; m/\|$/ and print 'pipeend'; return $_; } my @mulch = name_parse($filename);
    You should read perlre to see how those matches pick out the features that interest you.

    After Compline,
    Zaxo

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: searching the strings
by prasadbabu (Prior) on May 10, 2006 at 12:52 UTC

    Hi Singam,

    Your question is not clear. Take a look at How (Not) To Ask A Question.

    Also always use use strict, use warnings. If i understood your question correctly, here is my try

    use strict; use warnings; #my $filename = "<test.txt"; #example 1 #my $filename = ">test.txt"; #example 2 #my $filename = ">>test.txt"; #example 3 my $filename = "|test.txt"; if ($filename =~ /^\|\s*test/ ) { print "\npipefront\n"; } elsif ($filename =~ /test\.txt\s*\|$/ ) { print "\npipeend\n"; }

    Prasad

Re: searching the strings
by ikegami (Patriarch) on May 10, 2006 at 16:14 UTC
    It sounds like you are you trying to prevent users from making your open do something unexpected. Why don't you just use the 3-arg version of open (e.g. open(FH, '<', $filename)) instead? With the three 3-arg open, >, <, etc no longer have special meaning in the filename argument.
Re: searching the strings
by davido (Cardinal) on May 10, 2006 at 18:49 UTC

    In light of your recent question, design the open function, it sounds to me like you're trying to search through a bunch of Perl scripts to change the way your open commands are written, without having to perform those edits by hand. In other words, I think this question is related somehow to the question you asked a day ago. If that's the case, you're still approaching the problem the wrong way. Rather than trying to do an s/// operation on each open command within all of your scripts, you could just insert the use open ':locale'; pragma at the top of each of those scripts, as I pointed out in Re^3: design the open function.

    If I'm misunderstanding the question, or simply reading too much into your recent questions, I apologize, and implore you to give us a hint at the bigger picture; what it is you really want to do, so that we might help you find a real solution rather than a kludge.


    Dave

Re: searching the strings
by Hue-Bond (Priest) on May 10, 2006 at 13:00 UTC

    Let's see if I understood everything:

    foreach (@w) { print "working with $_... "; /^>>/ and do { print "error message\n"; next }; /^\s*([<>])\s*(\S+)/ and do { print "separated '$1' from '$2'\n"; ne +xt }; /^\s*\|/ and do { print "pipefront\n"; next }; /\|\s*$/ and do { print "pipeend\n"; next }; print "\n"; } __END__ working with >test.txt... separated '>' from 'test.txt' working with <test.txt... separated '<' from 'test.txt' working with >>test.txt... error message working with | test... pipefront working with test |... pipeend

    This probably needs more refinement but I think I'll leave that to you ;).

    Update: Code beautification.

    --
    David Serrano

      /^>>/ and do { print "error message\n"; next };

      I like to use short-circuiting logical operators for flow control, and to omit unnecessary parentheses, but I'd avoid using do like that, if possible. How 'bout the following instead?

      print("error message\n"), next if /^>>/; # or (print "error message\n"), next if /^>>/;

        I've seen it in the Camel ("2.6. Statements and declarations", "Bare blocks and case structures"), so I thought it would be ok. Indeed I think it's ok since it is listed only as one of several alternatives (another one of which is the one you posted).

        --
        David Serrano

Re: searching the strings
by blazar (Canon) on May 10, 2006 at 12:54 UTC

    Is "test.txt" (or "test") supposed to be fixed, and hence hardcoded? If not, then which chars can it hold? Any significative answer would depend on knowing such info...

    A reply falls below the community's threshold of quality. You may see it by logging in.