I see the system-ed perl as an autonomous process (unknowing of its parent process)

As you are probably aware, system is equivalent to fork followed by exec.

You are also probably aware tha fork preserves open file descriptors. This is why to create a daemon, it is necessary to fork twice. You fork once, close the standard handles in the child; and then fork a second time. Only then does the second child become disassociated with the terminal and a true daemon.

What you may not be familiar with is that (various forms of) exec are front end for execve. And that execve() also preserves open file descriptors. (Except those marked close-on-exec.)

To quote the above man page:By default, file descriptors remain open across an execve().

You can prove this to yourself. Run this one-liner (suitably adjusted):

perl -e"system qq[ $^X -e\"\$n=123; print \$n\" ];" 123

And you'll see the output 123

Now try this modified version:

C:\test>perl -e"close STDOUT; system qq[ $^X -e\"$n=123; print \$n\" ] +;"

Where did the output disappear to?

So bang goes the autonomous process theory.

In both cases, we're printing out a string with one character at codepoint U+00FF.

No. The return value from pack 'B8', ... is not a character; nor a codepoint; and absolutely nothing to do with Unicode.

It is a byte! An 8-bit unsigned number bit pattern stored in a 8-bit unit of memory and nothing else.

No interpretation of the meaning (nor even signedness) is placed (nor could be) upon that value until you do something with it!

The second system-ed perl has its output set ...

You're right that the interpretation applied to the 8-bit value is not preserved across the fork/exec pair, but not because of your reasoning.

The important part is that the OS cannot preserve what it has no knowledge of. There is no concept of encoding attached to the file descriptors.

It is also likely, though I haven't confirmed this, that Perl reopens the standard handles when it starts.

The bottom line -- for this thread, rather than this subthread -- is that the OP must have omitted some details from his scenario.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re^8: Standard handles inherited from a utf-8 enabled shell by BrowserUk
in thread Standard handles inherited from a utf-8 enabled shell by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.