Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: "open" Best Practices

by davido (Cardinal)
on Jul 11, 2019 at 22:42 UTC ( [id://11102708]=note: print w/replies, xml ) Need Help??


in reply to "open" Best Practices

Perhaps mention something about paying attention to the ownership and permissions on newly created paths and files, and to consider the ramifications of locking (or failing to do so). So often we envision our solutions in terms of simple development environments not considering that there's a real world where processes can compete with each other over the same resource, or where not everyone is the same person as $ENV{'USER'}, and so on. At minimum, if our code is expected to be the only instance of itself running (so that it's not competing for a resource with other instances of itself) are we doing anything to assure it is the only instance? Have we considered that someone might be writing to, unlinking, moving, copying, reading from the same file we're working with?

Using open correctly is the easy part.


Dave

Replies are listed 'Best First'.
Re^2: "open" Best Practices
by eyepopslikeamosquito (Archbishop) on Jul 12, 2019 at 08:56 UTC
Re^2: "open" Best Practices
by haukex (Archbishop) on Jul 12, 2019 at 16:26 UTC
    Using open correctly is the easy part.

    Yes, and hence my relatively short node - I really just wanted to focus on the open statement itself, so I'd have something to link to in posts like this one. But I've added a mention of your suggestion to the root node, thanks!

Re^2: "open" Best Practices
by karlgoethebier (Abbot) on Jul 12, 2019 at 13:30 UTC
    "...the easy part."

    Well said. Some callow thoughts about some situations when things get a little bit harder: I just wondered if some ugly locking stuff could be handled with mce_open from MCE::Shared::Handle. Probably this may be yet another case of total abuse of a module. And testing with lsof if a file is in use might be an option from time to time as well. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      Hi karlgoethebier,

      The mce_open call behaves similarly to the native open in Perl. It creates a shared handle with exclusive locking handled transparently.

      Example:

      use strict; use warnings; use feature 'say'; use MCE::Hobo; use MCE::Shared; # Passing a file ref without IO::FDPass will work for \*STDIN, # \*STDOUT, \*STDERR, and \*main::DATA only. Otherwise, providing # the actual path is preferred (i.e. "/path/to/file"). mce_open my $out, '>>', \*STDOUT or die 'open failed: $!'; mce_open my $err, '>>', \*STDERR or die 'open failed: $!'; printf $out "shared fileno(\$out) is %d\n", fileno($out); printf $out "shared fileno(\$err) is %d\n", fileno($err); if ($^O ne 'MSWin32') { mce_open my $log, '>>', '/dev/null' or die 'open failed: $!'; say $log "Hello, there!"; # sent to null } # The shared-handles work with threads, MCE::Hobo, MCE::Child, # Parallel::ForkManager, and other parallel modules on CPAN. # Note: There is no reason to choose MCE::Child over MCE::Hobo # if already involving the shared-manager. sub foo { my ($id) = @_; say $out "Hello, from pid $$"; } MCE::Hobo->create('foo', $_) for 1..4; MCE::Hobo->wait_all;

      Output:

      shared fileno($out) is 1 shared fileno($err) is 2 Hello, from pid 4651 Hello, from pid 4652 Hello, from pid 4653 Hello, from pid 4654

      See also:

      https://metacpan.org/pod/MCE::Shared#DBM-SHARING
      https://metacpan.org/pod/MCE::Shared#LOGGER-DEMONSTRATION

      Regards, Mario

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found