Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: PerlIO file handle dup

by hippo (Canon)
on Mar 07, 2017 at 16:25 UTC ( #1183846=note: print w/replies, xml ) Need Help??


in reply to Re^2: PerlIO file handle dup
in thread PerlIO file handle dup

It is confusing that MCE's read works fundamentally differently from Perl's read.

It's not clear what you mean by "MCE's read" but the snippet which you quoted as

read $fh, my($buf), '4k';

is most definitely Perl's read. HTH.

Replies are listed 'Best First'.
Re^4: PerlIO file handle dup
by chris212 (Scribe) on Mar 07, 2017 at 19:28 UTC

    MCE's read is how read behaves when an MCE::Shared file handle is used. MCE must create an I/O layer which makes Perl's read work differently, but it does not behave the same. First of all, AFAIK, Perl's read does not support the type suffix such as "k". More importantly, Perl's read will read the exact length of characters in the 3rd argument (unless EOF). MCE's read will do that, but then continue reading until it reaches $/ so it does not split records between reads. This is very useful, but still confusing. It would make more sense to have read behave the same with or without MCE, but implement another function such as "getlines".

    MCE::Shared::Handle

      Regarding MCE, MCE::Flow, and friends, the chunk_size option is dual mode. A value greater than 8192 means to read n_bytes and till the end of line. A value lower or equal to 8192 reads number of lines. I used the word lines to indicate the default $/ = "\n" or RS => "\n".

      Regarding MCE::Shared and MCE::Shared::Handle, which have native-like usage, read behaves similarly to the native read function without the 'k' or 'm' suffix. The 'k' or 'm' suffix is extra functionality to get MCE-like chunking capabilities. Basically, to have read continue reading till the end of line.

      # this feels closer to Perl-like read with extra functionality $n_chars = read $fh, $buf, "4k"; # this is another option, but involves more typing @lines = tied(%{$fh})->getlines(500);

      $fh is a tied object so that it can be used natively. The extra behaviour applies to read only for chunk IO capabilities. Currently, it is not 100% parity with MCE, because Perl's read function cannot store into an array. Therefore, I may have MCE::Shared and MCE:::Shared::Handle export "mce_read", mentioned here, to have 100% parity with MCE's chunking engine.

      Regards, Mario.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1183846]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2019-06-17 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (80 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!