in reply to Re^6: magic-diamond <> behavior -- WHAT?! (sanity)
in thread magic-diamond <> behavior -- WHAT?!

Well, what most people ranting about magic open say is that files ending in '|' get created either by accident, or they are there to try to exploit something. In such cases, I would like to see the files removed; I rather have my filter stop and complain in stead of silently processing the file and leaving the potential exploit there.

Now, if you actually want to have files that start with '<', '>', '|' or whitespace, or end with '|' or whitespace, then you are in for a lot of suffering - not just from Perl, but from the shell as well. But still, Perl has a documented solution (see for instance, 'perlopentut'):

BEGIN {s!^(\s)!./$1!,s/^/</,s/$/\0/ for @ARGV}
Yes, and that works with -i as well.

Replies are listed 'Best First'.
Re^8: magic-diamond <> behavior -- WHAT?! (-i)
by tye (Sage) on Oct 31, 2008 at 04:07 UTC
    Yes, and that works with -i as well.

    Doesn't seem to:

    > perl -i.bak -pe "BEGIN{s!^(\s)!./$1!,s/^/</,s/$/\0/ for @ARGV} s/fo +o/bar/" bar Can't open <bar: No such file or directory. > perl -v This is perl, v5.10.0 built for cygwin-thread-multi-64int (with 6 registered patches, see perl -V for more detail)

    Putting '<' on the front breaks -i. Putting "\0" on the end breaks -i.bak. Prepending "./" (at least on platforms that support that directory syntax) doesn't break either use of -i, but that still leaves files that end with space or '|' out.

    But I still boggle that you think it is better to make the 99% case this much harder while refusing to make the "cute hack" magical case any harder at all. So much for proper Huffman coding (much less defaulting to 'safe' behavior and requiring a slight acknowledgement for something dangerous).

    echo > '"Reason for this Perl insanity?" || rm -rf .. |'

    - tye        

Re^8: magic-diamond <> behavior -- WHAT?! (sanity)
by ikegami (Patriarch) on Oct 30, 2008 at 23:46 UTC

    not just from Perl, but from the shell as well.

    What shell are you using? Besides, most people have never seen a shell. They don't know about such oddities. They don't care about such oddities. They create all sort of weird file names.

Re^8: magic-diamond <> behavior -- WHAT?! (sanity)
by educated_foo (Vicar) on Oct 28, 2009 at 02:43 UTC
    Now, if you actually want to have files that start with '<', '>', '|' or whitespace, or end with '|' or whitespace...
    ...then you are a masochist, and deserve every bit of your self-inflicted pain. Why make your life harder than it has to be?